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