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> |
| 13 | #include "json.h" |
| 14 | #include "json-schema.h" |
| 15 | #include "edk/BaseTypes.h" |
| 16 | |
| 17 | //Private pre-definitions. |
| 18 | int validate_field(const char* name, json_object* schema, json_object* object, char* error_message); |
| 19 | int validate_integer(const char* field_name, json_object* schema, json_object* object, char* error_message); |
| 20 | int validate_string(const char* field_name, json_object* schema, json_object* object, char* error_message); |
| 21 | int validate_object(const char* field_name, json_object* schema, json_object* object, char* error_message); |
| 22 | int validate_array(const char* field_name, json_object* schema, json_object* object, char* error_message); |
| 23 | |
| 24 | //Validates a single JSON object against a provided schema file, returning 1 on success and 0 on failure to validate. |
| 25 | //Error message space must be allocated prior to call. |
| 26 | int validate_schema_from_file(const char* schema_file, json_object* object, char* error_message) |
| 27 | { |
| 28 | //Load schema IR from file. |
| 29 | json_object* schema_ir = json_object_from_file(schema_file); |
| 30 | if (schema_ir == NULL) |
| 31 | { |
| 32 | sprintf(error_message, "Failed to load schema from file '%s'.", schema_file); |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | //Get the directory of the file. |
| 37 | char* schema_file_copy = malloc(strlen(schema_file) + 1); |
| 38 | strcpy(schema_file_copy, schema_file); |
| 39 | char* schema_dir = dirname(schema_file_copy); |
| 40 | |
| 41 | int result = validate_schema(schema_ir, schema_dir, object, error_message); |
| 42 | |
| 43 | //Free memory from directory call. |
| 44 | free(schema_file_copy); |
| 45 | |
| 46 | return result; |
| 47 | } |
| 48 | |
| 49 | //Validates a single JSON object against a provided schema, returning 1 on success and 0 on failure to validate. |
| 50 | //Error message space must be allocated prior to call. |
| 51 | //If the schema does not include any other sub-schemas using "$ref", then leaving schema_directory as NULL is valid. |
| 52 | int validate_schema(json_object* schema, char* schema_directory, json_object* object, char* error_message) |
| 53 | { |
| 54 | //Check that the schema version is the same as this validator. |
| 55 | json_object* schema_ver = json_object_object_get(schema, "$schema"); |
| 56 | if (schema_ver == NULL || strcmp(json_object_get_string(schema_ver), JSON_SCHEMA_VERSION)) |
| 57 | { |
| 58 | sprintf(error_message, "Provided schema is not of the same version that is referenced by this validator, or is not a schema."); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | //Change current directory into the schema directory. |
| 63 | char* original_cwd = malloc(PATH_MAX); |
| 64 | if (getcwd(original_cwd, PATH_MAX) == NULL) |
| 65 | { |
| 66 | sprintf(error_message, "Failed fetching the current directory."); |
| 67 | return 0; |
| 68 | } |
| 69 | if (chdir(schema_directory)) |
| 70 | { |
| 71 | sprintf(error_message, "Failed to chdir into schema directory."); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | //Parse the top level structure appropriately. |
| 76 | int result = validate_field("parent", schema, object, error_message); |
| 77 | |
| 78 | //Change back to original CWD. |
| 79 | chdir(original_cwd); |
| 80 | free(original_cwd); |
| 81 | |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | //Validates a single JSON field given a schema/object. |
| 86 | int validate_field(const char* field_name, json_object* schema, json_object* object, char* error_message) |
| 87 | { |
| 88 | //If there is a "$ref" field, attempt to load the referenced schema. |
| 89 | json_object* ref_schema = json_object_object_get(schema, "$ref"); |
| 90 | if (ref_schema != NULL && json_object_get_type(ref_schema) == json_type_string) |
| 91 | { |
| 92 | //Attempt to load. If loading fails, report error. |
| 93 | const char* ref_path = json_object_get_string(ref_schema); |
| 94 | schema = json_object_from_file(ref_path); |
| 95 | if (schema == NULL) |
| 96 | { |
| 97 | sprintf(error_message, "Failed to open referenced schema file '%s'.", ref_path); |
| 98 | return 0; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | //Get the schema field type. |
| 103 | json_object* desired_field_type = json_object_object_get(schema, "type"); |
| 104 | if (desired_field_type == NULL || !json_object_is_type(desired_field_type, json_type_string)) |
| 105 | { |
| 106 | sprintf(error_message, "Desired field type not provided within schema/is not a string for field '%s' (schema violation).", field_name); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | //Check the field types are actually equal. |
| 111 | const char* desired_field_type_str = json_object_get_string(desired_field_type); |
| 112 | if (!( |
| 113 | (!strcmp(desired_field_type_str, "object") && json_object_is_type(object, json_type_object)) |
| 114 | || (!strcmp(desired_field_type_str, "array") && json_object_is_type(object, json_type_array)) |
| 115 | || (!strcmp(desired_field_type_str, "integer") && json_object_is_type(object, json_type_int)) |
| 116 | || (!strcmp(desired_field_type_str, "string") && json_object_is_type(object, json_type_string)) |
| 117 | || (!strcmp(desired_field_type_str, "boolean") && json_object_is_type(object, json_type_boolean)) |
| 118 | || (!strcmp(desired_field_type_str, "double") && json_object_is_type(object, json_type_double)) |
| 119 | )) |
| 120 | { |
| 121 | sprintf(error_message, "Field type match failed for field '%s'.", field_name); |
| 122 | return 0; |
| 123 | } |
| 124 | |
Lawrence Tang | 45e04b0 | 2022-07-12 16:54:01 +0100 | [diff] [blame^] | 125 | //todo: support oneOf |
| 126 | |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 127 | //Switch and validate each type in turn. |
| 128 | switch (json_object_get_type(object)) |
| 129 | { |
| 130 | case json_type_int: |
| 131 | return validate_integer(field_name, schema, object, error_message); |
| 132 | case json_type_string: |
| 133 | return validate_string(field_name, schema, object, error_message); |
| 134 | case json_type_object: |
| 135 | return validate_object(field_name, schema, object, error_message); |
| 136 | case json_type_array: |
| 137 | return validate_object(field_name, schema, object, error_message); |
| 138 | |
| 139 | //We don't perform extra validation on this type. |
| 140 | default: |
| 141 | return 1; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | //Validates a single integer value according to the given specification. |
| 146 | int validate_integer(const char* field_name, json_object* schema, json_object* object, char* error_message) |
| 147 | { |
| 148 | //Is there a minimum/maximum specified? If so, check those. |
| 149 | //Validate minimum. |
| 150 | json_object* min_value = json_object_object_get(schema, "minimum"); |
| 151 | if (min_value != NULL && json_object_is_type(min_value, json_type_int)) |
| 152 | { |
| 153 | int min_value_int = json_object_get_int(min_value); |
| 154 | if (json_object_get_uint64(object) < min_value_int) |
| 155 | { |
| 156 | sprintf(error_message, "Failed to validate integer field '%s'. Value was below minimum of %d.", field_name, min_value_int); |
| 157 | return 0; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | //Validate maximum. |
| 162 | json_object* max_value = json_object_object_get(schema, "maximum"); |
| 163 | if (max_value != NULL && json_object_is_type(max_value, json_type_int)) |
| 164 | { |
| 165 | int max_value_int = json_object_get_int(max_value); |
| 166 | if (json_object_get_uint64(object) > max_value_int) |
| 167 | { |
| 168 | sprintf(error_message, "Failed to validate integer field '%s'. Value was above maximum of %d.", field_name, max_value_int); |
| 169 | return 0; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return 1; |
| 174 | } |
| 175 | |
| 176 | //Validates a single string value according to the given specification. |
| 177 | int validate_string(const char* field_name, json_object* schema, json_object* object, char* error_message) |
| 178 | { |
| 179 | //todo: if there is a "pattern" field, verify the string with RegEx. |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | //Validates a single object value according to the given specification. |
| 184 | int validate_object(const char* field_name, json_object* schema, json_object* object, char* error_message) |
| 185 | { |
| 186 | //Are there a set of "required" fields? If so, check they all exist. |
| 187 | json_object* required_fields = json_object_object_get(schema, "required"); |
| 188 | if (required_fields != NULL && json_object_get_type(required_fields) == json_type_array) |
| 189 | { |
| 190 | int len = json_object_array_length(required_fields); |
| 191 | for (int i=0; i<len; i++) |
| 192 | { |
| 193 | //Get the required field from schema. |
| 194 | json_object* required_field = json_object_array_get_idx(required_fields, i); |
| 195 | if (json_object_get_type(required_field) != json_type_string) |
| 196 | { |
| 197 | sprintf(error_message, "Required field for object '%s' is not a string (schema violation).", field_name); |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | //Does it exist in the object? |
| 202 | const char* required_field_str = json_object_get_string(required_field); |
| 203 | if (json_object_object_get(object, required_field_str) == NULL) |
| 204 | { |
| 205 | sprintf(error_message, "Required field '%s' was not present in object '%s'.", required_field_str, field_name); |
| 206 | return 0; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | //If the boolean field "additionalProperties" exists and is set to false, ensure there are no |
| 212 | //extra properties apart from those required in the object. |
| 213 | //... todo |
| 214 | |
| 215 | //Run through the "properties" object and validate each of those in turn. |
| 216 | json_object* properties = json_object_object_get(schema, "properties"); |
| 217 | if (properties != NULL && json_object_get_type(properties) == json_type_object) |
| 218 | { |
| 219 | json_object_object_foreach(properties, key, value) { |
| 220 | |
| 221 | //If the given property name does not exist on the target object, ignore and continue next. |
| 222 | json_object* object_prop = json_object_object_get(object, key); |
| 223 | if (object_prop == NULL) |
| 224 | continue; |
| 225 | |
| 226 | //Validate against the schema. |
| 227 | if (!validate_field(key, value, object_prop, error_message)) |
| 228 | return 0; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | //Validates a single array value according to the given specification. |
| 236 | int validate_array(const char* field_name, json_object* schema, json_object* object, char* error_message) |
| 237 | { |
Lawrence Tang | 45e04b0 | 2022-07-12 16:54:01 +0100 | [diff] [blame^] | 238 | //Iterate all items in the array, and validate according to the "items" schema. |
| 239 | json_object* items_schema = json_object_object_get(schema, "items"); |
| 240 | if (items_schema != NULL && json_object_get_type(items_schema) == json_type_object) |
| 241 | { |
| 242 | int array_len = json_object_array_length(object); |
| 243 | for (int i=0; i<array_len; i++) |
| 244 | { |
| 245 | if (!validate_field(field_name, items_schema, json_object_array_get_idx(object, i), error_message)) |
| 246 | return 0; |
| 247 | } |
| 248 | } |
| 249 | |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 250 | return 1; |
| 251 | } |