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