Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 1 | /** |
| 2 | * A very basic, non-complete implementation of a validator for the JSON Schema specification, |
| 3 | * for validating CPER-JSON. |
| 4 | * |
| 5 | * Author: Lawrence.Tang@arm.com |
| 6 | **/ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include <unistd.h> |
| 11 | #include <libgen.h> |
| 12 | #include <limits.h> |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 13 | #include <stdarg.h> |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 14 | #include "json.h" |
| 15 | #include "json-schema.h" |
| 16 | #include "edk/BaseTypes.h" |
| 17 | |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 18 | //Field definitions. |
| 19 | int json_validator_debug = 0; |
| 20 | |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 21 | //Private pre-definitions. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 22 | int validate_field(const char *name, json_object *schema, json_object *object, |
| 23 | char *error_message); |
| 24 | int validate_integer(const char *field_name, json_object *schema, |
| 25 | json_object *object, char *error_message); |
| 26 | int validate_string(const char *field_name, json_object *schema, |
| 27 | json_object *object, char *error_message); |
| 28 | int validate_object(const char *field_name, json_object *schema, |
| 29 | json_object *object, char *error_message); |
| 30 | int validate_array(const char *field_name, json_object *schema, |
| 31 | json_object *object, char *error_message); |
| 32 | void log_validator_error(char *error_message, const char *format, ...); |
| 33 | void log_validator_debug(const char *format, ...); |
| 34 | void log_validator_msg(const char *format, va_list args); |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 35 | |
| 36 | //Validates a single JSON object against a provided schema file, returning 1 on success and 0 on failure to validate. |
| 37 | //Error message space must be allocated prior to call. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 38 | int validate_schema_from_file(const char *schema_file, json_object *object, |
| 39 | char *error_message) |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 40 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 41 | //Load schema IR from file. |
| 42 | json_object *schema_ir = json_object_from_file(schema_file); |
| 43 | if (schema_ir == NULL) { |
| 44 | log_validator_error(error_message, |
| 45 | "Failed to load schema from file '%s'.", |
| 46 | schema_file); |
| 47 | return 0; |
| 48 | } |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 49 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 50 | //Get the directory of the file. |
| 51 | char *schema_file_copy = malloc(strlen(schema_file) + 1); |
| 52 | strcpy(schema_file_copy, schema_file); |
| 53 | char *schema_dir = dirname(schema_file_copy); |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 54 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 55 | int result = |
| 56 | validate_schema(schema_ir, schema_dir, object, error_message); |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 57 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 58 | //Free memory from directory call. |
| 59 | free(schema_file_copy); |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 60 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 61 | return result; |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | //Validates a single JSON object against a provided schema, returning 1 on success and 0 on failure to validate. |
| 65 | //Error message space must be allocated prior to call. |
| 66 | //If the schema does not include any other sub-schemas using "$ref", then leaving schema_directory as NULL is valid. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 67 | int validate_schema(json_object *schema, char *schema_directory, |
| 68 | json_object *object, char *error_message) |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 69 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 70 | //Check that the schema version is the same as this validator. |
| 71 | json_object *schema_ver = json_object_object_get(schema, "$schema"); |
| 72 | if (schema_ver == NULL || |
| 73 | strcmp(json_object_get_string(schema_ver), JSON_SCHEMA_VERSION)) { |
| 74 | log_validator_error( |
| 75 | error_message, |
| 76 | "Provided schema is not of the same version that is referenced by this validator, or is not a schema."); |
| 77 | return 0; |
| 78 | } |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 79 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 80 | //Change current directory into the schema directory. |
| 81 | char *original_cwd = malloc(PATH_MAX); |
| 82 | if (getcwd(original_cwd, PATH_MAX) == NULL) { |
| 83 | log_validator_error(error_message, |
| 84 | "Failed fetching the current directory."); |
| 85 | return 0; |
| 86 | } |
| 87 | if (chdir(schema_directory)) { |
| 88 | log_validator_error(error_message, |
| 89 | "Failed to chdir into schema directory."); |
| 90 | return 0; |
| 91 | } |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 92 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 93 | //Parse the top level structure appropriately. |
| 94 | int result = validate_field("parent", schema, object, error_message); |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 95 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 96 | //Change back to original CWD. |
| 97 | chdir(original_cwd); |
| 98 | free(original_cwd); |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 99 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 100 | if (result) |
| 101 | log_validator_debug( |
| 102 | "Successfully validated the provided object against schema."); |
| 103 | return result; |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | //Validates a single JSON field given a schema/object. |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 107 | //Returns -1 on fatal/error failure, 0 on validation failure, and 1 on validation. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 108 | int validate_field(const char *field_name, json_object *schema, |
| 109 | json_object *object, char *error_message) |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 110 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 111 | log_validator_debug("Validating field '%s'...", field_name); |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 112 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 113 | //If there is a "$ref" field, attempt to load the referenced schema. |
| 114 | json_object *ref_schema = json_object_object_get(schema, "$ref"); |
| 115 | if (ref_schema != NULL && |
| 116 | json_object_get_type(ref_schema) == json_type_string) { |
| 117 | log_validator_debug("$ref schema detected for field '%s'.", |
| 118 | field_name); |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 119 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 120 | //Attempt to load. If loading fails, report error. |
| 121 | const char *ref_path = json_object_get_string(ref_schema); |
| 122 | schema = json_object_from_file(ref_path); |
| 123 | if (schema == NULL) { |
| 124 | log_validator_error( |
| 125 | error_message, |
| 126 | "Failed to open referenced schema file '%s'.", |
| 127 | ref_path); |
| 128 | return -1; |
| 129 | } |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 130 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 131 | log_validator_debug("loaded schema path '%s' for field '%s'.", |
| 132 | ref_path, field_name); |
| 133 | } |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 134 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 135 | //Get the schema field type. |
| 136 | json_object *desired_field_type = |
| 137 | json_object_object_get(schema, "type"); |
| 138 | if (desired_field_type == NULL || |
| 139 | !json_object_is_type(desired_field_type, json_type_string)) { |
| 140 | log_validator_error( |
| 141 | error_message, |
| 142 | "Desired field type not provided within schema/is not a string for field '%s' (schema violation).", |
| 143 | field_name); |
| 144 | return -1; |
| 145 | } |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 146 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 147 | //Check the field types are actually equal. |
| 148 | const char *desired_field_type_str = |
| 149 | json_object_get_string(desired_field_type); |
| 150 | if (!((!strcmp(desired_field_type_str, "object") && |
| 151 | json_object_is_type(object, json_type_object)) || |
| 152 | (!strcmp(desired_field_type_str, "array") && |
| 153 | json_object_is_type(object, json_type_array)) || |
| 154 | (!strcmp(desired_field_type_str, "integer") && |
| 155 | json_object_is_type(object, json_type_int)) || |
| 156 | (!strcmp(desired_field_type_str, "string") && |
| 157 | json_object_is_type(object, json_type_string)) || |
| 158 | (!strcmp(desired_field_type_str, "boolean") && |
| 159 | json_object_is_type(object, json_type_boolean)) || |
| 160 | (!strcmp(desired_field_type_str, "double") && |
| 161 | json_object_is_type(object, json_type_double)))) { |
| 162 | log_validator_error(error_message, |
| 163 | "Field type match failed for field '%s'.", |
| 164 | field_name); |
| 165 | return 0; |
| 166 | } |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 167 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 168 | //If the schema contains a "oneOf" array, we need to validate the field against each of the |
| 169 | //possible options in turn. |
| 170 | json_object *one_of = json_object_object_get(schema, "oneOf"); |
| 171 | if (one_of != NULL && json_object_get_type(one_of) == json_type_array) { |
| 172 | log_validator_debug("oneOf options detected for field '%s'.", |
| 173 | field_name); |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 174 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 175 | int len = json_object_array_length(one_of); |
| 176 | int validated = 0; |
| 177 | for (int i = 0; i < len; i++) { |
| 178 | //If the "oneOf" member isn't an object, warn on schema violation. |
| 179 | json_object *one_of_option = |
| 180 | json_object_array_get_idx(one_of, i); |
| 181 | if (one_of_option == NULL || |
| 182 | json_object_get_type(one_of_option) != |
| 183 | json_type_object) { |
| 184 | log_validator_debug( |
| 185 | "Schema Warning: 'oneOf' member for field '%s' is not an object, schema violation.", |
| 186 | field_name); |
| 187 | continue; |
| 188 | } |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 189 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 190 | //Validate field with schema. |
| 191 | validated = validate_field(field_name, one_of_option, |
| 192 | object, error_message); |
| 193 | if (validated == -1) |
| 194 | return -1; |
| 195 | if (validated) |
| 196 | break; |
| 197 | } |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 198 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 199 | //Return if failed all checks. |
| 200 | if (!validated) { |
| 201 | log_validator_error( |
| 202 | error_message, |
| 203 | "No schema object structures matched provided object for field '%s'.", |
| 204 | field_name); |
| 205 | return 0; |
| 206 | } |
| 207 | } |
Lawrence Tang | 45e04b0 | 2022-07-12 16:54:01 +0100 | [diff] [blame] | 208 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 209 | //Switch and validate each type in turn. |
| 210 | switch (json_object_get_type(object)) { |
| 211 | case json_type_int: |
| 212 | return validate_integer(field_name, schema, object, |
| 213 | error_message); |
| 214 | case json_type_string: |
| 215 | return validate_string(field_name, schema, object, |
| 216 | error_message); |
| 217 | case json_type_object: |
| 218 | return validate_object(field_name, schema, object, |
| 219 | error_message); |
| 220 | case json_type_array: |
| 221 | return validate_array(field_name, schema, object, |
| 222 | error_message); |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 223 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 224 | //We don't perform extra validation on this type. |
| 225 | default: |
| 226 | log_validator_debug( |
| 227 | "validation passed for '%s' (no extra validation).", |
| 228 | field_name); |
| 229 | return 1; |
| 230 | } |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | //Validates a single integer value according to the given specification. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 234 | int validate_integer(const char *field_name, json_object *schema, |
| 235 | json_object *object, char *error_message) |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 236 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 237 | //Is there a minimum/maximum specified? If so, check those. |
| 238 | //Validate minimum. |
| 239 | json_object *min_value = json_object_object_get(schema, "minimum"); |
| 240 | if (min_value != NULL && |
| 241 | json_object_is_type(min_value, json_type_int)) { |
| 242 | int min_value_int = json_object_get_int(min_value); |
| 243 | if (json_object_get_uint64(object) < min_value_int) { |
| 244 | log_validator_error( |
| 245 | error_message, |
| 246 | "Failed to validate integer field '%s'. Value was below minimum of %d.", |
| 247 | field_name, min_value_int); |
| 248 | return 0; |
| 249 | } |
| 250 | } |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 251 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 252 | //Validate maximum. |
| 253 | json_object *max_value = json_object_object_get(schema, "maximum"); |
| 254 | if (max_value != NULL && |
| 255 | json_object_is_type(max_value, json_type_int)) { |
| 256 | int max_value_int = json_object_get_int(max_value); |
| 257 | if (json_object_get_uint64(object) > max_value_int) { |
| 258 | log_validator_error( |
| 259 | error_message, |
| 260 | "Failed to validate integer field '%s'. Value was above maximum of %d.", |
| 261 | field_name, max_value_int); |
| 262 | return 0; |
| 263 | } |
| 264 | } |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 265 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 266 | return 1; |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | //Validates a single string value according to the given specification. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 270 | int validate_string(const char *field_name, json_object *schema, |
| 271 | json_object *object, char *error_message) |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 272 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 273 | //todo: if there is a "pattern" field, verify the string with RegEx. |
| 274 | return 1; |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | //Validates a single object value according to the given specification. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 278 | int validate_object(const char *field_name, json_object *schema, |
| 279 | json_object *object, char *error_message) |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 280 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 281 | //Are there a set of "required" fields? If so, check they all exist. |
| 282 | json_object *required_fields = |
| 283 | json_object_object_get(schema, "required"); |
| 284 | if (required_fields != NULL && |
| 285 | json_object_get_type(required_fields) == json_type_array) { |
| 286 | log_validator_debug( |
| 287 | "Required fields found for '%s', matching...", |
| 288 | field_name); |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 289 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 290 | int len = json_object_array_length(required_fields); |
| 291 | for (int i = 0; i < len; i++) { |
| 292 | //Get the required field from schema. |
| 293 | json_object *required_field = |
| 294 | json_object_array_get_idx(required_fields, i); |
| 295 | if (json_object_get_type(required_field) != |
| 296 | json_type_string) { |
| 297 | log_validator_error( |
| 298 | error_message, |
| 299 | "Required field for object '%s' is not a string (schema violation).", |
| 300 | field_name); |
| 301 | return 0; |
| 302 | } |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 303 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 304 | //Does it exist in the object? |
| 305 | const char *required_field_str = |
| 306 | json_object_get_string(required_field); |
| 307 | if (json_object_object_get( |
| 308 | object, required_field_str) == NULL) { |
| 309 | log_validator_error( |
| 310 | error_message, |
| 311 | "Required field '%s' was not present in object '%s'.", |
| 312 | required_field_str, field_name); |
| 313 | return 0; |
| 314 | } |
| 315 | } |
| 316 | } |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 317 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 318 | //Get additional properties value in advance. |
| 319 | json_object *additional_properties = |
| 320 | json_object_object_get(schema, "additionalProperties"); |
| 321 | int additional_properties_allowed = 0; |
| 322 | if (additional_properties != NULL && |
| 323 | json_object_get_type(additional_properties) == json_type_boolean) |
| 324 | additional_properties_allowed = |
| 325 | json_object_get_boolean(additional_properties); |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 326 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 327 | //Run through the "properties" object and validate each of those in turn. |
| 328 | json_object *properties = json_object_object_get(schema, "properties"); |
| 329 | if (properties != NULL && |
| 330 | json_object_get_type(properties) == json_type_object) { |
| 331 | json_object_object_foreach(properties, key, value) |
| 332 | { |
| 333 | //If the given property name does not exist on the target object, ignore and continue next. |
| 334 | json_object *object_prop = |
| 335 | json_object_object_get(object, key); |
| 336 | if (object_prop == NULL) |
| 337 | continue; |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 338 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 339 | //Validate against the schema. |
| 340 | if (!validate_field(key, value, object_prop, |
| 341 | error_message)) |
| 342 | return 0; |
| 343 | } |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 344 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 345 | //If additional properties are banned, validate that no additional properties exist. |
| 346 | if (!additional_properties_allowed) { |
| 347 | json_object_object_foreach(object, key, value) |
| 348 | { |
| 349 | //If the given property name does not exist on the schema object, fail validation. |
| 350 | json_object *schema_prop = |
| 351 | json_object_object_get(properties, key); |
| 352 | if (schema_prop == NULL) { |
| 353 | log_validator_error( |
| 354 | error_message, |
| 355 | "Invalid additional property '%s' detected on field '%s'.", |
| 356 | key, field_name); |
| 357 | return 0; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
Lawrence Tang | c481459 | 2022-07-13 10:24:09 +0100 | [diff] [blame] | 362 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 363 | return 1; |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | //Validates a single array value according to the given specification. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 367 | int validate_array(const char *field_name, json_object *schema, |
| 368 | json_object *object, char *error_message) |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 369 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 370 | //Iterate all items in the array, and validate according to the "items" schema. |
| 371 | json_object *items_schema = json_object_object_get(schema, "items"); |
| 372 | if (items_schema != NULL && |
| 373 | json_object_get_type(items_schema) == json_type_object) { |
| 374 | int array_len = json_object_array_length(object); |
| 375 | for (int i = 0; i < array_len; i++) { |
| 376 | if (!validate_field(field_name, items_schema, |
| 377 | json_object_array_get_idx(object, |
| 378 | i), |
| 379 | error_message)) |
| 380 | return 0; |
| 381 | } |
| 382 | } |
Lawrence Tang | 45e04b0 | 2022-07-12 16:54:01 +0100 | [diff] [blame] | 383 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 384 | return 1; |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | //Enables/disables debugging globally for the JSON validator. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 388 | void validate_schema_debug_enable() |
| 389 | { |
| 390 | json_validator_debug = 1; |
| 391 | } |
| 392 | void validate_schema_debug_disable() |
| 393 | { |
| 394 | json_validator_debug = 0; |
| 395 | } |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 396 | |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 397 | //Logs an error message to the given error message location and (optionally) provides debug output. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 398 | void log_validator_error(char *error_message, const char *format, ...) |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 399 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 400 | va_list args; |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 401 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 402 | //Log error to error out. |
| 403 | va_start(args, format); |
| 404 | vsnprintf(error_message, JSON_ERROR_MSG_MAX_LEN, format, args); |
| 405 | va_end(args); |
| 406 | |
| 407 | //Debug message if necessary. |
| 408 | va_start(args, format); |
| 409 | log_validator_msg(format, args); |
| 410 | va_end(args); |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 413 | //Logs a debug message to stdout, if validator debug is enabled. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 414 | void log_validator_debug(const char *format, ...) |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 415 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 416 | va_list args; |
| 417 | va_start(args, format); |
| 418 | log_validator_msg(format, args); |
| 419 | va_end(args); |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | //Logs a single validator debug/error message. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 423 | void log_validator_msg(const char *format, va_list args) |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 424 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 425 | //Print debug output if debug is on. |
| 426 | if (json_validator_debug) { |
| 427 | //Make new format string for error. |
| 428 | const char *header = "json_validator: "; |
| 429 | char *new_format = malloc(strlen(header) + strlen(format) + 2); |
| 430 | strcpy(new_format, header); |
| 431 | strcat(new_format, format); |
| 432 | strcat(new_format, "\n"); |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 433 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame^] | 434 | //Print & free format. |
| 435 | vfprintf(stdout, new_format, args); |
| 436 | free(new_format); |
| 437 | } |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 438 | } |