xref: /openbmc/libcper/json-schema.c (revision d34f2b11)
18a2d737cSLawrence Tang /**
28a2d737cSLawrence Tang  * A very basic, non-complete implementation of a validator for the JSON Schema specification,
38a2d737cSLawrence Tang  * for validating CPER-JSON.
48a2d737cSLawrence Tang  *
58a2d737cSLawrence Tang  * Author: Lawrence.Tang@arm.com
68a2d737cSLawrence Tang  **/
78a2d737cSLawrence Tang 
88a2d737cSLawrence Tang #include <stdio.h>
98a2d737cSLawrence Tang #include <string.h>
108a2d737cSLawrence Tang #include <unistd.h>
118a2d737cSLawrence Tang #include <libgen.h>
128a2d737cSLawrence Tang #include <limits.h>
138f793ac3SLawrence Tang #include <stdarg.h>
148a2d737cSLawrence Tang #include "json.h"
158a2d737cSLawrence Tang #include "json-schema.h"
168a2d737cSLawrence Tang #include "edk/BaseTypes.h"
178a2d737cSLawrence Tang 
188f793ac3SLawrence Tang //Field definitions.
198f793ac3SLawrence Tang int json_validator_debug = 0;
208f793ac3SLawrence Tang 
218a2d737cSLawrence Tang //Private pre-definitions.
228a2d737cSLawrence Tang int validate_field(const char* name, json_object* schema, json_object* object, char* error_message);
238a2d737cSLawrence Tang int validate_integer(const char* field_name, json_object* schema, json_object* object, char* error_message);
248a2d737cSLawrence Tang int validate_string(const char* field_name, json_object* schema, json_object* object, char* error_message);
258a2d737cSLawrence Tang int validate_object(const char* field_name, json_object* schema, json_object* object, char* error_message);
268a2d737cSLawrence Tang int validate_array(const char* field_name, json_object* schema, json_object* object, char* error_message);
278f793ac3SLawrence Tang void log_validator_error(char* error_message, const char* format, ...);
288f793ac3SLawrence Tang void log_validator_debug(const char* format, ...);
298f793ac3SLawrence Tang void log_validator_msg(const char* format, va_list args);
308a2d737cSLawrence Tang 
318a2d737cSLawrence Tang //Validates a single JSON object against a provided schema file, returning 1 on success and 0 on failure to validate.
328a2d737cSLawrence Tang //Error message space must be allocated prior to call.
338a2d737cSLawrence Tang int validate_schema_from_file(const char* schema_file, json_object* object, char* error_message)
348a2d737cSLawrence Tang {
358a2d737cSLawrence Tang     //Load schema IR from file.
368a2d737cSLawrence Tang     json_object* schema_ir = json_object_from_file(schema_file);
378a2d737cSLawrence Tang     if (schema_ir == NULL)
388a2d737cSLawrence Tang     {
398f793ac3SLawrence Tang         log_validator_error(error_message, "Failed to load schema from file '%s'.", schema_file);
408a2d737cSLawrence Tang         return 0;
418a2d737cSLawrence Tang     }
428a2d737cSLawrence Tang 
438a2d737cSLawrence Tang     //Get the directory of the file.
448a2d737cSLawrence Tang     char* schema_file_copy = malloc(strlen(schema_file) + 1);
458a2d737cSLawrence Tang     strcpy(schema_file_copy, schema_file);
468a2d737cSLawrence Tang     char* schema_dir = dirname(schema_file_copy);
478a2d737cSLawrence Tang 
488a2d737cSLawrence Tang     int result = validate_schema(schema_ir, schema_dir, object, error_message);
498a2d737cSLawrence Tang 
508a2d737cSLawrence Tang     //Free memory from directory call.
518a2d737cSLawrence Tang     free(schema_file_copy);
528a2d737cSLawrence Tang 
538a2d737cSLawrence Tang     return result;
548a2d737cSLawrence Tang }
558a2d737cSLawrence Tang 
568a2d737cSLawrence Tang //Validates a single JSON object against a provided schema, returning 1 on success and 0 on failure to validate.
578a2d737cSLawrence Tang //Error message space must be allocated prior to call.
588a2d737cSLawrence Tang //If the schema does not include any other sub-schemas using "$ref", then leaving schema_directory as NULL is valid.
598a2d737cSLawrence Tang int validate_schema(json_object* schema, char* schema_directory, json_object* object, char* error_message)
608a2d737cSLawrence Tang {
618a2d737cSLawrence Tang     //Check that the schema version is the same as this validator.
628a2d737cSLawrence Tang     json_object* schema_ver = json_object_object_get(schema, "$schema");
638a2d737cSLawrence Tang     if (schema_ver == NULL || strcmp(json_object_get_string(schema_ver), JSON_SCHEMA_VERSION))
648a2d737cSLawrence Tang     {
658f793ac3SLawrence Tang         log_validator_error(error_message, "Provided schema is not of the same version that is referenced by this validator, or is not a schema.");
668a2d737cSLawrence Tang         return 0;
678a2d737cSLawrence Tang     }
688a2d737cSLawrence Tang 
698a2d737cSLawrence Tang     //Change current directory into the schema directory.
708a2d737cSLawrence Tang     char* original_cwd = malloc(PATH_MAX);
718a2d737cSLawrence Tang     if (getcwd(original_cwd, PATH_MAX) == NULL)
728a2d737cSLawrence Tang     {
738f793ac3SLawrence Tang         log_validator_error(error_message, "Failed fetching the current directory.");
748a2d737cSLawrence Tang         return 0;
758a2d737cSLawrence Tang     }
768a2d737cSLawrence Tang     if (chdir(schema_directory))
778a2d737cSLawrence Tang     {
788f793ac3SLawrence Tang         log_validator_error(error_message, "Failed to chdir into schema directory.");
798a2d737cSLawrence Tang         return 0;
808a2d737cSLawrence Tang     }
818a2d737cSLawrence Tang 
828a2d737cSLawrence Tang     //Parse the top level structure appropriately.
838a2d737cSLawrence Tang     int result = validate_field("parent", schema, object, error_message);
848a2d737cSLawrence Tang 
858a2d737cSLawrence Tang     //Change back to original CWD.
868a2d737cSLawrence Tang     chdir(original_cwd);
878a2d737cSLawrence Tang     free(original_cwd);
888a2d737cSLawrence Tang 
89*d34f2b11SLawrence Tang     if (result)
90*d34f2b11SLawrence Tang         log_validator_debug("Successfully validated the provided object against schema.");
918a2d737cSLawrence Tang     return result;
928a2d737cSLawrence Tang }
938a2d737cSLawrence Tang 
948a2d737cSLawrence Tang //Validates a single JSON field given a schema/object.
958f793ac3SLawrence Tang //Returns -1 on fatal/error failure, 0 on validation failure, and 1 on validation.
968a2d737cSLawrence Tang int validate_field(const char* field_name, json_object* schema, json_object* object, char* error_message)
978a2d737cSLawrence Tang {
988f793ac3SLawrence Tang     log_validator_debug("Validating field '%s'...", field_name);
998f793ac3SLawrence Tang 
1008a2d737cSLawrence Tang     //If there is a "$ref" field, attempt to load the referenced schema.
1018a2d737cSLawrence Tang     json_object* ref_schema = json_object_object_get(schema, "$ref");
1028a2d737cSLawrence Tang     if (ref_schema != NULL && json_object_get_type(ref_schema) == json_type_string)
1038a2d737cSLawrence Tang     {
1048f793ac3SLawrence Tang         log_validator_debug("$ref schema detected for field '%s'.", field_name);
1058f793ac3SLawrence Tang 
1068a2d737cSLawrence Tang         //Attempt to load. If loading fails, report error.
1078a2d737cSLawrence Tang         const char* ref_path = json_object_get_string(ref_schema);
1088a2d737cSLawrence Tang         schema = json_object_from_file(ref_path);
1098a2d737cSLawrence Tang         if (schema == NULL)
1108a2d737cSLawrence Tang         {
1118f793ac3SLawrence Tang             log_validator_error(error_message, "Failed to open referenced schema file '%s'.", ref_path);
1128f793ac3SLawrence Tang             return -1;
1138a2d737cSLawrence Tang         }
1148f793ac3SLawrence Tang 
1158f793ac3SLawrence Tang         log_validator_debug("loaded schema path '%s' for field '%s'.", ref_path, field_name);
1168a2d737cSLawrence Tang     }
1178a2d737cSLawrence Tang 
1188a2d737cSLawrence Tang     //Get the schema field type.
1198a2d737cSLawrence Tang     json_object* desired_field_type = json_object_object_get(schema, "type");
1208a2d737cSLawrence Tang     if (desired_field_type == NULL || !json_object_is_type(desired_field_type, json_type_string))
1218a2d737cSLawrence Tang     {
1228f793ac3SLawrence Tang         log_validator_error(error_message, "Desired field type not provided within schema/is not a string for field '%s' (schema violation).", field_name);
1238f793ac3SLawrence Tang         return -1;
1248a2d737cSLawrence Tang     }
1258a2d737cSLawrence Tang 
1268a2d737cSLawrence Tang     //Check the field types are actually equal.
1278a2d737cSLawrence Tang     const char* desired_field_type_str = json_object_get_string(desired_field_type);
1288a2d737cSLawrence Tang     if (!(
1298a2d737cSLawrence Tang         (!strcmp(desired_field_type_str, "object") && json_object_is_type(object, json_type_object))
1308a2d737cSLawrence Tang         || (!strcmp(desired_field_type_str, "array") && json_object_is_type(object, json_type_array))
1318a2d737cSLawrence Tang         || (!strcmp(desired_field_type_str, "integer") && json_object_is_type(object, json_type_int))
1328a2d737cSLawrence Tang         || (!strcmp(desired_field_type_str, "string") && json_object_is_type(object, json_type_string))
1338a2d737cSLawrence Tang         || (!strcmp(desired_field_type_str, "boolean") && json_object_is_type(object, json_type_boolean))
1348a2d737cSLawrence Tang         || (!strcmp(desired_field_type_str, "double") && json_object_is_type(object, json_type_double))
1358a2d737cSLawrence Tang     ))
1368a2d737cSLawrence Tang     {
1378f793ac3SLawrence Tang         log_validator_error(error_message, "Field type match failed for field '%s'.", field_name);
1388a2d737cSLawrence Tang         return 0;
1398a2d737cSLawrence Tang     }
1408a2d737cSLawrence Tang 
1418f793ac3SLawrence Tang     //If the schema contains a "oneOf" array, we need to validate the field against each of the
1428f793ac3SLawrence Tang     //possible options in turn.
1438f793ac3SLawrence Tang     json_object* one_of = json_object_object_get(schema, "oneOf");
1448f793ac3SLawrence Tang     if (one_of != NULL && json_object_get_type(one_of) == json_type_array)
1458f793ac3SLawrence Tang     {
1468f793ac3SLawrence Tang         log_validator_debug("oneOf options detected for field '%s'.", field_name);
1478f793ac3SLawrence Tang 
1488f793ac3SLawrence Tang         int len = json_object_array_length(one_of);
1498f793ac3SLawrence Tang         int validated = 0;
1508f793ac3SLawrence Tang         for (int i=0; i<len; i++)
1518f793ac3SLawrence Tang         {
1528f793ac3SLawrence Tang             //If the "oneOf" member isn't an object, warn on schema violation.
1538f793ac3SLawrence Tang             json_object* one_of_option = json_object_array_get_idx(one_of, i);
1548f793ac3SLawrence Tang             if (one_of_option == NULL || json_object_get_type(one_of_option) != json_type_object)
1558f793ac3SLawrence Tang             {
1568f793ac3SLawrence Tang                 log_validator_debug("Schema Warning: 'oneOf' member for field '%s' is not an object, schema violation.", field_name);
1578f793ac3SLawrence Tang                 continue;
1588f793ac3SLawrence Tang             }
1598f793ac3SLawrence Tang 
1608f793ac3SLawrence Tang             //Validate field with schema.
1618f793ac3SLawrence Tang             validated = validate_field(field_name, one_of_option, object, error_message);
1628f793ac3SLawrence Tang             if (validated == -1)
1638f793ac3SLawrence Tang                 return -1;
1648f793ac3SLawrence Tang             if (validated)
1658f793ac3SLawrence Tang                 break;
1668f793ac3SLawrence Tang         }
1678f793ac3SLawrence Tang 
1688f793ac3SLawrence Tang         //Return if failed all checks.
1698f793ac3SLawrence Tang         if (!validated)
1708f793ac3SLawrence Tang         {
1718f793ac3SLawrence Tang             log_validator_error(error_message, "No schema object structures matched provided object for field '%s'.", field_name);
1728f793ac3SLawrence Tang             return 0;
1738f793ac3SLawrence Tang         }
1748f793ac3SLawrence Tang     }
17545e04b0aSLawrence Tang 
1768a2d737cSLawrence Tang     //Switch and validate each type in turn.
1778a2d737cSLawrence Tang     switch (json_object_get_type(object))
1788a2d737cSLawrence Tang     {
1798a2d737cSLawrence Tang         case json_type_int:
1808a2d737cSLawrence Tang             return validate_integer(field_name, schema, object, error_message);
1818a2d737cSLawrence Tang         case json_type_string:
1828a2d737cSLawrence Tang             return validate_string(field_name, schema, object, error_message);
1838a2d737cSLawrence Tang         case json_type_object:
1848a2d737cSLawrence Tang             return validate_object(field_name, schema, object, error_message);
1858a2d737cSLawrence Tang         case json_type_array:
1868f793ac3SLawrence Tang             return validate_array(field_name, schema, object, error_message);
1878a2d737cSLawrence Tang 
1888a2d737cSLawrence Tang         //We don't perform extra validation on this type.
1898a2d737cSLawrence Tang         default:
1908f793ac3SLawrence Tang             log_validator_debug("validation passed for '%s' (no extra validation).", field_name);
1918a2d737cSLawrence Tang             return 1;
1928a2d737cSLawrence Tang     }
1938a2d737cSLawrence Tang }
1948a2d737cSLawrence Tang 
1958a2d737cSLawrence Tang //Validates a single integer value according to the given specification.
1968a2d737cSLawrence Tang int validate_integer(const char* field_name, json_object* schema, json_object* object, char* error_message)
1978a2d737cSLawrence Tang {
1988a2d737cSLawrence Tang     //Is there a minimum/maximum specified? If so, check those.
1998a2d737cSLawrence Tang     //Validate minimum.
2008a2d737cSLawrence Tang     json_object* min_value = json_object_object_get(schema, "minimum");
2018a2d737cSLawrence Tang     if (min_value != NULL && json_object_is_type(min_value, json_type_int))
2028a2d737cSLawrence Tang     {
2038a2d737cSLawrence Tang         int min_value_int = json_object_get_int(min_value);
2048a2d737cSLawrence Tang         if (json_object_get_uint64(object) < min_value_int)
2058a2d737cSLawrence Tang         {
2068f793ac3SLawrence Tang             log_validator_error(error_message, "Failed to validate integer field '%s'. Value was below minimum of %d.", field_name, min_value_int);
2078a2d737cSLawrence Tang             return 0;
2088a2d737cSLawrence Tang         }
2098a2d737cSLawrence Tang     }
2108a2d737cSLawrence Tang 
2118a2d737cSLawrence Tang     //Validate maximum.
2128a2d737cSLawrence Tang     json_object* max_value = json_object_object_get(schema, "maximum");
2138a2d737cSLawrence Tang     if (max_value != NULL && json_object_is_type(max_value, json_type_int))
2148a2d737cSLawrence Tang     {
2158a2d737cSLawrence Tang         int max_value_int = json_object_get_int(max_value);
2168a2d737cSLawrence Tang         if (json_object_get_uint64(object) > max_value_int)
2178a2d737cSLawrence Tang         {
2188f793ac3SLawrence Tang             log_validator_error(error_message, "Failed to validate integer field '%s'. Value was above maximum of %d.", field_name, max_value_int);
2198a2d737cSLawrence Tang             return 0;
2208a2d737cSLawrence Tang         }
2218a2d737cSLawrence Tang     }
2228a2d737cSLawrence Tang 
2238a2d737cSLawrence Tang     return 1;
2248a2d737cSLawrence Tang }
2258a2d737cSLawrence Tang 
2268a2d737cSLawrence Tang //Validates a single string value according to the given specification.
2278a2d737cSLawrence Tang int validate_string(const char* field_name, json_object* schema, json_object* object, char* error_message)
2288a2d737cSLawrence Tang {
2298a2d737cSLawrence Tang     //todo: if there is a "pattern" field, verify the string with RegEx.
2308a2d737cSLawrence Tang     return 1;
2318a2d737cSLawrence Tang }
2328a2d737cSLawrence Tang 
2338a2d737cSLawrence Tang //Validates a single object value according to the given specification.
2348a2d737cSLawrence Tang int validate_object(const char* field_name, json_object* schema, json_object* object, char* error_message)
2358a2d737cSLawrence Tang {
2368a2d737cSLawrence Tang     //Are there a set of "required" fields? If so, check they all exist.
2378a2d737cSLawrence Tang     json_object* required_fields = json_object_object_get(schema, "required");
2388a2d737cSLawrence Tang     if (required_fields != NULL && json_object_get_type(required_fields) == json_type_array)
2398a2d737cSLawrence Tang     {
2408f793ac3SLawrence Tang         log_validator_debug("Required fields found for '%s', matching...", field_name);
2418f793ac3SLawrence Tang 
2428a2d737cSLawrence Tang         int len = json_object_array_length(required_fields);
2438a2d737cSLawrence Tang         for (int i=0; i<len; i++)
2448a2d737cSLawrence Tang         {
2458a2d737cSLawrence Tang             //Get the required field from schema.
2468a2d737cSLawrence Tang             json_object* required_field = json_object_array_get_idx(required_fields, i);
2478a2d737cSLawrence Tang             if (json_object_get_type(required_field) != json_type_string)
2488a2d737cSLawrence Tang             {
2498f793ac3SLawrence Tang                 log_validator_error(error_message, "Required field for object '%s' is not a string (schema violation).", field_name);
2508a2d737cSLawrence Tang                 return 0;
2518a2d737cSLawrence Tang             }
2528a2d737cSLawrence Tang 
2538a2d737cSLawrence Tang             //Does it exist in the object?
2548a2d737cSLawrence Tang             const char* required_field_str = json_object_get_string(required_field);
2558a2d737cSLawrence Tang             if (json_object_object_get(object, required_field_str) == NULL)
2568a2d737cSLawrence Tang             {
2578f793ac3SLawrence Tang                 log_validator_error(error_message, "Required field '%s' was not present in object '%s'.", required_field_str, field_name);
2588a2d737cSLawrence Tang                 return 0;
2598a2d737cSLawrence Tang             }
2608a2d737cSLawrence Tang         }
2618a2d737cSLawrence Tang     }
2628a2d737cSLawrence Tang 
263c4814594SLawrence Tang     //Get additional properties value in advance.
264c4814594SLawrence Tang     json_object* additional_properties = json_object_object_get(schema, "additionalProperties");
265c4814594SLawrence Tang     int additional_properties_allowed = 0;
266c4814594SLawrence Tang     if (additional_properties != NULL && json_object_get_type(additional_properties) == json_type_boolean)
267c4814594SLawrence Tang         additional_properties_allowed = json_object_get_boolean(additional_properties);
2688a2d737cSLawrence Tang 
2698a2d737cSLawrence Tang     //Run through the "properties" object and validate each of those in turn.
2708a2d737cSLawrence Tang     json_object* properties = json_object_object_get(schema, "properties");
2718a2d737cSLawrence Tang     if (properties != NULL && json_object_get_type(properties) == json_type_object)
2728a2d737cSLawrence Tang     {
2738a2d737cSLawrence Tang         json_object_object_foreach(properties, key, value) {
2748a2d737cSLawrence Tang 
2758a2d737cSLawrence Tang             //If the given property name does not exist on the target object, ignore and continue next.
2768a2d737cSLawrence Tang             json_object* object_prop = json_object_object_get(object, key);
2778a2d737cSLawrence Tang             if (object_prop == NULL)
2788a2d737cSLawrence Tang                 continue;
2798a2d737cSLawrence Tang 
2808a2d737cSLawrence Tang             //Validate against the schema.
2818a2d737cSLawrence Tang             if (!validate_field(key, value, object_prop, error_message))
2828a2d737cSLawrence Tang                 return 0;
2838a2d737cSLawrence Tang         }
284c4814594SLawrence Tang 
285c4814594SLawrence Tang         //If additional properties are banned, validate that no additional properties exist.
286c4814594SLawrence Tang         if (!additional_properties_allowed)
287c4814594SLawrence Tang         {
288c4814594SLawrence Tang             json_object_object_foreach(object, key, value) {
289c4814594SLawrence Tang 
290c4814594SLawrence Tang                 //If the given property name does not exist on the schema object, fail validation.
291c4814594SLawrence Tang                 json_object* schema_prop = json_object_object_get(properties, key);
292c4814594SLawrence Tang                 if (schema_prop == NULL)
293c4814594SLawrence Tang                 {
294c4814594SLawrence Tang                     log_validator_error(error_message, "Invalid additional property '%s' detected on field '%s'.", key, field_name);
295c4814594SLawrence Tang                     return 0;
296c4814594SLawrence Tang                 }
297c4814594SLawrence Tang             }
298c4814594SLawrence Tang         }
2998a2d737cSLawrence Tang     }
3008a2d737cSLawrence Tang 
3018a2d737cSLawrence Tang     return 1;
3028a2d737cSLawrence Tang }
3038a2d737cSLawrence Tang 
3048a2d737cSLawrence Tang //Validates a single array value according to the given specification.
3058a2d737cSLawrence Tang int validate_array(const char* field_name, json_object* schema, json_object* object, char* error_message)
3068a2d737cSLawrence Tang {
30745e04b0aSLawrence Tang     //Iterate all items in the array, and validate according to the "items" schema.
30845e04b0aSLawrence Tang     json_object* items_schema = json_object_object_get(schema, "items");
30945e04b0aSLawrence Tang     if (items_schema != NULL && json_object_get_type(items_schema) == json_type_object)
31045e04b0aSLawrence Tang     {
31145e04b0aSLawrence Tang         int array_len = json_object_array_length(object);
31245e04b0aSLawrence Tang         for (int i=0; i<array_len; i++)
31345e04b0aSLawrence Tang         {
31445e04b0aSLawrence Tang             if (!validate_field(field_name, items_schema, json_object_array_get_idx(object, i), error_message))
31545e04b0aSLawrence Tang                 return 0;
31645e04b0aSLawrence Tang         }
31745e04b0aSLawrence Tang     }
31845e04b0aSLawrence Tang 
3198a2d737cSLawrence Tang     return 1;
3208a2d737cSLawrence Tang }
3218f793ac3SLawrence Tang 
3228f793ac3SLawrence Tang //Enables/disables debugging globally for the JSON validator.
3238f793ac3SLawrence Tang void validate_schema_debug_enable() { json_validator_debug = 1; }
3248f793ac3SLawrence Tang void validate_schema_debug_disable() { json_validator_debug = 0; }
3258f793ac3SLawrence Tang 
3267cd13908SLawrence Tang //Logs an error message to the given error message location and (optionally) provides debug output.
3278f793ac3SLawrence Tang void log_validator_error(char* error_message, const char* format, ...)
3288f793ac3SLawrence Tang {
3298f793ac3SLawrence Tang     va_list args;
3308f793ac3SLawrence Tang 
3318f793ac3SLawrence Tang     //Log error to error out.
3328f793ac3SLawrence Tang     va_start(args, format);
3338f793ac3SLawrence Tang     vsnprintf(error_message, JSON_ERROR_MSG_MAX_LEN, format, args);
3348f793ac3SLawrence Tang     va_end(args);
3358f793ac3SLawrence Tang 
3368f793ac3SLawrence Tang     //Debug message if necessary.
3378f793ac3SLawrence Tang     va_start(args, format);
3388f793ac3SLawrence Tang     log_validator_msg(format, args);
3398f793ac3SLawrence Tang     va_end(args);
3408f793ac3SLawrence Tang }
3418f793ac3SLawrence Tang 
3427cd13908SLawrence Tang //Logs a debug message to stdout, if validator debug is enabled.
3438f793ac3SLawrence Tang void log_validator_debug(const char* format, ...)
3448f793ac3SLawrence Tang {
3458f793ac3SLawrence Tang     va_list args;
3468f793ac3SLawrence Tang     va_start(args, format);
3478f793ac3SLawrence Tang     log_validator_msg(format, args);
3488f793ac3SLawrence Tang     va_end(args);
3498f793ac3SLawrence Tang }
3508f793ac3SLawrence Tang 
3518f793ac3SLawrence Tang //Logs a single validator debug/error message.
3528f793ac3SLawrence Tang void log_validator_msg(const char* format, va_list args)
3538f793ac3SLawrence Tang {
3548f793ac3SLawrence Tang     //Print debug output if debug is on.
3558f793ac3SLawrence Tang     if (json_validator_debug)
3568f793ac3SLawrence Tang     {
3578f793ac3SLawrence Tang         //Make new format string for error.
3588f793ac3SLawrence Tang         const char* header = "json_validator: ";
3598f793ac3SLawrence Tang         char* new_format = malloc(strlen(header) + strlen(format) + 2);
3608f793ac3SLawrence Tang         strcpy(new_format, header);
3618f793ac3SLawrence Tang         strcat(new_format, format);
3628f793ac3SLawrence Tang         strcat(new_format, "\n");
3638f793ac3SLawrence Tang 
3648f793ac3SLawrence Tang         //Print & free format.
3658f793ac3SLawrence Tang         vfprintf(stdout, new_format, args);
3668f793ac3SLawrence Tang         free(new_format);
3678f793ac3SLawrence Tang     }
3688f793ac3SLawrence Tang }