blob: c86ce4c19dba0bc9d0cebaddd390465270a7722d [file] [log] [blame]
Lawrence Tang8a2d7372022-07-12 16:44:49 +01001/**
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.
18int validate_field(const char* name, json_object* schema, json_object* object, char* error_message);
19int validate_integer(const char* field_name, json_object* schema, json_object* object, char* error_message);
20int validate_string(const char* field_name, json_object* schema, json_object* object, char* error_message);
21int validate_object(const char* field_name, json_object* schema, json_object* object, char* error_message);
22int 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.
26int 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.
52int 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.
86int 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
125 //Switch and validate each type in turn.
126 switch (json_object_get_type(object))
127 {
128 case json_type_int:
129 return validate_integer(field_name, schema, object, error_message);
130 case json_type_string:
131 return validate_string(field_name, schema, object, error_message);
132 case json_type_object:
133 return validate_object(field_name, schema, object, error_message);
134 case json_type_array:
135 return validate_object(field_name, schema, object, error_message);
136
137 //We don't perform extra validation on this type.
138 default:
139 return 1;
140 }
141}
142
143//Validates a single integer value according to the given specification.
144int validate_integer(const char* field_name, json_object* schema, json_object* object, char* error_message)
145{
146 //Is there a minimum/maximum specified? If so, check those.
147 //Validate minimum.
148 json_object* min_value = json_object_object_get(schema, "minimum");
149 if (min_value != NULL && json_object_is_type(min_value, json_type_int))
150 {
151 int min_value_int = json_object_get_int(min_value);
152 if (json_object_get_uint64(object) < min_value_int)
153 {
154 sprintf(error_message, "Failed to validate integer field '%s'. Value was below minimum of %d.", field_name, min_value_int);
155 return 0;
156 }
157 }
158
159 //Validate maximum.
160 json_object* max_value = json_object_object_get(schema, "maximum");
161 if (max_value != NULL && json_object_is_type(max_value, json_type_int))
162 {
163 int max_value_int = json_object_get_int(max_value);
164 if (json_object_get_uint64(object) > max_value_int)
165 {
166 sprintf(error_message, "Failed to validate integer field '%s'. Value was above maximum of %d.", field_name, max_value_int);
167 return 0;
168 }
169 }
170
171 return 1;
172}
173
174//Validates a single string value according to the given specification.
175int validate_string(const char* field_name, json_object* schema, json_object* object, char* error_message)
176{
177 //todo: if there is a "pattern" field, verify the string with RegEx.
178 return 1;
179}
180
181//Validates a single object value according to the given specification.
182int validate_object(const char* field_name, json_object* schema, json_object* object, char* error_message)
183{
184 //Are there a set of "required" fields? If so, check they all exist.
185 json_object* required_fields = json_object_object_get(schema, "required");
186 if (required_fields != NULL && json_object_get_type(required_fields) == json_type_array)
187 {
188 int len = json_object_array_length(required_fields);
189 for (int i=0; i<len; i++)
190 {
191 //Get the required field from schema.
192 json_object* required_field = json_object_array_get_idx(required_fields, i);
193 if (json_object_get_type(required_field) != json_type_string)
194 {
195 sprintf(error_message, "Required field for object '%s' is not a string (schema violation).", field_name);
196 return 0;
197 }
198
199 //Does it exist in the object?
200 const char* required_field_str = json_object_get_string(required_field);
201 if (json_object_object_get(object, required_field_str) == NULL)
202 {
203 sprintf(error_message, "Required field '%s' was not present in object '%s'.", required_field_str, field_name);
204 return 0;
205 }
206 }
207 }
208
209 //If the boolean field "additionalProperties" exists and is set to false, ensure there are no
210 //extra properties apart from those required in the object.
211 //... todo
212
213 //Run through the "properties" object and validate each of those in turn.
214 json_object* properties = json_object_object_get(schema, "properties");
215 if (properties != NULL && json_object_get_type(properties) == json_type_object)
216 {
217 json_object_object_foreach(properties, key, value) {
218
219 //If the given property name does not exist on the target object, ignore and continue next.
220 json_object* object_prop = json_object_object_get(object, key);
221 if (object_prop == NULL)
222 continue;
223
224 //Validate against the schema.
225 if (!validate_field(key, value, object_prop, error_message))
226 return 0;
227 }
228 }
229
230 return 1;
231}
232
233//Validates a single array value according to the given specification.
234int validate_array(const char* field_name, json_object* schema, json_object* object, char* error_message)
235{
236 return 1;
237}