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> 145202bbb4SLawrence 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. 22e407b4c8SLawrence Tang int validate_field(const char *name, json_object *schema, json_object *object, 23e407b4c8SLawrence Tang char *error_message); 24e407b4c8SLawrence Tang int validate_integer(const char *field_name, json_object *schema, 25e407b4c8SLawrence Tang json_object *object, char *error_message); 26e407b4c8SLawrence Tang int validate_string(const char *field_name, json_object *schema, 27*b35d957eSEd Tanous json_object *object, const char *error_message); 28e407b4c8SLawrence Tang int validate_object(const char *field_name, json_object *schema, 29e407b4c8SLawrence Tang json_object *object, char *error_message); 30e407b4c8SLawrence Tang int validate_array(const char *field_name, json_object *schema, 31e407b4c8SLawrence Tang json_object *object, char *error_message); 328f793ac3SLawrence Tang void log_validator_error(char *error_message, const char *format, ...); 338f793ac3SLawrence Tang void log_validator_debug(const char *format, ...); 348f793ac3SLawrence Tang void log_validator_msg(const char *format, va_list args); 358a2d737cSLawrence Tang 368a2d737cSLawrence Tang //Validates a single JSON object against a provided schema file, returning 1 on success and 0 on failure to validate. 378a2d737cSLawrence Tang //Error message space must be allocated prior to call. 38e407b4c8SLawrence Tang int validate_schema_from_file(const char *schema_file, json_object *object, 39e407b4c8SLawrence Tang char *error_message) 408a2d737cSLawrence Tang { 418a2d737cSLawrence Tang //Load schema IR from file. 428a2d737cSLawrence Tang json_object *schema_ir = json_object_from_file(schema_file); 43e407b4c8SLawrence Tang if (schema_ir == NULL) { 44e407b4c8SLawrence Tang log_validator_error(error_message, 45e407b4c8SLawrence Tang "Failed to load schema from file '%s'.", 46e407b4c8SLawrence Tang schema_file); 478a2d737cSLawrence Tang return 0; 488a2d737cSLawrence Tang } 498a2d737cSLawrence Tang 508a2d737cSLawrence Tang //Get the directory of the file. 518a2d737cSLawrence Tang char *schema_file_copy = malloc(strlen(schema_file) + 1); 528a2d737cSLawrence Tang strcpy(schema_file_copy, schema_file); 538a2d737cSLawrence Tang char *schema_dir = dirname(schema_file_copy); 548a2d737cSLawrence Tang 55e407b4c8SLawrence Tang int result = 56e407b4c8SLawrence Tang validate_schema(schema_ir, schema_dir, object, error_message); 578a2d737cSLawrence Tang 588a2d737cSLawrence Tang //Free memory from directory call. 598a2d737cSLawrence Tang free(schema_file_copy); 60f8fc7052SJohn Chung json_object_put(schema_ir); 618a2d737cSLawrence Tang 628a2d737cSLawrence Tang return result; 638a2d737cSLawrence Tang } 648a2d737cSLawrence Tang 658a2d737cSLawrence Tang //Validates a single JSON object against a provided schema, returning 1 on success and 0 on failure to validate. 668a2d737cSLawrence Tang //Error message space must be allocated prior to call. 678a2d737cSLawrence Tang //If the schema does not include any other sub-schemas using "$ref", then leaving schema_directory as NULL is valid. 68e407b4c8SLawrence Tang int validate_schema(json_object *schema, char *schema_directory, 69e407b4c8SLawrence Tang json_object *object, char *error_message) 708a2d737cSLawrence Tang { 718a2d737cSLawrence Tang //Check that the schema version is the same as this validator. 728a2d737cSLawrence Tang json_object *schema_ver = json_object_object_get(schema, "$schema"); 73f8fc7052SJohn Chung if (schema_ver == NULL || strcmp(json_object_get_string(schema_ver), 74f8fc7052SJohn Chung JSON_SCHEMA_VERSION) != 0) { 75e407b4c8SLawrence Tang log_validator_error( 76e407b4c8SLawrence Tang error_message, 77e407b4c8SLawrence Tang "Provided schema is not of the same version that is referenced by this validator, or is not a schema."); 788a2d737cSLawrence Tang return 0; 798a2d737cSLawrence Tang } 808a2d737cSLawrence Tang 818a2d737cSLawrence Tang //Change current directory into the schema directory. 828a2d737cSLawrence Tang char *original_cwd = malloc(PATH_MAX); 83e407b4c8SLawrence Tang if (getcwd(original_cwd, PATH_MAX) == NULL) { 84e407b4c8SLawrence Tang log_validator_error(error_message, 85e407b4c8SLawrence Tang "Failed fetching the current directory."); 86f8fc7052SJohn Chung if (original_cwd) { 87f8fc7052SJohn Chung free(original_cwd); 88f8fc7052SJohn Chung } 898a2d737cSLawrence Tang return 0; 908a2d737cSLawrence Tang } 91e407b4c8SLawrence Tang if (chdir(schema_directory)) { 92e407b4c8SLawrence Tang log_validator_error(error_message, 93e407b4c8SLawrence Tang "Failed to chdir into schema directory."); 94f8fc7052SJohn Chung if (original_cwd) { 95f8fc7052SJohn Chung free(original_cwd); 96f8fc7052SJohn Chung } 978a2d737cSLawrence Tang return 0; 988a2d737cSLawrence Tang } 998a2d737cSLawrence Tang 1008a2d737cSLawrence Tang //Parse the top level structure appropriately. 1018a2d737cSLawrence Tang int result = validate_field("parent", schema, object, error_message); 1028a2d737cSLawrence Tang 1038a2d737cSLawrence Tang //Change back to original CWD. 104f8fc7052SJohn Chung if (chdir(original_cwd)) { 105f8fc7052SJohn Chung log_validator_error(error_message, 106f8fc7052SJohn Chung "Failed to chdir into original directory."); 107f8fc7052SJohn Chung } 108f8fc7052SJohn Chung 1098a2d737cSLawrence Tang free(original_cwd); 1108a2d737cSLawrence Tang 111f8fc7052SJohn Chung if (result) { 112e407b4c8SLawrence Tang log_validator_debug( 113e407b4c8SLawrence Tang "Successfully validated the provided object against schema."); 114f8fc7052SJohn Chung } 1158a2d737cSLawrence Tang return result; 1168a2d737cSLawrence Tang } 1178a2d737cSLawrence Tang 1188a2d737cSLawrence Tang //Validates a single JSON field given a schema/object. 1198f793ac3SLawrence Tang //Returns -1 on fatal/error failure, 0 on validation failure, and 1 on validation. 120e407b4c8SLawrence Tang int validate_field(const char *field_name, json_object *schema, 121e407b4c8SLawrence Tang json_object *object, char *error_message) 1228a2d737cSLawrence Tang { 1238f793ac3SLawrence Tang log_validator_debug("Validating field '%s'...", field_name); 1248f793ac3SLawrence Tang 1258a2d737cSLawrence Tang //If there is a "$ref" field, attempt to load the referenced schema. 1268a2d737cSLawrence Tang json_object *ref_schema = json_object_object_get(schema, "$ref"); 127e407b4c8SLawrence Tang if (ref_schema != NULL && 128e407b4c8SLawrence Tang json_object_get_type(ref_schema) == json_type_string) { 129e407b4c8SLawrence Tang log_validator_debug("$ref schema detected for field '%s'.", 130e407b4c8SLawrence Tang field_name); 1318f793ac3SLawrence Tang 1328a2d737cSLawrence Tang //Attempt to load. If loading fails, report error. 1338a2d737cSLawrence Tang const char *ref_path = json_object_get_string(ref_schema); 134f8fc7052SJohn Chung json_object *tmp = json_object_from_file(ref_path); 135f8fc7052SJohn Chung if (tmp == NULL) { 136e407b4c8SLawrence Tang log_validator_error( 137e407b4c8SLawrence Tang error_message, 138e407b4c8SLawrence Tang "Failed to open referenced schema file '%s'.", 139e407b4c8SLawrence Tang ref_path); 1408f793ac3SLawrence Tang return -1; 1418a2d737cSLawrence Tang } 142f8fc7052SJohn Chung json_object_put(tmp); 1438f793ac3SLawrence Tang 144e407b4c8SLawrence Tang log_validator_debug("loaded schema path '%s' for field '%s'.", 145e407b4c8SLawrence Tang ref_path, field_name); 1468a2d737cSLawrence Tang } 1478a2d737cSLawrence Tang 1488a2d737cSLawrence Tang //Get the schema field type. 149e407b4c8SLawrence Tang json_object *desired_field_type = 150e407b4c8SLawrence Tang json_object_object_get(schema, "type"); 151e407b4c8SLawrence Tang if (desired_field_type == NULL || 152e407b4c8SLawrence Tang !json_object_is_type(desired_field_type, json_type_string)) { 153e407b4c8SLawrence Tang log_validator_error( 154e407b4c8SLawrence Tang error_message, 155e407b4c8SLawrence Tang "Desired field type not provided within schema/is not a string for field '%s' (schema violation).", 156e407b4c8SLawrence Tang field_name); 1578f793ac3SLawrence Tang return -1; 1588a2d737cSLawrence Tang } 1598a2d737cSLawrence Tang 1608a2d737cSLawrence Tang //Check the field types are actually equal. 161e407b4c8SLawrence Tang const char *desired_field_type_str = 162e407b4c8SLawrence Tang json_object_get_string(desired_field_type); 163e407b4c8SLawrence Tang if (!((!strcmp(desired_field_type_str, "object") && 164e407b4c8SLawrence Tang json_object_is_type(object, json_type_object)) || 165e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "array") && 166e407b4c8SLawrence Tang json_object_is_type(object, json_type_array)) || 167e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "integer") && 168e407b4c8SLawrence Tang json_object_is_type(object, json_type_int)) || 169e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "string") && 170e407b4c8SLawrence Tang json_object_is_type(object, json_type_string)) || 171e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "boolean") && 172e407b4c8SLawrence Tang json_object_is_type(object, json_type_boolean)) || 173e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "double") && 174e407b4c8SLawrence Tang json_object_is_type(object, json_type_double)))) { 175e407b4c8SLawrence Tang log_validator_error(error_message, 176e407b4c8SLawrence Tang "Field type match failed for field '%s'.", 177e407b4c8SLawrence Tang field_name); 1788a2d737cSLawrence Tang return 0; 1798a2d737cSLawrence Tang } 1808a2d737cSLawrence Tang 1818f793ac3SLawrence Tang //If the schema contains a "oneOf" array, we need to validate the field against each of the 1828f793ac3SLawrence Tang //possible options in turn. 1838f793ac3SLawrence Tang json_object *one_of = json_object_object_get(schema, "oneOf"); 184e407b4c8SLawrence Tang if (one_of != NULL && json_object_get_type(one_of) == json_type_array) { 185e407b4c8SLawrence Tang log_validator_debug("oneOf options detected for field '%s'.", 186e407b4c8SLawrence Tang field_name); 1878f793ac3SLawrence Tang 1888f793ac3SLawrence Tang int len = json_object_array_length(one_of); 1898f793ac3SLawrence Tang int validated = 0; 190e407b4c8SLawrence Tang for (int i = 0; i < len; i++) { 1918f793ac3SLawrence Tang //If the "oneOf" member isn't an object, warn on schema violation. 192e407b4c8SLawrence Tang json_object *one_of_option = 193e407b4c8SLawrence Tang json_object_array_get_idx(one_of, i); 194e407b4c8SLawrence Tang if (one_of_option == NULL || 195e407b4c8SLawrence Tang json_object_get_type(one_of_option) != 196e407b4c8SLawrence Tang json_type_object) { 197e407b4c8SLawrence Tang log_validator_debug( 198e407b4c8SLawrence Tang "Schema Warning: 'oneOf' member for field '%s' is not an object, schema violation.", 199e407b4c8SLawrence Tang field_name); 2008f793ac3SLawrence Tang continue; 2018f793ac3SLawrence Tang } 2028f793ac3SLawrence Tang 2038f793ac3SLawrence Tang //Validate field with schema. 204e407b4c8SLawrence Tang validated = validate_field(field_name, one_of_option, 205e407b4c8SLawrence Tang object, error_message); 206f8fc7052SJohn Chung if (validated == -1) { 2078f793ac3SLawrence Tang return -1; 208f8fc7052SJohn Chung } 209f8fc7052SJohn Chung if (validated) { 2108f793ac3SLawrence Tang break; 2118f793ac3SLawrence Tang } 212f8fc7052SJohn Chung } 2138f793ac3SLawrence Tang 2148f793ac3SLawrence Tang //Return if failed all checks. 215e407b4c8SLawrence Tang if (!validated) { 216e407b4c8SLawrence Tang log_validator_error( 217e407b4c8SLawrence Tang error_message, 218e407b4c8SLawrence Tang "No schema object structures matched provided object for field '%s'.", 219e407b4c8SLawrence Tang field_name); 2208f793ac3SLawrence Tang return 0; 2218f793ac3SLawrence Tang } 2228f793ac3SLawrence Tang } 22345e04b0aSLawrence Tang 2248a2d737cSLawrence Tang //Switch and validate each type in turn. 225e407b4c8SLawrence Tang switch (json_object_get_type(object)) { 2268a2d737cSLawrence Tang case json_type_int: 227e407b4c8SLawrence Tang return validate_integer(field_name, schema, object, 228e407b4c8SLawrence Tang error_message); 2298a2d737cSLawrence Tang case json_type_string: 230e407b4c8SLawrence Tang return validate_string(field_name, schema, object, 231e407b4c8SLawrence Tang error_message); 2328a2d737cSLawrence Tang case json_type_object: 233e407b4c8SLawrence Tang return validate_object(field_name, schema, object, 234e407b4c8SLawrence Tang error_message); 2358a2d737cSLawrence Tang case json_type_array: 236e407b4c8SLawrence Tang return validate_array(field_name, schema, object, 237e407b4c8SLawrence Tang error_message); 2388a2d737cSLawrence Tang 2398a2d737cSLawrence Tang //We don't perform extra validation on this type. 2408a2d737cSLawrence Tang default: 241e407b4c8SLawrence Tang log_validator_debug( 242e407b4c8SLawrence Tang "validation passed for '%s' (no extra validation).", 243e407b4c8SLawrence Tang field_name); 2448a2d737cSLawrence Tang return 1; 2458a2d737cSLawrence Tang } 2468a2d737cSLawrence Tang } 2478a2d737cSLawrence Tang 2488a2d737cSLawrence Tang //Validates a single integer value according to the given specification. 249e407b4c8SLawrence Tang int validate_integer(const char *field_name, json_object *schema, 250e407b4c8SLawrence Tang json_object *object, char *error_message) 2518a2d737cSLawrence Tang { 2528a2d737cSLawrence Tang //Is there a minimum/maximum specified? If so, check those. 2538a2d737cSLawrence Tang //Validate minimum. 2548a2d737cSLawrence Tang json_object *min_value = json_object_object_get(schema, "minimum"); 255e407b4c8SLawrence Tang if (min_value != NULL && 256e407b4c8SLawrence Tang json_object_is_type(min_value, json_type_int)) { 2578a2d737cSLawrence Tang int min_value_int = json_object_get_int(min_value); 258f8fc7052SJohn Chung if (json_object_get_uint64(object) < (uint64_t)min_value_int) { 259e407b4c8SLawrence Tang log_validator_error( 260e407b4c8SLawrence Tang error_message, 261e407b4c8SLawrence Tang "Failed to validate integer field '%s'. Value was below minimum of %d.", 262e407b4c8SLawrence Tang field_name, min_value_int); 2638a2d737cSLawrence Tang return 0; 2648a2d737cSLawrence Tang } 2658a2d737cSLawrence Tang } 2668a2d737cSLawrence Tang 2678a2d737cSLawrence Tang //Validate maximum. 2688a2d737cSLawrence Tang json_object *max_value = json_object_object_get(schema, "maximum"); 269e407b4c8SLawrence Tang if (max_value != NULL && 270e407b4c8SLawrence Tang json_object_is_type(max_value, json_type_int)) { 2718a2d737cSLawrence Tang int max_value_int = json_object_get_int(max_value); 272f8fc7052SJohn Chung if (json_object_get_uint64(object) > (uint64_t)max_value_int) { 273e407b4c8SLawrence Tang log_validator_error( 274e407b4c8SLawrence Tang error_message, 275e407b4c8SLawrence Tang "Failed to validate integer field '%s'. Value was above maximum of %d.", 276e407b4c8SLawrence Tang field_name, max_value_int); 2778a2d737cSLawrence Tang return 0; 2788a2d737cSLawrence Tang } 2798a2d737cSLawrence Tang } 2808a2d737cSLawrence Tang 2818a2d737cSLawrence Tang return 1; 2828a2d737cSLawrence Tang } 2838a2d737cSLawrence Tang 2848a2d737cSLawrence Tang //Validates a single string value according to the given specification. 285e407b4c8SLawrence Tang int validate_string(const char *field_name, json_object *schema, 286*b35d957eSEd Tanous json_object *object, const char *error_message) 2878a2d737cSLawrence Tang { 2888a2d737cSLawrence Tang //todo: if there is a "pattern" field, verify the string with RegEx. 289f8fc7052SJohn Chung (void)field_name; 290f8fc7052SJohn Chung (void)schema; 291f8fc7052SJohn Chung (void)object; 292f8fc7052SJohn Chung (void)error_message; 293f8fc7052SJohn Chung 2948a2d737cSLawrence Tang return 1; 2958a2d737cSLawrence Tang } 2968a2d737cSLawrence Tang 2978a2d737cSLawrence Tang //Validates a single object value according to the given specification. 298e407b4c8SLawrence Tang int validate_object(const char *field_name, json_object *schema, 299e407b4c8SLawrence Tang json_object *object, char *error_message) 3008a2d737cSLawrence Tang { 3018a2d737cSLawrence Tang //Are there a set of "required" fields? If so, check they all exist. 302e407b4c8SLawrence Tang json_object *required_fields = 303e407b4c8SLawrence Tang json_object_object_get(schema, "required"); 304e407b4c8SLawrence Tang if (required_fields != NULL && 305e407b4c8SLawrence Tang json_object_get_type(required_fields) == json_type_array) { 306e407b4c8SLawrence Tang log_validator_debug( 307e407b4c8SLawrence Tang "Required fields found for '%s', matching...", 308e407b4c8SLawrence Tang field_name); 3098f793ac3SLawrence Tang 3108a2d737cSLawrence Tang int len = json_object_array_length(required_fields); 311e407b4c8SLawrence Tang for (int i = 0; i < len; i++) { 3128a2d737cSLawrence Tang //Get the required field from schema. 313e407b4c8SLawrence Tang json_object *required_field = 314e407b4c8SLawrence Tang json_object_array_get_idx(required_fields, i); 315e407b4c8SLawrence Tang if (json_object_get_type(required_field) != 316e407b4c8SLawrence Tang json_type_string) { 317e407b4c8SLawrence Tang log_validator_error( 318e407b4c8SLawrence Tang error_message, 319e407b4c8SLawrence Tang "Required field for object '%s' is not a string (schema violation).", 320e407b4c8SLawrence Tang field_name); 3218a2d737cSLawrence Tang return 0; 3228a2d737cSLawrence Tang } 3238a2d737cSLawrence Tang 3248a2d737cSLawrence Tang //Does it exist in the object? 325e407b4c8SLawrence Tang const char *required_field_str = 326e407b4c8SLawrence Tang json_object_get_string(required_field); 327e407b4c8SLawrence Tang if (json_object_object_get( 328e407b4c8SLawrence Tang object, required_field_str) == NULL) { 329e407b4c8SLawrence Tang log_validator_error( 330e407b4c8SLawrence Tang error_message, 331e407b4c8SLawrence Tang "Required field '%s' was not present in object '%s'.", 332e407b4c8SLawrence Tang required_field_str, field_name); 3338a2d737cSLawrence Tang return 0; 3348a2d737cSLawrence Tang } 3358a2d737cSLawrence Tang } 3368a2d737cSLawrence Tang } 3378a2d737cSLawrence Tang 338c4814594SLawrence Tang //Get additional properties value in advance. 339e407b4c8SLawrence Tang json_object *additional_properties = 340e407b4c8SLawrence Tang json_object_object_get(schema, "additionalProperties"); 341c4814594SLawrence Tang int additional_properties_allowed = 0; 342e407b4c8SLawrence Tang if (additional_properties != NULL && 343f8fc7052SJohn Chung json_object_get_type(additional_properties) == json_type_boolean) { 344e407b4c8SLawrence Tang additional_properties_allowed = 345e407b4c8SLawrence Tang json_object_get_boolean(additional_properties); 346f8fc7052SJohn Chung } 3478a2d737cSLawrence Tang 3488a2d737cSLawrence Tang //Run through the "properties" object and validate each of those in turn. 3498a2d737cSLawrence Tang json_object *properties = json_object_object_get(schema, "properties"); 350e407b4c8SLawrence Tang if (properties != NULL && 351e407b4c8SLawrence Tang json_object_get_type(properties) == json_type_object) { 352e407b4c8SLawrence Tang json_object_object_foreach(properties, key, value) 3538a2d737cSLawrence Tang { 3548a2d737cSLawrence Tang //If the given property name does not exist on the target object, ignore and continue next. 355e407b4c8SLawrence Tang json_object *object_prop = 356e407b4c8SLawrence Tang json_object_object_get(object, key); 357f8fc7052SJohn Chung if (object_prop == NULL) { 3588a2d737cSLawrence Tang continue; 359f8fc7052SJohn Chung } 3608a2d737cSLawrence Tang 3618a2d737cSLawrence Tang //Validate against the schema. 362e407b4c8SLawrence Tang if (!validate_field(key, value, object_prop, 363f8fc7052SJohn Chung error_message)) { 3648a2d737cSLawrence Tang return 0; 3658a2d737cSLawrence Tang } 366f8fc7052SJohn Chung } 367c4814594SLawrence Tang 368c4814594SLawrence Tang //If additional properties are banned, validate that no additional properties exist. 369e407b4c8SLawrence Tang if (!additional_properties_allowed) { 370e407b4c8SLawrence Tang json_object_object_foreach(object, key, value) 371c4814594SLawrence Tang { 372f8fc7052SJohn Chung //Avoid compiler warning 373f8fc7052SJohn Chung (void)value; 374f8fc7052SJohn Chung 375c4814594SLawrence Tang //If the given property name does not exist on the schema object, fail validation. 376f8fc7052SJohn Chung const json_object *schema_prop = 377e407b4c8SLawrence Tang json_object_object_get(properties, key); 378e407b4c8SLawrence Tang if (schema_prop == NULL) { 379e407b4c8SLawrence Tang log_validator_error( 380e407b4c8SLawrence Tang error_message, 381e407b4c8SLawrence Tang "Invalid additional property '%s' detected on field '%s'.", 382e407b4c8SLawrence Tang key, field_name); 383c4814594SLawrence Tang return 0; 384c4814594SLawrence Tang } 385c4814594SLawrence Tang } 386c4814594SLawrence Tang } 3878a2d737cSLawrence Tang } 3888a2d737cSLawrence Tang 3898a2d737cSLawrence Tang return 1; 3908a2d737cSLawrence Tang } 3918a2d737cSLawrence Tang 3928a2d737cSLawrence Tang //Validates a single array value according to the given specification. 393e407b4c8SLawrence Tang int validate_array(const char *field_name, json_object *schema, 394e407b4c8SLawrence Tang json_object *object, char *error_message) 3958a2d737cSLawrence Tang { 39645e04b0aSLawrence Tang //Iterate all items in the array, and validate according to the "items" schema. 39745e04b0aSLawrence Tang json_object *items_schema = json_object_object_get(schema, "items"); 398e407b4c8SLawrence Tang if (items_schema != NULL && 399e407b4c8SLawrence Tang json_object_get_type(items_schema) == json_type_object) { 40045e04b0aSLawrence Tang int array_len = json_object_array_length(object); 401e407b4c8SLawrence Tang for (int i = 0; i < array_len; i++) { 402e407b4c8SLawrence Tang if (!validate_field(field_name, items_schema, 403e407b4c8SLawrence Tang json_object_array_get_idx(object, 404e407b4c8SLawrence Tang i), 405f8fc7052SJohn Chung error_message)) { 40645e04b0aSLawrence Tang return 0; 40745e04b0aSLawrence Tang } 40845e04b0aSLawrence Tang } 409f8fc7052SJohn Chung } 41045e04b0aSLawrence Tang 4118a2d737cSLawrence Tang return 1; 4128a2d737cSLawrence Tang } 4138f793ac3SLawrence Tang 4148f793ac3SLawrence Tang //Enables/disables debugging globally for the JSON validator. 415e407b4c8SLawrence Tang void validate_schema_debug_enable() 416e407b4c8SLawrence Tang { 417e407b4c8SLawrence Tang json_validator_debug = 1; 418e407b4c8SLawrence Tang } 419e407b4c8SLawrence Tang void validate_schema_debug_disable() 420e407b4c8SLawrence Tang { 421e407b4c8SLawrence Tang json_validator_debug = 0; 422e407b4c8SLawrence Tang } 4238f793ac3SLawrence Tang 4247cd13908SLawrence Tang //Logs an error message to the given error message location and (optionally) provides debug output. 4258f793ac3SLawrence Tang void log_validator_error(char *error_message, const char *format, ...) 4268f793ac3SLawrence Tang { 4278f793ac3SLawrence Tang va_list args; 4288f793ac3SLawrence Tang 4298f793ac3SLawrence Tang //Log error to error out. 4308f793ac3SLawrence Tang va_start(args, format); 4318f793ac3SLawrence Tang vsnprintf(error_message, JSON_ERROR_MSG_MAX_LEN, format, args); 4328f793ac3SLawrence Tang va_end(args); 4338f793ac3SLawrence Tang 4348f793ac3SLawrence Tang //Debug message if necessary. 4358f793ac3SLawrence Tang va_start(args, format); 4368f793ac3SLawrence Tang log_validator_msg(format, args); 4378f793ac3SLawrence Tang va_end(args); 4388f793ac3SLawrence Tang } 4398f793ac3SLawrence Tang 4407cd13908SLawrence Tang //Logs a debug message to stdout, if validator debug is enabled. 4418f793ac3SLawrence Tang void log_validator_debug(const char *format, ...) 4428f793ac3SLawrence Tang { 4438f793ac3SLawrence Tang va_list args; 4448f793ac3SLawrence Tang va_start(args, format); 4458f793ac3SLawrence Tang log_validator_msg(format, args); 4468f793ac3SLawrence Tang va_end(args); 4478f793ac3SLawrence Tang } 4488f793ac3SLawrence Tang 4498f793ac3SLawrence Tang //Logs a single validator debug/error message. 4508f793ac3SLawrence Tang void log_validator_msg(const char *format, va_list args) 4518f793ac3SLawrence Tang { 4528f793ac3SLawrence Tang //Print debug output if debug is on. 453e407b4c8SLawrence Tang if (json_validator_debug) { 4548f793ac3SLawrence Tang //Make new format string for error. 4558f793ac3SLawrence Tang const char *header = "json_validator: "; 4568f793ac3SLawrence Tang char *new_format = malloc(strlen(header) + strlen(format) + 2); 4578f793ac3SLawrence Tang strcpy(new_format, header); 4588f793ac3SLawrence Tang strcat(new_format, format); 4598f793ac3SLawrence Tang strcat(new_format, "\n"); 4608f793ac3SLawrence Tang 4618f793ac3SLawrence Tang //Print & free format. 4628f793ac3SLawrence Tang vfprintf(stdout, new_format, args); 4638f793ac3SLawrence Tang free(new_format); 4648f793ac3SLawrence Tang } 4658f793ac3SLawrence Tang } 466