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>
15*e42fb487SThu Nguyen #include <libcper/json-schema.h>
16*e42fb487SThu Nguyen #include <libcper/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,
27b35d957eSEd 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.
validate_schema_from_file(const char * schema_file,json_object * object,char * error_message)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.
validate_schema(json_object * schema,char * schema_directory,json_object * object,char * error_message)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);
10212587b29SKarthik Rajagopalan if (result < 0) {
10312587b29SKarthik Rajagopalan log_validator_error(error_message,
10412587b29SKarthik Rajagopalan "Failed validating schema for parent.");
10512587b29SKarthik Rajagopalan result = 0;
10612587b29SKarthik Rajagopalan }
1078a2d737cSLawrence Tang
1088a2d737cSLawrence Tang //Change back to original CWD.
109f8fc7052SJohn Chung if (chdir(original_cwd)) {
110f8fc7052SJohn Chung log_validator_error(error_message,
111f8fc7052SJohn Chung "Failed to chdir into original directory.");
112f8fc7052SJohn Chung }
113f8fc7052SJohn Chung
1148a2d737cSLawrence Tang free(original_cwd);
1158a2d737cSLawrence Tang
116f8fc7052SJohn Chung if (result) {
117e407b4c8SLawrence Tang log_validator_debug(
118e407b4c8SLawrence Tang "Successfully validated the provided object against schema.");
119f8fc7052SJohn Chung }
1208a2d737cSLawrence Tang return result;
1218a2d737cSLawrence Tang }
1228a2d737cSLawrence Tang
1238a2d737cSLawrence Tang //Validates a single JSON field given a schema/object.
1248f793ac3SLawrence Tang //Returns -1 on fatal/error failure, 0 on validation failure, and 1 on validation.
validate_field(const char * field_name,json_object * schema,json_object * object,char * error_message)125e407b4c8SLawrence Tang int validate_field(const char *field_name, json_object *schema,
126e407b4c8SLawrence Tang json_object *object, char *error_message)
1278a2d737cSLawrence Tang {
12812587b29SKarthik Rajagopalan int ret = -1;
12912587b29SKarthik Rajagopalan
1308f793ac3SLawrence Tang log_validator_debug("Validating field '%s'...", field_name);
1318f793ac3SLawrence Tang
1328a2d737cSLawrence Tang //If there is a "$ref" field, attempt to load the referenced schema.
13312587b29SKarthik Rajagopalan json_object *ref_field = json_object_object_get(schema, "$ref");
13412587b29SKarthik Rajagopalan if (ref_field != NULL &&
13512587b29SKarthik Rajagopalan json_object_get_type(ref_field) == json_type_string) {
136e407b4c8SLawrence Tang log_validator_debug("$ref schema detected for field '%s'.",
137e407b4c8SLawrence Tang field_name);
1388f793ac3SLawrence Tang
1398a2d737cSLawrence Tang //Attempt to load. If loading fails, report error.
14012587b29SKarthik Rajagopalan const char *ref_path = json_object_get_string(ref_field);
14112587b29SKarthik Rajagopalan json_object *ref_schema = json_object_from_file(ref_path);
14212587b29SKarthik Rajagopalan if (ref_schema == NULL) {
143e407b4c8SLawrence Tang log_validator_error(
144e407b4c8SLawrence Tang error_message,
145e407b4c8SLawrence Tang "Failed to open referenced schema file '%s'.",
146e407b4c8SLawrence Tang ref_path);
1478f793ac3SLawrence Tang return -1;
1488a2d737cSLawrence Tang }
1498f793ac3SLawrence Tang
150e407b4c8SLawrence Tang log_validator_debug("loaded schema path '%s' for field '%s'.",
151e407b4c8SLawrence Tang ref_path, field_name);
15212587b29SKarthik Rajagopalan
15312587b29SKarthik Rajagopalan //Validate field with schema.
15412587b29SKarthik Rajagopalan ret = validate_field(field_name, ref_schema, object,
15512587b29SKarthik Rajagopalan error_message);
15612587b29SKarthik Rajagopalan json_object_put(ref_schema);
15712587b29SKarthik Rajagopalan
15812587b29SKarthik Rajagopalan return ret;
1598a2d737cSLawrence Tang }
1608a2d737cSLawrence Tang
1618a2d737cSLawrence Tang //Get the schema field type.
162e407b4c8SLawrence Tang json_object *desired_field_type =
163e407b4c8SLawrence Tang json_object_object_get(schema, "type");
164e407b4c8SLawrence Tang if (desired_field_type == NULL ||
165e407b4c8SLawrence Tang !json_object_is_type(desired_field_type, json_type_string)) {
166e407b4c8SLawrence Tang log_validator_error(
167e407b4c8SLawrence Tang error_message,
168e407b4c8SLawrence Tang "Desired field type not provided within schema/is not a string for field '%s' (schema violation).",
169e407b4c8SLawrence Tang field_name);
17012587b29SKarthik Rajagopalan return 0;
1718a2d737cSLawrence Tang }
1728a2d737cSLawrence Tang
1738a2d737cSLawrence Tang //Check the field types are actually equal.
174e407b4c8SLawrence Tang const char *desired_field_type_str =
175e407b4c8SLawrence Tang json_object_get_string(desired_field_type);
176e407b4c8SLawrence Tang if (!((!strcmp(desired_field_type_str, "object") &&
177e407b4c8SLawrence Tang json_object_is_type(object, json_type_object)) ||
178e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "array") &&
179e407b4c8SLawrence Tang json_object_is_type(object, json_type_array)) ||
180e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "integer") &&
181e407b4c8SLawrence Tang json_object_is_type(object, json_type_int)) ||
182e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "string") &&
183e407b4c8SLawrence Tang json_object_is_type(object, json_type_string)) ||
184e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "boolean") &&
185e407b4c8SLawrence Tang json_object_is_type(object, json_type_boolean)) ||
186e407b4c8SLawrence Tang (!strcmp(desired_field_type_str, "double") &&
187e407b4c8SLawrence Tang json_object_is_type(object, json_type_double)))) {
188e407b4c8SLawrence Tang log_validator_error(error_message,
189e407b4c8SLawrence Tang "Field type match failed for field '%s'.",
190e407b4c8SLawrence Tang field_name);
1918a2d737cSLawrence Tang return 0;
1928a2d737cSLawrence Tang }
1938a2d737cSLawrence Tang
1948f793ac3SLawrence Tang //If the schema contains a "oneOf" array, we need to validate the field against each of the
1958f793ac3SLawrence Tang //possible options in turn.
1968f793ac3SLawrence Tang json_object *one_of = json_object_object_get(schema, "oneOf");
197e407b4c8SLawrence Tang if (one_of != NULL && json_object_get_type(one_of) == json_type_array) {
198e407b4c8SLawrence Tang log_validator_debug("oneOf options detected for field '%s'.",
199e407b4c8SLawrence Tang field_name);
2008f793ac3SLawrence Tang
2018f793ac3SLawrence Tang int len = json_object_array_length(one_of);
2028f793ac3SLawrence Tang int validated = 0;
203e407b4c8SLawrence Tang for (int i = 0; i < len; i++) {
2048f793ac3SLawrence Tang //If the "oneOf" member isn't an object, warn on schema violation.
205e407b4c8SLawrence Tang json_object *one_of_option =
206e407b4c8SLawrence Tang json_object_array_get_idx(one_of, i);
207e407b4c8SLawrence Tang if (one_of_option == NULL ||
208e407b4c8SLawrence Tang json_object_get_type(one_of_option) !=
209e407b4c8SLawrence Tang json_type_object) {
210e407b4c8SLawrence Tang log_validator_debug(
211e407b4c8SLawrence Tang "Schema Warning: 'oneOf' member for field '%s' is not an object, schema violation.",
212e407b4c8SLawrence Tang field_name);
2138f793ac3SLawrence Tang continue;
2148f793ac3SLawrence Tang }
2158f793ac3SLawrence Tang
2168f793ac3SLawrence Tang //Validate field with schema.
217e407b4c8SLawrence Tang validated = validate_field(field_name, one_of_option,
218e407b4c8SLawrence Tang object, error_message);
219f8fc7052SJohn Chung if (validated == -1) {
2208f793ac3SLawrence Tang return -1;
221f8fc7052SJohn Chung }
222f8fc7052SJohn Chung if (validated) {
2238f793ac3SLawrence Tang break;
2248f793ac3SLawrence Tang }
225f8fc7052SJohn Chung }
2268f793ac3SLawrence Tang
2278f793ac3SLawrence Tang //Return if failed all checks.
228e407b4c8SLawrence Tang if (!validated) {
229e407b4c8SLawrence Tang log_validator_error(
230e407b4c8SLawrence Tang error_message,
231e407b4c8SLawrence Tang "No schema object structures matched provided object for field '%s'.",
232e407b4c8SLawrence Tang field_name);
2338f793ac3SLawrence Tang return 0;
2348f793ac3SLawrence Tang }
2358f793ac3SLawrence Tang }
23645e04b0aSLawrence Tang
2378a2d737cSLawrence Tang //Switch and validate each type in turn.
238e407b4c8SLawrence Tang switch (json_object_get_type(object)) {
2398a2d737cSLawrence Tang case json_type_int:
240e407b4c8SLawrence Tang return validate_integer(field_name, schema, object,
241e407b4c8SLawrence Tang error_message);
2428a2d737cSLawrence Tang case json_type_string:
243e407b4c8SLawrence Tang return validate_string(field_name, schema, object,
244e407b4c8SLawrence Tang error_message);
2458a2d737cSLawrence Tang case json_type_object:
246e407b4c8SLawrence Tang return validate_object(field_name, schema, object,
247e407b4c8SLawrence Tang error_message);
2488a2d737cSLawrence Tang case json_type_array:
249e407b4c8SLawrence Tang return validate_array(field_name, schema, object,
250e407b4c8SLawrence Tang error_message);
2518a2d737cSLawrence Tang
2528a2d737cSLawrence Tang //We don't perform extra validation on this type.
2538a2d737cSLawrence Tang default:
254e407b4c8SLawrence Tang log_validator_debug(
255e407b4c8SLawrence Tang "validation passed for '%s' (no extra validation).",
256e407b4c8SLawrence Tang field_name);
2578a2d737cSLawrence Tang return 1;
2588a2d737cSLawrence Tang }
2598a2d737cSLawrence Tang }
2608a2d737cSLawrence Tang
2618a2d737cSLawrence Tang //Validates a single integer value according to the given specification.
validate_integer(const char * field_name,json_object * schema,json_object * object,char * error_message)262e407b4c8SLawrence Tang int validate_integer(const char *field_name, json_object *schema,
263e407b4c8SLawrence Tang json_object *object, char *error_message)
2648a2d737cSLawrence Tang {
2658a2d737cSLawrence Tang //Is there a minimum/maximum specified? If so, check those.
2668a2d737cSLawrence Tang //Validate minimum.
2678a2d737cSLawrence Tang json_object *min_value = json_object_object_get(schema, "minimum");
268e407b4c8SLawrence Tang if (min_value != NULL &&
269e407b4c8SLawrence Tang json_object_is_type(min_value, json_type_int)) {
2708a2d737cSLawrence Tang int min_value_int = json_object_get_int(min_value);
271f8fc7052SJohn Chung if (json_object_get_uint64(object) < (uint64_t)min_value_int) {
272e407b4c8SLawrence Tang log_validator_error(
273e407b4c8SLawrence Tang error_message,
274e407b4c8SLawrence Tang "Failed to validate integer field '%s'. Value was below minimum of %d.",
275e407b4c8SLawrence Tang field_name, min_value_int);
2768a2d737cSLawrence Tang return 0;
2778a2d737cSLawrence Tang }
2788a2d737cSLawrence Tang }
2798a2d737cSLawrence Tang
2808a2d737cSLawrence Tang //Validate maximum.
2818a2d737cSLawrence Tang json_object *max_value = json_object_object_get(schema, "maximum");
282e407b4c8SLawrence Tang if (max_value != NULL &&
283e407b4c8SLawrence Tang json_object_is_type(max_value, json_type_int)) {
2848a2d737cSLawrence Tang int max_value_int = json_object_get_int(max_value);
285f8fc7052SJohn Chung if (json_object_get_uint64(object) > (uint64_t)max_value_int) {
286e407b4c8SLawrence Tang log_validator_error(
287e407b4c8SLawrence Tang error_message,
288e407b4c8SLawrence Tang "Failed to validate integer field '%s'. Value was above maximum of %d.",
289e407b4c8SLawrence Tang field_name, max_value_int);
2908a2d737cSLawrence Tang return 0;
2918a2d737cSLawrence Tang }
2928a2d737cSLawrence Tang }
2938a2d737cSLawrence Tang
2948a2d737cSLawrence Tang return 1;
2958a2d737cSLawrence Tang }
2968a2d737cSLawrence Tang
2978a2d737cSLawrence Tang //Validates a single string value according to the given specification.
validate_string(const char * field_name,json_object * schema,json_object * object,const char * error_message)298e407b4c8SLawrence Tang int validate_string(const char *field_name, json_object *schema,
299b35d957eSEd Tanous json_object *object, const char *error_message)
3008a2d737cSLawrence Tang {
3018a2d737cSLawrence Tang //todo: if there is a "pattern" field, verify the string with RegEx.
302f8fc7052SJohn Chung (void)field_name;
303f8fc7052SJohn Chung (void)schema;
304f8fc7052SJohn Chung (void)object;
305f8fc7052SJohn Chung (void)error_message;
306f8fc7052SJohn Chung
3078a2d737cSLawrence Tang return 1;
3088a2d737cSLawrence Tang }
3098a2d737cSLawrence Tang
3108a2d737cSLawrence Tang //Validates a single object value according to the given specification.
validate_object(const char * field_name,json_object * schema,json_object * object,char * error_message)311e407b4c8SLawrence Tang int validate_object(const char *field_name, json_object *schema,
312e407b4c8SLawrence Tang json_object *object, char *error_message)
3138a2d737cSLawrence Tang {
3148a2d737cSLawrence Tang //Are there a set of "required" fields? If so, check they all exist.
315e407b4c8SLawrence Tang json_object *required_fields =
316e407b4c8SLawrence Tang json_object_object_get(schema, "required");
317e407b4c8SLawrence Tang if (required_fields != NULL &&
318e407b4c8SLawrence Tang json_object_get_type(required_fields) == json_type_array) {
319e407b4c8SLawrence Tang log_validator_debug(
320e407b4c8SLawrence Tang "Required fields found for '%s', matching...",
321e407b4c8SLawrence Tang field_name);
3228f793ac3SLawrence Tang
3238a2d737cSLawrence Tang int len = json_object_array_length(required_fields);
324e407b4c8SLawrence Tang for (int i = 0; i < len; i++) {
3258a2d737cSLawrence Tang //Get the required field from schema.
326e407b4c8SLawrence Tang json_object *required_field =
327e407b4c8SLawrence Tang json_object_array_get_idx(required_fields, i);
328e407b4c8SLawrence Tang if (json_object_get_type(required_field) !=
329e407b4c8SLawrence Tang json_type_string) {
330e407b4c8SLawrence Tang log_validator_error(
331e407b4c8SLawrence Tang error_message,
332e407b4c8SLawrence Tang "Required field for object '%s' is not a string (schema violation).",
333e407b4c8SLawrence Tang field_name);
3348a2d737cSLawrence Tang return 0;
3358a2d737cSLawrence Tang }
3368a2d737cSLawrence Tang
3378a2d737cSLawrence Tang //Does it exist in the object?
338e407b4c8SLawrence Tang const char *required_field_str =
339e407b4c8SLawrence Tang json_object_get_string(required_field);
340e407b4c8SLawrence Tang if (json_object_object_get(
341e407b4c8SLawrence Tang object, required_field_str) == NULL) {
342e407b4c8SLawrence Tang log_validator_error(
343e407b4c8SLawrence Tang error_message,
344e407b4c8SLawrence Tang "Required field '%s' was not present in object '%s'.",
345e407b4c8SLawrence Tang required_field_str, field_name);
3468a2d737cSLawrence Tang return 0;
3478a2d737cSLawrence Tang }
3488a2d737cSLawrence Tang }
3498a2d737cSLawrence Tang }
3508a2d737cSLawrence Tang
351c4814594SLawrence Tang //Get additional properties value in advance.
352e407b4c8SLawrence Tang json_object *additional_properties =
353e407b4c8SLawrence Tang json_object_object_get(schema, "additionalProperties");
354c4814594SLawrence Tang int additional_properties_allowed = 0;
355e407b4c8SLawrence Tang if (additional_properties != NULL &&
356f8fc7052SJohn Chung json_object_get_type(additional_properties) == json_type_boolean) {
357e407b4c8SLawrence Tang additional_properties_allowed =
358e407b4c8SLawrence Tang json_object_get_boolean(additional_properties);
359f8fc7052SJohn Chung }
3608a2d737cSLawrence Tang
3618a2d737cSLawrence Tang //Run through the "properties" object and validate each of those in turn.
3628a2d737cSLawrence Tang json_object *properties = json_object_object_get(schema, "properties");
363e407b4c8SLawrence Tang if (properties != NULL &&
364e407b4c8SLawrence Tang json_object_get_type(properties) == json_type_object) {
365e407b4c8SLawrence Tang json_object_object_foreach(properties, key, value)
3668a2d737cSLawrence Tang {
3678a2d737cSLawrence Tang //If the given property name does not exist on the target object, ignore and continue next.
368e407b4c8SLawrence Tang json_object *object_prop =
369e407b4c8SLawrence Tang json_object_object_get(object, key);
370f8fc7052SJohn Chung if (object_prop == NULL) {
3718a2d737cSLawrence Tang continue;
372f8fc7052SJohn Chung }
3738a2d737cSLawrence Tang
3748a2d737cSLawrence Tang //Validate against the schema.
375e407b4c8SLawrence Tang if (!validate_field(key, value, object_prop,
376f8fc7052SJohn Chung error_message)) {
3778a2d737cSLawrence Tang return 0;
3788a2d737cSLawrence Tang }
379f8fc7052SJohn Chung }
380c4814594SLawrence Tang
381c4814594SLawrence Tang //If additional properties are banned, validate that no additional properties exist.
382e407b4c8SLawrence Tang if (!additional_properties_allowed) {
383e407b4c8SLawrence Tang json_object_object_foreach(object, key, value)
384c4814594SLawrence Tang {
385f8fc7052SJohn Chung //Avoid compiler warning
386f8fc7052SJohn Chung (void)value;
387f8fc7052SJohn Chung
388c4814594SLawrence Tang //If the given property name does not exist on the schema object, fail validation.
389f8fc7052SJohn Chung const json_object *schema_prop =
390e407b4c8SLawrence Tang json_object_object_get(properties, key);
391e407b4c8SLawrence Tang if (schema_prop == NULL) {
392e407b4c8SLawrence Tang log_validator_error(
393e407b4c8SLawrence Tang error_message,
394e407b4c8SLawrence Tang "Invalid additional property '%s' detected on field '%s'.",
395e407b4c8SLawrence Tang key, field_name);
396c4814594SLawrence Tang return 0;
397c4814594SLawrence Tang }
398c4814594SLawrence Tang }
399c4814594SLawrence Tang }
4008a2d737cSLawrence Tang }
4018a2d737cSLawrence Tang
4028a2d737cSLawrence Tang return 1;
4038a2d737cSLawrence Tang }
4048a2d737cSLawrence Tang
4058a2d737cSLawrence Tang //Validates a single array value according to the given specification.
validate_array(const char * field_name,json_object * schema,json_object * object,char * error_message)406e407b4c8SLawrence Tang int validate_array(const char *field_name, json_object *schema,
407e407b4c8SLawrence Tang json_object *object, char *error_message)
4088a2d737cSLawrence Tang {
40945e04b0aSLawrence Tang //Iterate all items in the array, and validate according to the "items" schema.
41045e04b0aSLawrence Tang json_object *items_schema = json_object_object_get(schema, "items");
411e407b4c8SLawrence Tang if (items_schema != NULL &&
412e407b4c8SLawrence Tang json_object_get_type(items_schema) == json_type_object) {
41345e04b0aSLawrence Tang int array_len = json_object_array_length(object);
414e407b4c8SLawrence Tang for (int i = 0; i < array_len; i++) {
415e407b4c8SLawrence Tang if (!validate_field(field_name, items_schema,
416e407b4c8SLawrence Tang json_object_array_get_idx(object,
417e407b4c8SLawrence Tang i),
418f8fc7052SJohn Chung error_message)) {
41945e04b0aSLawrence Tang return 0;
42045e04b0aSLawrence Tang }
42145e04b0aSLawrence Tang }
422f8fc7052SJohn Chung }
42345e04b0aSLawrence Tang
4248a2d737cSLawrence Tang return 1;
4258a2d737cSLawrence Tang }
4268f793ac3SLawrence Tang
4278f793ac3SLawrence Tang //Enables/disables debugging globally for the JSON validator.
validate_schema_debug_enable()428e407b4c8SLawrence Tang void validate_schema_debug_enable()
429e407b4c8SLawrence Tang {
430e407b4c8SLawrence Tang json_validator_debug = 1;
431e407b4c8SLawrence Tang }
validate_schema_debug_disable()432e407b4c8SLawrence Tang void validate_schema_debug_disable()
433e407b4c8SLawrence Tang {
434e407b4c8SLawrence Tang json_validator_debug = 0;
435e407b4c8SLawrence Tang }
4368f793ac3SLawrence Tang
4377cd13908SLawrence Tang //Logs an error message to the given error message location and (optionally) provides debug output.
log_validator_error(char * error_message,const char * format,...)4388f793ac3SLawrence Tang void log_validator_error(char *error_message, const char *format, ...)
4398f793ac3SLawrence Tang {
4408f793ac3SLawrence Tang va_list args;
4418f793ac3SLawrence Tang
4428f793ac3SLawrence Tang //Log error to error out.
4438f793ac3SLawrence Tang va_start(args, format);
4448f793ac3SLawrence Tang vsnprintf(error_message, JSON_ERROR_MSG_MAX_LEN, format, args);
4458f793ac3SLawrence Tang va_end(args);
4468f793ac3SLawrence Tang
4478f793ac3SLawrence Tang //Debug message if necessary.
4488f793ac3SLawrence Tang va_start(args, format);
4498f793ac3SLawrence Tang log_validator_msg(format, args);
4508f793ac3SLawrence Tang va_end(args);
4518f793ac3SLawrence Tang }
4528f793ac3SLawrence Tang
4537cd13908SLawrence Tang //Logs a debug message to stdout, if validator debug is enabled.
log_validator_debug(const char * format,...)4548f793ac3SLawrence Tang void log_validator_debug(const char *format, ...)
4558f793ac3SLawrence Tang {
4568f793ac3SLawrence Tang va_list args;
4578f793ac3SLawrence Tang va_start(args, format);
4588f793ac3SLawrence Tang log_validator_msg(format, args);
4598f793ac3SLawrence Tang va_end(args);
4608f793ac3SLawrence Tang }
4618f793ac3SLawrence Tang
4628f793ac3SLawrence Tang //Logs a single validator debug/error message.
log_validator_msg(const char * format,va_list args)4638f793ac3SLawrence Tang void log_validator_msg(const char *format, va_list args)
4648f793ac3SLawrence Tang {
4658f793ac3SLawrence Tang //Print debug output if debug is on.
466e407b4c8SLawrence Tang if (json_validator_debug) {
4678f793ac3SLawrence Tang //Make new format string for error.
4688f793ac3SLawrence Tang const char *header = "json_validator: ";
4698f793ac3SLawrence Tang char *new_format = malloc(strlen(header) + strlen(format) + 2);
4708f793ac3SLawrence Tang strcpy(new_format, header);
4718f793ac3SLawrence Tang strcat(new_format, format);
4728f793ac3SLawrence Tang strcat(new_format, "\n");
4738f793ac3SLawrence Tang
4748f793ac3SLawrence Tang //Print & free format.
4758f793ac3SLawrence Tang vfprintf(stdout, new_format, args);
4768f793ac3SLawrence Tang free(new_format);
4778f793ac3SLawrence Tang }
4788f793ac3SLawrence Tang }
479