blob: 9eb60be78a3d6ae397258fa4828469076e1d5344 [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.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tang8a2d7372022-07-12 16:44:49 +01005 * 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 Tang8f793ac2022-07-13 10:17:09 +010013#include <stdarg.h>
Lawrence Tang5202bbb2022-08-12 14:54:36 +010014#include <json.h>
Thu Nguyene42fb482024-10-15 14:43:11 +000015#include <libcper/json-schema.h>
16#include <libcper/BaseTypes.h>
Lawrence Tang8a2d7372022-07-12 16:44:49 +010017
Lawrence Tang8f793ac2022-07-13 10:17:09 +010018//Field definitions.
19int json_validator_debug = 0;
20
Lawrence Tang8a2d7372022-07-12 16:44:49 +010021//Private pre-definitions.
Lawrence Tange407b4c2022-07-21 13:54:01 +010022int validate_field(const char *name, json_object *schema, json_object *object,
23 char *error_message);
24int validate_integer(const char *field_name, json_object *schema,
25 json_object *object, char *error_message);
26int validate_string(const char *field_name, json_object *schema,
Ed Tanousb35d9572024-06-18 13:17:22 -070027 json_object *object, const char *error_message);
Lawrence Tange407b4c2022-07-21 13:54:01 +010028int validate_object(const char *field_name, json_object *schema,
29 json_object *object, char *error_message);
30int validate_array(const char *field_name, json_object *schema,
31 json_object *object, char *error_message);
32void log_validator_error(char *error_message, const char *format, ...);
33void log_validator_debug(const char *format, ...);
34void log_validator_msg(const char *format, va_list args);
Lawrence Tang8a2d7372022-07-12 16:44:49 +010035
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 Tange407b4c2022-07-21 13:54:01 +010038int validate_schema_from_file(const char *schema_file, json_object *object,
39 char *error_message)
Lawrence Tang8a2d7372022-07-12 16:44:49 +010040{
Lawrence Tange407b4c2022-07-21 13:54:01 +010041 //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 Tang8a2d7372022-07-12 16:44:49 +010049
Lawrence Tange407b4c2022-07-21 13:54:01 +010050 //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 Tang8a2d7372022-07-12 16:44:49 +010054
Lawrence Tange407b4c2022-07-21 13:54:01 +010055 int result =
56 validate_schema(schema_ir, schema_dir, object, error_message);
Lawrence Tang8a2d7372022-07-12 16:44:49 +010057
Lawrence Tange407b4c2022-07-21 13:54:01 +010058 //Free memory from directory call.
59 free(schema_file_copy);
John Chungf8fc7052024-05-03 20:05:29 +080060 json_object_put(schema_ir);
Lawrence Tang8a2d7372022-07-12 16:44:49 +010061
Lawrence Tange407b4c2022-07-21 13:54:01 +010062 return result;
Lawrence Tang8a2d7372022-07-12 16:44:49 +010063}
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 Tange407b4c2022-07-21 13:54:01 +010068int validate_schema(json_object *schema, char *schema_directory,
69 json_object *object, char *error_message)
Lawrence Tang8a2d7372022-07-12 16:44:49 +010070{
Lawrence Tange407b4c2022-07-21 13:54:01 +010071 //Check that the schema version is the same as this validator.
72 json_object *schema_ver = json_object_object_get(schema, "$schema");
John Chungf8fc7052024-05-03 20:05:29 +080073 if (schema_ver == NULL || strcmp(json_object_get_string(schema_ver),
74 JSON_SCHEMA_VERSION) != 0) {
Lawrence Tange407b4c2022-07-21 13:54:01 +010075 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 Tang8a2d7372022-07-12 16:44:49 +010080
Lawrence Tange407b4c2022-07-21 13:54:01 +010081 //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 Chungf8fc7052024-05-03 20:05:29 +080086 if (original_cwd) {
87 free(original_cwd);
88 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010089 return 0;
90 }
91 if (chdir(schema_directory)) {
92 log_validator_error(error_message,
93 "Failed to chdir into schema directory.");
John Chungf8fc7052024-05-03 20:05:29 +080094 if (original_cwd) {
95 free(original_cwd);
96 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010097 return 0;
98 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +010099
Lawrence Tange407b4c2022-07-21 13:54:01 +0100100 //Parse the top level structure appropriately.
101 int result = validate_field("parent", schema, object, error_message);
Karthik Rajagopalan12587b22024-09-25 08:36:06 -0700102 if (result < 0) {
103 log_validator_error(error_message,
104 "Failed validating schema for parent.");
105 result = 0;
106 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100107
Lawrence Tange407b4c2022-07-21 13:54:01 +0100108 //Change back to original CWD.
John Chungf8fc7052024-05-03 20:05:29 +0800109 if (chdir(original_cwd)) {
110 log_validator_error(error_message,
111 "Failed to chdir into original directory.");
112 }
113
Lawrence Tange407b4c2022-07-21 13:54:01 +0100114 free(original_cwd);
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100115
John Chungf8fc7052024-05-03 20:05:29 +0800116 if (result) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100117 log_validator_debug(
118 "Successfully validated the provided object against schema.");
John Chungf8fc7052024-05-03 20:05:29 +0800119 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100120 return result;
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100121}
122
123//Validates a single JSON field given a schema/object.
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100124//Returns -1 on fatal/error failure, 0 on validation failure, and 1 on validation.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100125int validate_field(const char *field_name, json_object *schema,
126 json_object *object, char *error_message)
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100127{
Karthik Rajagopalan12587b22024-09-25 08:36:06 -0700128 int ret = -1;
129
Lawrence Tange407b4c2022-07-21 13:54:01 +0100130 log_validator_debug("Validating field '%s'...", field_name);
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100131
Lawrence Tange407b4c2022-07-21 13:54:01 +0100132 //If there is a "$ref" field, attempt to load the referenced schema.
Karthik Rajagopalan12587b22024-09-25 08:36:06 -0700133 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 Tange407b4c2022-07-21 13:54:01 +0100136 log_validator_debug("$ref schema detected for field '%s'.",
137 field_name);
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100138
Lawrence Tange407b4c2022-07-21 13:54:01 +0100139 //Attempt to load. If loading fails, report error.
Karthik Rajagopalan12587b22024-09-25 08:36:06 -0700140 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 Tange407b4c2022-07-21 13:54:01 +0100143 log_validator_error(
144 error_message,
145 "Failed to open referenced schema file '%s'.",
146 ref_path);
147 return -1;
148 }
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100149
Lawrence Tange407b4c2022-07-21 13:54:01 +0100150 log_validator_debug("loaded schema path '%s' for field '%s'.",
151 ref_path, field_name);
Karthik Rajagopalan12587b22024-09-25 08:36:06 -0700152
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 Tange407b4c2022-07-21 13:54:01 +0100159 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100160
Lawrence Tange407b4c2022-07-21 13:54:01 +0100161 //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 Rajagopalan12587b22024-09-25 08:36:06 -0700170 return 0;
Lawrence Tange407b4c2022-07-21 13:54:01 +0100171 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100172
Lawrence Tange407b4c2022-07-21 13:54:01 +0100173 //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 Tang8a2d7372022-07-12 16:44:49 +0100193
Lawrence Tange407b4c2022-07-21 13:54:01 +0100194 //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 Tang8f793ac2022-07-13 10:17:09 +0100200
Lawrence Tange407b4c2022-07-21 13:54:01 +0100201 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 Tang8f793ac2022-07-13 10:17:09 +0100215
Lawrence Tange407b4c2022-07-21 13:54:01 +0100216 //Validate field with schema.
217 validated = validate_field(field_name, one_of_option,
218 object, error_message);
John Chungf8fc7052024-05-03 20:05:29 +0800219 if (validated == -1) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100220 return -1;
John Chungf8fc7052024-05-03 20:05:29 +0800221 }
222 if (validated) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100223 break;
John Chungf8fc7052024-05-03 20:05:29 +0800224 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100225 }
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100226
Lawrence Tange407b4c2022-07-21 13:54:01 +0100227 //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 Tang45e04b02022-07-12 16:54:01 +0100236
Lawrence Tange407b4c2022-07-21 13:54:01 +0100237 //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 Tang8a2d7372022-07-12 16:44:49 +0100251
Lawrence Tange407b4c2022-07-21 13:54:01 +0100252 //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 Tang8a2d7372022-07-12 16:44:49 +0100259}
260
261//Validates a single integer value according to the given specification.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100262int validate_integer(const char *field_name, json_object *schema,
263 json_object *object, char *error_message)
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100264{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100265 //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 Chungf8fc7052024-05-03 20:05:29 +0800271 if (json_object_get_uint64(object) < (uint64_t)min_value_int) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100272 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 Tang8a2d7372022-07-12 16:44:49 +0100279
Lawrence Tange407b4c2022-07-21 13:54:01 +0100280 //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 Chungf8fc7052024-05-03 20:05:29 +0800285 if (json_object_get_uint64(object) > (uint64_t)max_value_int) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100286 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 Tang8a2d7372022-07-12 16:44:49 +0100293
Lawrence Tange407b4c2022-07-21 13:54:01 +0100294 return 1;
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100295}
296
297//Validates a single string value according to the given specification.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100298int validate_string(const char *field_name, json_object *schema,
Ed Tanousb35d9572024-06-18 13:17:22 -0700299 json_object *object, const char *error_message)
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100300{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100301 //todo: if there is a "pattern" field, verify the string with RegEx.
John Chungf8fc7052024-05-03 20:05:29 +0800302 (void)field_name;
303 (void)schema;
304 (void)object;
305 (void)error_message;
306
Lawrence Tange407b4c2022-07-21 13:54:01 +0100307 return 1;
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100308}
309
310//Validates a single object value according to the given specification.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100311int validate_object(const char *field_name, json_object *schema,
312 json_object *object, char *error_message)
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100313{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100314 //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 Tang8f793ac2022-07-13 10:17:09 +0100322
Lawrence Tange407b4c2022-07-21 13:54:01 +0100323 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 Tang8a2d7372022-07-12 16:44:49 +0100336
Lawrence Tange407b4c2022-07-21 13:54:01 +0100337 //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 Tang8a2d7372022-07-12 16:44:49 +0100350
Lawrence Tange407b4c2022-07-21 13:54:01 +0100351 //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 Chungf8fc7052024-05-03 20:05:29 +0800356 json_object_get_type(additional_properties) == json_type_boolean) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100357 additional_properties_allowed =
358 json_object_get_boolean(additional_properties);
John Chungf8fc7052024-05-03 20:05:29 +0800359 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100360
Lawrence Tange407b4c2022-07-21 13:54:01 +0100361 //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 Chungf8fc7052024-05-03 20:05:29 +0800370 if (object_prop == NULL) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100371 continue;
John Chungf8fc7052024-05-03 20:05:29 +0800372 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100373
Lawrence Tange407b4c2022-07-21 13:54:01 +0100374 //Validate against the schema.
375 if (!validate_field(key, value, object_prop,
John Chungf8fc7052024-05-03 20:05:29 +0800376 error_message)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100377 return 0;
John Chungf8fc7052024-05-03 20:05:29 +0800378 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100379 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100380
Lawrence Tange407b4c2022-07-21 13:54:01 +0100381 //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 Chungf8fc7052024-05-03 20:05:29 +0800385 //Avoid compiler warning
386 (void)value;
387
Lawrence Tange407b4c2022-07-21 13:54:01 +0100388 //If the given property name does not exist on the schema object, fail validation.
John Chungf8fc7052024-05-03 20:05:29 +0800389 const json_object *schema_prop =
Lawrence Tange407b4c2022-07-21 13:54:01 +0100390 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 Tangc4814592022-07-13 10:24:09 +0100401
Lawrence Tange407b4c2022-07-21 13:54:01 +0100402 return 1;
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100403}
404
405//Validates a single array value according to the given specification.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100406int validate_array(const char *field_name, json_object *schema,
407 json_object *object, char *error_message)
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100408{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100409 //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 Chungf8fc7052024-05-03 20:05:29 +0800418 error_message)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100419 return 0;
John Chungf8fc7052024-05-03 20:05:29 +0800420 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100421 }
422 }
Lawrence Tang45e04b02022-07-12 16:54:01 +0100423
Lawrence Tange407b4c2022-07-21 13:54:01 +0100424 return 1;
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100425}
426
427//Enables/disables debugging globally for the JSON validator.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100428void validate_schema_debug_enable()
429{
430 json_validator_debug = 1;
431}
432void validate_schema_debug_disable()
433{
434 json_validator_debug = 0;
435}
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100436
Lawrence Tang7cd13902022-07-13 16:59:25 +0100437//Logs an error message to the given error message location and (optionally) provides debug output.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100438void log_validator_error(char *error_message, const char *format, ...)
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100439{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100440 va_list args;
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100441
Lawrence Tange407b4c2022-07-21 13:54:01 +0100442 //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 Tang8f793ac2022-07-13 10:17:09 +0100451}
452
Lawrence Tang7cd13902022-07-13 16:59:25 +0100453//Logs a debug message to stdout, if validator debug is enabled.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100454void log_validator_debug(const char *format, ...)
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100455{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100456 va_list args;
457 va_start(args, format);
458 log_validator_msg(format, args);
459 va_end(args);
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100460}
461
462//Logs a single validator debug/error message.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100463void log_validator_msg(const char *format, va_list args)
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100464{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100465 //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 Tang8f793ac2022-07-13 10:17:09 +0100473
Lawrence Tange407b4c2022-07-21 13:54:01 +0100474 //Print & free format.
475 vfprintf(stdout, new_format, args);
476 free(new_format);
477 }
John Chungf8fc7052024-05-03 20:05:29 +0800478}