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" 17*f8fc7052SJohn Chung #include <linux/limits.h> 188a2d737cSLawrence Tang 198f793ac3SLawrence Tang //Field definitions. 208f793ac3SLawrence Tang int json_validator_debug = 0; 218f793ac3SLawrence Tang 228a2d737cSLawrence Tang //Private pre-definitions. 23e407b4c8SLawrence Tang int validate_field(const char *name, json_object *schema, json_object *object, 24e407b4c8SLawrence Tang char *error_message); 25e407b4c8SLawrence Tang int validate_integer(const char *field_name, json_object *schema, 26e407b4c8SLawrence Tang json_object *object, char *error_message); 27e407b4c8SLawrence Tang int validate_string(const char *field_name, json_object *schema, 28e407b4c8SLawrence Tang json_object *object, char *error_message); 29e407b4c8SLawrence Tang int validate_object(const char *field_name, json_object *schema, 30e407b4c8SLawrence Tang json_object *object, char *error_message); 31e407b4c8SLawrence Tang int validate_array(const char *field_name, json_object *schema, 32e407b4c8SLawrence Tang json_object *object, char *error_message); 338f793ac3SLawrence Tang void log_validator_error(char *error_message, const char *format, ...); 348f793ac3SLawrence Tang void log_validator_debug(const char *format, ...); 358f793ac3SLawrence Tang void log_validator_msg(const char *format, va_list args); 368a2d737cSLawrence Tang 378a2d737cSLawrence Tang //Validates a single JSON object against a provided schema file, returning 1 on success and 0 on failure to validate. 388a2d737cSLawrence Tang //Error message space must be allocated prior to call. 39e407b4c8SLawrence Tang int validate_schema_from_file(const char *schema_file, json_object *object, 40e407b4c8SLawrence Tang char *error_message) 418a2d737cSLawrence Tang { 428a2d737cSLawrence Tang //Load schema IR from file. 438a2d737cSLawrence Tang json_object *schema_ir = json_object_from_file(schema_file); 44e407b4c8SLawrence Tang if (schema_ir == NULL) { 45e407b4c8SLawrence Tang log_validator_error(error_message, 46e407b4c8SLawrence Tang "Failed to load schema from file '%s'.", 47e407b4c8SLawrence Tang schema_file); 488a2d737cSLawrence Tang return 0; 498a2d737cSLawrence Tang } 508a2d737cSLawrence Tang 518a2d737cSLawrence Tang //Get the directory of the file. 528a2d737cSLawrence Tang char *schema_file_copy = malloc(strlen(schema_file) + 1); 538a2d737cSLawrence Tang strcpy(schema_file_copy, schema_file); 548a2d737cSLawrence Tang char *schema_dir = dirname(schema_file_copy); 558a2d737cSLawrence Tang 56e407b4c8SLawrence Tang int result = 57e407b4c8SLawrence Tang validate_schema(schema_ir, schema_dir, object, error_message); 588a2d737cSLawrence Tang 598a2d737cSLawrence Tang //Free memory from directory call. 608a2d737cSLawrence Tang free(schema_file_copy); 61*f8fc7052SJohn Chung json_object_put(schema_ir); 628a2d737cSLawrence Tang 638a2d737cSLawrence Tang return result; 648a2d737cSLawrence Tang } 658a2d737cSLawrence Tang 668a2d737cSLawrence Tang //Validates a single JSON object against a provided schema, returning 1 on success and 0 on failure to validate. 678a2d737cSLawrence Tang //Error message space must be allocated prior to call. 688a2d737cSLawrence Tang //If the schema does not include any other sub-schemas using "$ref", then leaving schema_directory as NULL is valid. 69e407b4c8SLawrence Tang int validate_schema(json_object *schema, char *schema_directory, 70e407b4c8SLawrence Tang json_object *object, char *error_message) 718a2d737cSLawrence Tang { 728a2d737cSLawrence Tang //Check that the schema version is the same as this validator. 738a2d737cSLawrence Tang json_object *schema_ver = json_object_object_get(schema, "$schema"); 74*f8fc7052SJohn Chung if (schema_ver == NULL || strcmp(json_object_get_string(schema_ver), 75*f8fc7052SJohn Chung JSON_SCHEMA_VERSION) != 0) { 76e407b4c8SLawrence Tang log_validator_error( 77e407b4c8SLawrence Tang error_message, 78e407b4c8SLawrence Tang "Provided schema is not of the same version that is referenced by this validator, or is not a schema."); 798a2d737cSLawrence Tang return 0; 808a2d737cSLawrence Tang } 818a2d737cSLawrence Tang 828a2d737cSLawrence Tang //Change current directory into the schema directory. 838a2d737cSLawrence Tang char *original_cwd = malloc(PATH_MAX); 84e407b4c8SLawrence Tang if (getcwd(original_cwd, PATH_MAX) == NULL) { 85e407b4c8SLawrence Tang log_validator_error(error_message, 86e407b4c8SLawrence Tang "Failed fetching the current directory."); 87*f8fc7052SJohn Chung if (original_cwd) { 88*f8fc7052SJohn Chung free(original_cwd); 89*f8fc7052SJohn Chung } 908a2d737cSLawrence Tang return 0; 918a2d737cSLawrence Tang } 92e407b4c8SLawrence Tang if (chdir(schema_directory)) { 93e407b4c8SLawrence Tang log_validator_error(error_message, 94e407b4c8SLawrence Tang "Failed to chdir into schema directory."); 95*f8fc7052SJohn Chung if (original_cwd) { 96*f8fc7052SJohn Chung free(original_cwd); 97*f8fc7052SJohn Chung } 988a2d737cSLawrence Tang return 0; 998a2d737cSLawrence Tang } 1008a2d737cSLawrence Tang 1018a2d737cSLawrence Tang //Parse the top level structure appropriately. 1028a2d737cSLawrence Tang int result = validate_field("parent", schema, object, error_message); 1038a2d737cSLawrence Tang 1048a2d737cSLawrence Tang //Change back to original CWD. 105*f8fc7052SJohn Chung if (chdir(original_cwd)) { 106*f8fc7052SJohn Chung log_validator_error(error_message, 107*f8fc7052SJohn Chung "Failed to chdir into original directory."); 108*f8fc7052SJohn Chung } 109*f8fc7052SJohn Chung 1108a2d737cSLawrence Tang free(original_cwd); 1118a2d737cSLawrence Tang 112*f8fc7052SJohn Chung if (result) { 113e407b4c8SLawrence Tang log_validator_debug( 114e407b4c8SLawrence Tang "Successfully validated the provided object against schema."); 115*f8fc7052SJohn Chung } 1168a2d737cSLawrence Tang return result; 1178a2d737cSLawrence Tang } 1188a2d737cSLawrence Tang 1198a2d737cSLawrence Tang //Validates a single JSON field given a schema/object. 1208f793ac3SLawrence Tang //Returns -1 on fatal/error failure, 0 on validation failure, and 1 on validation. 121e407b4c8SLawrence Tang int validate_field(const char *field_name, json_object *schema, 122e407b4c8SLawrence Tang json_object *object, char *error_message) 1238a2d737cSLawrence Tang { 1248f793ac3SLawrence Tang log_validator_debug("Validating field '%s'...", field_name); 1258f793ac3SLawrence Tang 1268a2d737cSLawrence Tang //If there is a "$ref" field, attempt to load the referenced schema. 1278a2d737cSLawrence Tang json_object *ref_schema = json_object_object_get(schema, "$ref"); 128e407b4c8SLawrence Tang if (ref_schema != NULL && 129e407b4c8SLawrence Tang json_object_get_type(ref_schema) == json_type_string) { 130e407b4c8SLawrence Tang log_validator_debug("$ref schema detected for field '%s'.", 131e407b4c8SLawrence Tang field_name); 1328f793ac3SLawrence Tang 1338a2d737cSLawrence Tang //Attempt to load. If loading fails, report error. 1348a2d737cSLawrence Tang const char *ref_path = json_object_get_string(ref_schema); 135*f8fc7052SJohn Chung json_object *tmp = json_object_from_file(ref_path); 136*f8fc7052SJohn Chung if (tmp == NULL) { 137e407b4c8SLawrence Tang log_validator_error( 138e407b4c8SLawrence Tang error_message, 139e407b4c8SLawrence Tang "Failed to open referenced schema file '%s'.", 140e407b4c8SLawrence Tang ref_path); 1418f793ac3SLawrence Tang return -1; 1428a2d737cSLawrence Tang } 143*f8fc7052SJohn Chung json_object_put(tmp); 1448f793ac3SLawrence Tang 145e407b4c8SLawrence Tang log_validator_debug("loaded schema path '%s' for field '%s'.", 146e407b4c8SLawrence Tang ref_path, field_name); 1478a2d737cSLawrence Tang } 1488a2d737cSLawrence Tang 1498a2d737cSLawrence Tang //Get the schema field type. 150e407b4c8SLawrence Tang json_object *desired_field_type = 151e407b4c8SLawrence Tang json_object_object_get(schema, "type"); 152e407b4c8SLawrence Tang if (desired_field_type == NULL || 153e407b4c8SLawrence Tang !json_object_is_type(desired_field_type, json_type_string)) { 154e407b4c8SLawrence Tang log_validator_error( 155e407b4c8SLawrence Tang error_message, 156e407b4c8SLawrence Tang "Desired field type not provided within schema/is not a string for field '%s' (schema violation).", 157e407b4c8SLawrence Tang field_name); 1588f793ac3SLawrence Tang return -1; 1598a2d737cSLawrence Tang } 1608a2d737cSLawrence Tang 1618a2d737cSLawrence Tang //Check the field types are actually equal. 162e407b4c8SLawrence Tang const char *desired_field_type_str = 163e407b4c8SLawrence Tang json_object_get_string(desired_field_type); 164e407b4c8SLawrence Tang if (!((!strcmp(desired_field_type_str, "object") && 165e407b4c8SLawrence Tang json_object_is_type(object, json_type_object)) || 166e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "array") && 167e407b4c8SLawrence Tang json_object_is_type(object, json_type_array)) || 168e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "integer") && 169e407b4c8SLawrence Tang json_object_is_type(object, json_type_int)) || 170e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "string") && 171e407b4c8SLawrence Tang json_object_is_type(object, json_type_string)) || 172e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "boolean") && 173e407b4c8SLawrence Tang json_object_is_type(object, json_type_boolean)) || 174e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "double") && 175e407b4c8SLawrence Tang json_object_is_type(object, json_type_double)))) { 176e407b4c8SLawrence Tang log_validator_error(error_message, 177e407b4c8SLawrence Tang "Field type match failed for field '%s'.", 178e407b4c8SLawrence Tang field_name); 1798a2d737cSLawrence Tang return 0; 1808a2d737cSLawrence Tang } 1818a2d737cSLawrence Tang 1828f793ac3SLawrence Tang //If the schema contains a "oneOf" array, we need to validate the field against each of the 1838f793ac3SLawrence Tang //possible options in turn. 1848f793ac3SLawrence Tang json_object *one_of = json_object_object_get(schema, "oneOf"); 185e407b4c8SLawrence Tang if (one_of != NULL && json_object_get_type(one_of) == json_type_array) { 186e407b4c8SLawrence Tang log_validator_debug("oneOf options detected for field '%s'.", 187e407b4c8SLawrence Tang field_name); 1888f793ac3SLawrence Tang 1898f793ac3SLawrence Tang int len = json_object_array_length(one_of); 1908f793ac3SLawrence Tang int validated = 0; 191e407b4c8SLawrence Tang for (int i = 0; i < len; i++) { 1928f793ac3SLawrence Tang //If the "oneOf" member isn't an object, warn on schema violation. 193e407b4c8SLawrence Tang json_object *one_of_option = 194e407b4c8SLawrence Tang json_object_array_get_idx(one_of, i); 195e407b4c8SLawrence Tang if (one_of_option == NULL || 196e407b4c8SLawrence Tang json_object_get_type(one_of_option) != 197e407b4c8SLawrence Tang json_type_object) { 198e407b4c8SLawrence Tang log_validator_debug( 199e407b4c8SLawrence Tang "Schema Warning: 'oneOf' member for field '%s' is not an object, schema violation.", 200e407b4c8SLawrence Tang field_name); 2018f793ac3SLawrence Tang continue; 2028f793ac3SLawrence Tang } 2038f793ac3SLawrence Tang 2048f793ac3SLawrence Tang //Validate field with schema. 205e407b4c8SLawrence Tang validated = validate_field(field_name, one_of_option, 206e407b4c8SLawrence Tang object, error_message); 207*f8fc7052SJohn Chung if (validated == -1) { 2088f793ac3SLawrence Tang return -1; 209*f8fc7052SJohn Chung } 210*f8fc7052SJohn Chung if (validated) { 2118f793ac3SLawrence Tang break; 2128f793ac3SLawrence Tang } 213*f8fc7052SJohn Chung } 2148f793ac3SLawrence Tang 2158f793ac3SLawrence Tang //Return if failed all checks. 216e407b4c8SLawrence Tang if (!validated) { 217e407b4c8SLawrence Tang log_validator_error( 218e407b4c8SLawrence Tang error_message, 219e407b4c8SLawrence Tang "No schema object structures matched provided object for field '%s'.", 220e407b4c8SLawrence Tang field_name); 2218f793ac3SLawrence Tang return 0; 2228f793ac3SLawrence Tang } 2238f793ac3SLawrence Tang } 22445e04b0aSLawrence Tang 2258a2d737cSLawrence Tang //Switch and validate each type in turn. 226e407b4c8SLawrence Tang switch (json_object_get_type(object)) { 2278a2d737cSLawrence Tang case json_type_int: 228e407b4c8SLawrence Tang return validate_integer(field_name, schema, object, 229e407b4c8SLawrence Tang error_message); 2308a2d737cSLawrence Tang case json_type_string: 231e407b4c8SLawrence Tang return validate_string(field_name, schema, object, 232e407b4c8SLawrence Tang error_message); 2338a2d737cSLawrence Tang case json_type_object: 234e407b4c8SLawrence Tang return validate_object(field_name, schema, object, 235e407b4c8SLawrence Tang error_message); 2368a2d737cSLawrence Tang case json_type_array: 237e407b4c8SLawrence Tang return validate_array(field_name, schema, object, 238e407b4c8SLawrence Tang error_message); 2398a2d737cSLawrence Tang 2408a2d737cSLawrence Tang //We don't perform extra validation on this type. 2418a2d737cSLawrence Tang default: 242e407b4c8SLawrence Tang log_validator_debug( 243e407b4c8SLawrence Tang "validation passed for '%s' (no extra validation).", 244e407b4c8SLawrence Tang field_name); 2458a2d737cSLawrence Tang return 1; 2468a2d737cSLawrence Tang } 2478a2d737cSLawrence Tang } 2488a2d737cSLawrence Tang 2498a2d737cSLawrence Tang //Validates a single integer value according to the given specification. 250e407b4c8SLawrence Tang int validate_integer(const char *field_name, json_object *schema, 251e407b4c8SLawrence Tang json_object *object, char *error_message) 2528a2d737cSLawrence Tang { 2538a2d737cSLawrence Tang //Is there a minimum/maximum specified? If so, check those. 2548a2d737cSLawrence Tang //Validate minimum. 2558a2d737cSLawrence Tang json_object *min_value = json_object_object_get(schema, "minimum"); 256e407b4c8SLawrence Tang if (min_value != NULL && 257e407b4c8SLawrence Tang json_object_is_type(min_value, json_type_int)) { 2588a2d737cSLawrence Tang int min_value_int = json_object_get_int(min_value); 259*f8fc7052SJohn Chung if (json_object_get_uint64(object) < (uint64_t)min_value_int) { 260e407b4c8SLawrence Tang log_validator_error( 261e407b4c8SLawrence Tang error_message, 262e407b4c8SLawrence Tang "Failed to validate integer field '%s'. Value was below minimum of %d.", 263e407b4c8SLawrence Tang field_name, min_value_int); 2648a2d737cSLawrence Tang return 0; 2658a2d737cSLawrence Tang } 2668a2d737cSLawrence Tang } 2678a2d737cSLawrence Tang 2688a2d737cSLawrence Tang //Validate maximum. 2698a2d737cSLawrence Tang json_object *max_value = json_object_object_get(schema, "maximum"); 270e407b4c8SLawrence Tang if (max_value != NULL && 271e407b4c8SLawrence Tang json_object_is_type(max_value, json_type_int)) { 2728a2d737cSLawrence Tang int max_value_int = json_object_get_int(max_value); 273*f8fc7052SJohn Chung if (json_object_get_uint64(object) > (uint64_t)max_value_int) { 274e407b4c8SLawrence Tang log_validator_error( 275e407b4c8SLawrence Tang error_message, 276e407b4c8SLawrence Tang "Failed to validate integer field '%s'. Value was above maximum of %d.", 277e407b4c8SLawrence Tang field_name, max_value_int); 2788a2d737cSLawrence Tang return 0; 2798a2d737cSLawrence Tang } 2808a2d737cSLawrence Tang } 2818a2d737cSLawrence Tang 2828a2d737cSLawrence Tang return 1; 2838a2d737cSLawrence Tang } 2848a2d737cSLawrence Tang 2858a2d737cSLawrence Tang //Validates a single string value according to the given specification. 286e407b4c8SLawrence Tang int validate_string(const char *field_name, json_object *schema, 287e407b4c8SLawrence Tang json_object *object, char *error_message) 2888a2d737cSLawrence Tang { 2898a2d737cSLawrence Tang //todo: if there is a "pattern" field, verify the string with RegEx. 290*f8fc7052SJohn Chung (void)field_name; 291*f8fc7052SJohn Chung (void)schema; 292*f8fc7052SJohn Chung (void)object; 293*f8fc7052SJohn Chung (void)error_message; 294*f8fc7052SJohn Chung 2958a2d737cSLawrence Tang return 1; 2968a2d737cSLawrence Tang } 2978a2d737cSLawrence Tang 2988a2d737cSLawrence Tang //Validates a single object value according to the given specification. 299e407b4c8SLawrence Tang int validate_object(const char *field_name, json_object *schema, 300e407b4c8SLawrence Tang json_object *object, char *error_message) 3018a2d737cSLawrence Tang { 3028a2d737cSLawrence Tang //Are there a set of "required" fields? If so, check they all exist. 303e407b4c8SLawrence Tang json_object *required_fields = 304e407b4c8SLawrence Tang json_object_object_get(schema, "required"); 305e407b4c8SLawrence Tang if (required_fields != NULL && 306e407b4c8SLawrence Tang json_object_get_type(required_fields) == json_type_array) { 307e407b4c8SLawrence Tang log_validator_debug( 308e407b4c8SLawrence Tang "Required fields found for '%s', matching...", 309e407b4c8SLawrence Tang field_name); 3108f793ac3SLawrence Tang 3118a2d737cSLawrence Tang int len = json_object_array_length(required_fields); 312e407b4c8SLawrence Tang for (int i = 0; i < len; i++) { 3138a2d737cSLawrence Tang //Get the required field from schema. 314e407b4c8SLawrence Tang json_object *required_field = 315e407b4c8SLawrence Tang json_object_array_get_idx(required_fields, i); 316e407b4c8SLawrence Tang if (json_object_get_type(required_field) != 317e407b4c8SLawrence Tang json_type_string) { 318e407b4c8SLawrence Tang log_validator_error( 319e407b4c8SLawrence Tang error_message, 320e407b4c8SLawrence Tang "Required field for object '%s' is not a string (schema violation).", 321e407b4c8SLawrence Tang field_name); 3228a2d737cSLawrence Tang return 0; 3238a2d737cSLawrence Tang } 3248a2d737cSLawrence Tang 3258a2d737cSLawrence Tang //Does it exist in the object? 326e407b4c8SLawrence Tang const char *required_field_str = 327e407b4c8SLawrence Tang json_object_get_string(required_field); 328e407b4c8SLawrence Tang if (json_object_object_get( 329e407b4c8SLawrence Tang object, required_field_str) == NULL) { 330e407b4c8SLawrence Tang log_validator_error( 331e407b4c8SLawrence Tang error_message, 332e407b4c8SLawrence Tang "Required field '%s' was not present in object '%s'.", 333e407b4c8SLawrence Tang required_field_str, field_name); 3348a2d737cSLawrence Tang return 0; 3358a2d737cSLawrence Tang } 3368a2d737cSLawrence Tang } 3378a2d737cSLawrence Tang } 3388a2d737cSLawrence Tang 339c4814594SLawrence Tang //Get additional properties value in advance. 340e407b4c8SLawrence Tang json_object *additional_properties = 341e407b4c8SLawrence Tang json_object_object_get(schema, "additionalProperties"); 342c4814594SLawrence Tang int additional_properties_allowed = 0; 343e407b4c8SLawrence Tang if (additional_properties != NULL && 344*f8fc7052SJohn Chung json_object_get_type(additional_properties) == json_type_boolean) { 345e407b4c8SLawrence Tang additional_properties_allowed = 346e407b4c8SLawrence Tang json_object_get_boolean(additional_properties); 347*f8fc7052SJohn Chung } 3488a2d737cSLawrence Tang 3498a2d737cSLawrence Tang //Run through the "properties" object and validate each of those in turn. 3508a2d737cSLawrence Tang json_object *properties = json_object_object_get(schema, "properties"); 351e407b4c8SLawrence Tang if (properties != NULL && 352e407b4c8SLawrence Tang json_object_get_type(properties) == json_type_object) { 353e407b4c8SLawrence Tang json_object_object_foreach(properties, key, value) 3548a2d737cSLawrence Tang { 3558a2d737cSLawrence Tang //If the given property name does not exist on the target object, ignore and continue next. 356e407b4c8SLawrence Tang json_object *object_prop = 357e407b4c8SLawrence Tang json_object_object_get(object, key); 358*f8fc7052SJohn Chung if (object_prop == NULL) { 3598a2d737cSLawrence Tang continue; 360*f8fc7052SJohn Chung } 3618a2d737cSLawrence Tang 3628a2d737cSLawrence Tang //Validate against the schema. 363e407b4c8SLawrence Tang if (!validate_field(key, value, object_prop, 364*f8fc7052SJohn Chung error_message)) { 3658a2d737cSLawrence Tang return 0; 3668a2d737cSLawrence Tang } 367*f8fc7052SJohn Chung } 368c4814594SLawrence Tang 369c4814594SLawrence Tang //If additional properties are banned, validate that no additional properties exist. 370e407b4c8SLawrence Tang if (!additional_properties_allowed) { 371e407b4c8SLawrence Tang json_object_object_foreach(object, key, value) 372c4814594SLawrence Tang { 373*f8fc7052SJohn Chung //Avoid compiler warning 374*f8fc7052SJohn Chung (void)value; 375*f8fc7052SJohn Chung 376c4814594SLawrence Tang //If the given property name does not exist on the schema object, fail validation. 377*f8fc7052SJohn Chung const json_object *schema_prop = 378e407b4c8SLawrence Tang json_object_object_get(properties, key); 379e407b4c8SLawrence Tang if (schema_prop == NULL) { 380e407b4c8SLawrence Tang log_validator_error( 381e407b4c8SLawrence Tang error_message, 382e407b4c8SLawrence Tang "Invalid additional property '%s' detected on field '%s'.", 383e407b4c8SLawrence Tang key, field_name); 384c4814594SLawrence Tang return 0; 385c4814594SLawrence Tang } 386c4814594SLawrence Tang } 387c4814594SLawrence Tang } 3888a2d737cSLawrence Tang } 3898a2d737cSLawrence Tang 3908a2d737cSLawrence Tang return 1; 3918a2d737cSLawrence Tang } 3928a2d737cSLawrence Tang 3938a2d737cSLawrence Tang //Validates a single array value according to the given specification. 394e407b4c8SLawrence Tang int validate_array(const char *field_name, json_object *schema, 395e407b4c8SLawrence Tang json_object *object, char *error_message) 3968a2d737cSLawrence Tang { 39745e04b0aSLawrence Tang //Iterate all items in the array, and validate according to the "items" schema. 39845e04b0aSLawrence Tang json_object *items_schema = json_object_object_get(schema, "items"); 399e407b4c8SLawrence Tang if (items_schema != NULL && 400e407b4c8SLawrence Tang json_object_get_type(items_schema) == json_type_object) { 40145e04b0aSLawrence Tang int array_len = json_object_array_length(object); 402e407b4c8SLawrence Tang for (int i = 0; i < array_len; i++) { 403e407b4c8SLawrence Tang if (!validate_field(field_name, items_schema, 404e407b4c8SLawrence Tang json_object_array_get_idx(object, 405e407b4c8SLawrence Tang i), 406*f8fc7052SJohn Chung error_message)) { 40745e04b0aSLawrence Tang return 0; 40845e04b0aSLawrence Tang } 40945e04b0aSLawrence Tang } 410*f8fc7052SJohn Chung } 41145e04b0aSLawrence Tang 4128a2d737cSLawrence Tang return 1; 4138a2d737cSLawrence Tang } 4148f793ac3SLawrence Tang 4158f793ac3SLawrence Tang //Enables/disables debugging globally for the JSON validator. 416e407b4c8SLawrence Tang void validate_schema_debug_enable() 417e407b4c8SLawrence Tang { 418e407b4c8SLawrence Tang json_validator_debug = 1; 419e407b4c8SLawrence Tang } 420e407b4c8SLawrence Tang void validate_schema_debug_disable() 421e407b4c8SLawrence Tang { 422e407b4c8SLawrence Tang json_validator_debug = 0; 423e407b4c8SLawrence Tang } 4248f793ac3SLawrence Tang 4257cd13908SLawrence Tang //Logs an error message to the given error message location and (optionally) provides debug output. 4268f793ac3SLawrence Tang void log_validator_error(char *error_message, const char *format, ...) 4278f793ac3SLawrence Tang { 4288f793ac3SLawrence Tang va_list args; 4298f793ac3SLawrence Tang 4308f793ac3SLawrence Tang //Log error to error out. 4318f793ac3SLawrence Tang va_start(args, format); 4328f793ac3SLawrence Tang vsnprintf(error_message, JSON_ERROR_MSG_MAX_LEN, format, args); 4338f793ac3SLawrence Tang va_end(args); 4348f793ac3SLawrence Tang 4358f793ac3SLawrence Tang //Debug message if necessary. 4368f793ac3SLawrence Tang va_start(args, format); 4378f793ac3SLawrence Tang log_validator_msg(format, args); 4388f793ac3SLawrence Tang va_end(args); 4398f793ac3SLawrence Tang } 4408f793ac3SLawrence Tang 4417cd13908SLawrence Tang //Logs a debug message to stdout, if validator debug is enabled. 4428f793ac3SLawrence Tang void log_validator_debug(const char *format, ...) 4438f793ac3SLawrence Tang { 4448f793ac3SLawrence Tang va_list args; 4458f793ac3SLawrence Tang va_start(args, format); 4468f793ac3SLawrence Tang log_validator_msg(format, args); 4478f793ac3SLawrence Tang va_end(args); 4488f793ac3SLawrence Tang } 4498f793ac3SLawrence Tang 4508f793ac3SLawrence Tang //Logs a single validator debug/error message. 4518f793ac3SLawrence Tang void log_validator_msg(const char *format, va_list args) 4528f793ac3SLawrence Tang { 4538f793ac3SLawrence Tang //Print debug output if debug is on. 454e407b4c8SLawrence Tang if (json_validator_debug) { 4558f793ac3SLawrence Tang //Make new format string for error. 4568f793ac3SLawrence Tang const char *header = "json_validator: "; 4578f793ac3SLawrence Tang char *new_format = malloc(strlen(header) + strlen(format) + 2); 4588f793ac3SLawrence Tang strcpy(new_format, header); 4598f793ac3SLawrence Tang strcat(new_format, format); 4608f793ac3SLawrence Tang strcat(new_format, "\n"); 4618f793ac3SLawrence Tang 4628f793ac3SLawrence Tang //Print & free format. 4638f793ac3SLawrence Tang vfprintf(stdout, new_format, args); 4648f793ac3SLawrence Tang free(new_format); 4658f793ac3SLawrence Tang } 4668f793ac3SLawrence Tang } 467