blob: 9c0ed36c4eebdae093b3ce3a1a93b16df995d44f [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>
Lawrence Tang8a2d7372022-07-12 16:44:49 +010015#include "json-schema.h"
16#include "edk/BaseTypes.h"
17
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);
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100102
Lawrence Tange407b4c2022-07-21 13:54:01 +0100103 //Change back to original CWD.
John Chungf8fc7052024-05-03 20:05:29 +0800104 if (chdir(original_cwd)) {
105 log_validator_error(error_message,
106 "Failed to chdir into original directory.");
107 }
108
Lawrence Tange407b4c2022-07-21 13:54:01 +0100109 free(original_cwd);
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100110
John Chungf8fc7052024-05-03 20:05:29 +0800111 if (result) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100112 log_validator_debug(
113 "Successfully validated the provided object against schema.");
John Chungf8fc7052024-05-03 20:05:29 +0800114 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100115 return result;
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100116}
117
118//Validates a single JSON field given a schema/object.
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100119//Returns -1 on fatal/error failure, 0 on validation failure, and 1 on validation.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100120int validate_field(const char *field_name, json_object *schema,
121 json_object *object, char *error_message)
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100122{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100123 log_validator_debug("Validating field '%s'...", field_name);
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100124
Lawrence Tange407b4c2022-07-21 13:54:01 +0100125 //If there is a "$ref" field, attempt to load the referenced schema.
126 json_object *ref_schema = json_object_object_get(schema, "$ref");
127 if (ref_schema != NULL &&
128 json_object_get_type(ref_schema) == json_type_string) {
129 log_validator_debug("$ref schema detected for field '%s'.",
130 field_name);
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100131
Lawrence Tange407b4c2022-07-21 13:54:01 +0100132 //Attempt to load. If loading fails, report error.
133 const char *ref_path = json_object_get_string(ref_schema);
John Chungf8fc7052024-05-03 20:05:29 +0800134 json_object *tmp = json_object_from_file(ref_path);
135 if (tmp == NULL) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100136 log_validator_error(
137 error_message,
138 "Failed to open referenced schema file '%s'.",
139 ref_path);
140 return -1;
141 }
John Chungf8fc7052024-05-03 20:05:29 +0800142 json_object_put(tmp);
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100143
Lawrence Tange407b4c2022-07-21 13:54:01 +0100144 log_validator_debug("loaded schema path '%s' for field '%s'.",
145 ref_path, field_name);
146 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100147
Lawrence Tange407b4c2022-07-21 13:54:01 +0100148 //Get the schema field type.
149 json_object *desired_field_type =
150 json_object_object_get(schema, "type");
151 if (desired_field_type == NULL ||
152 !json_object_is_type(desired_field_type, json_type_string)) {
153 log_validator_error(
154 error_message,
155 "Desired field type not provided within schema/is not a string for field '%s' (schema violation).",
156 field_name);
157 return -1;
158 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100159
Lawrence Tange407b4c2022-07-21 13:54:01 +0100160 //Check the field types are actually equal.
161 const char *desired_field_type_str =
162 json_object_get_string(desired_field_type);
163 if (!((!strcmp(desired_field_type_str, "object") &&
164 json_object_is_type(object, json_type_object)) ||
165 (!strcmp(desired_field_type_str, "array") &&
166 json_object_is_type(object, json_type_array)) ||
167 (!strcmp(desired_field_type_str, "integer") &&
168 json_object_is_type(object, json_type_int)) ||
169 (!strcmp(desired_field_type_str, "string") &&
170 json_object_is_type(object, json_type_string)) ||
171 (!strcmp(desired_field_type_str, "boolean") &&
172 json_object_is_type(object, json_type_boolean)) ||
173 (!strcmp(desired_field_type_str, "double") &&
174 json_object_is_type(object, json_type_double)))) {
175 log_validator_error(error_message,
176 "Field type match failed for field '%s'.",
177 field_name);
178 return 0;
179 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100180
Lawrence Tange407b4c2022-07-21 13:54:01 +0100181 //If the schema contains a "oneOf" array, we need to validate the field against each of the
182 //possible options in turn.
183 json_object *one_of = json_object_object_get(schema, "oneOf");
184 if (one_of != NULL && json_object_get_type(one_of) == json_type_array) {
185 log_validator_debug("oneOf options detected for field '%s'.",
186 field_name);
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100187
Lawrence Tange407b4c2022-07-21 13:54:01 +0100188 int len = json_object_array_length(one_of);
189 int validated = 0;
190 for (int i = 0; i < len; i++) {
191 //If the "oneOf" member isn't an object, warn on schema violation.
192 json_object *one_of_option =
193 json_object_array_get_idx(one_of, i);
194 if (one_of_option == NULL ||
195 json_object_get_type(one_of_option) !=
196 json_type_object) {
197 log_validator_debug(
198 "Schema Warning: 'oneOf' member for field '%s' is not an object, schema violation.",
199 field_name);
200 continue;
201 }
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100202
Lawrence Tange407b4c2022-07-21 13:54:01 +0100203 //Validate field with schema.
204 validated = validate_field(field_name, one_of_option,
205 object, error_message);
John Chungf8fc7052024-05-03 20:05:29 +0800206 if (validated == -1) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100207 return -1;
John Chungf8fc7052024-05-03 20:05:29 +0800208 }
209 if (validated) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100210 break;
John Chungf8fc7052024-05-03 20:05:29 +0800211 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100212 }
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100213
Lawrence Tange407b4c2022-07-21 13:54:01 +0100214 //Return if failed all checks.
215 if (!validated) {
216 log_validator_error(
217 error_message,
218 "No schema object structures matched provided object for field '%s'.",
219 field_name);
220 return 0;
221 }
222 }
Lawrence Tang45e04b02022-07-12 16:54:01 +0100223
Lawrence Tange407b4c2022-07-21 13:54:01 +0100224 //Switch and validate each type in turn.
225 switch (json_object_get_type(object)) {
226 case json_type_int:
227 return validate_integer(field_name, schema, object,
228 error_message);
229 case json_type_string:
230 return validate_string(field_name, schema, object,
231 error_message);
232 case json_type_object:
233 return validate_object(field_name, schema, object,
234 error_message);
235 case json_type_array:
236 return validate_array(field_name, schema, object,
237 error_message);
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100238
Lawrence Tange407b4c2022-07-21 13:54:01 +0100239 //We don't perform extra validation on this type.
240 default:
241 log_validator_debug(
242 "validation passed for '%s' (no extra validation).",
243 field_name);
244 return 1;
245 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100246}
247
248//Validates a single integer value according to the given specification.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100249int validate_integer(const char *field_name, json_object *schema,
250 json_object *object, char *error_message)
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100251{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100252 //Is there a minimum/maximum specified? If so, check those.
253 //Validate minimum.
254 json_object *min_value = json_object_object_get(schema, "minimum");
255 if (min_value != NULL &&
256 json_object_is_type(min_value, json_type_int)) {
257 int min_value_int = json_object_get_int(min_value);
John Chungf8fc7052024-05-03 20:05:29 +0800258 if (json_object_get_uint64(object) < (uint64_t)min_value_int) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100259 log_validator_error(
260 error_message,
261 "Failed to validate integer field '%s'. Value was below minimum of %d.",
262 field_name, min_value_int);
263 return 0;
264 }
265 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100266
Lawrence Tange407b4c2022-07-21 13:54:01 +0100267 //Validate maximum.
268 json_object *max_value = json_object_object_get(schema, "maximum");
269 if (max_value != NULL &&
270 json_object_is_type(max_value, json_type_int)) {
271 int max_value_int = json_object_get_int(max_value);
John Chungf8fc7052024-05-03 20:05:29 +0800272 if (json_object_get_uint64(object) > (uint64_t)max_value_int) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100273 log_validator_error(
274 error_message,
275 "Failed to validate integer field '%s'. Value was above maximum of %d.",
276 field_name, max_value_int);
277 return 0;
278 }
279 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100280
Lawrence Tange407b4c2022-07-21 13:54:01 +0100281 return 1;
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100282}
283
284//Validates a single string value according to the given specification.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100285int validate_string(const char *field_name, json_object *schema,
Ed Tanousb35d9572024-06-18 13:17:22 -0700286 json_object *object, const char *error_message)
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100287{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100288 //todo: if there is a "pattern" field, verify the string with RegEx.
John Chungf8fc7052024-05-03 20:05:29 +0800289 (void)field_name;
290 (void)schema;
291 (void)object;
292 (void)error_message;
293
Lawrence Tange407b4c2022-07-21 13:54:01 +0100294 return 1;
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100295}
296
297//Validates a single object value according to the given specification.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100298int validate_object(const char *field_name, json_object *schema,
299 json_object *object, char *error_message)
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100300{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100301 //Are there a set of "required" fields? If so, check they all exist.
302 json_object *required_fields =
303 json_object_object_get(schema, "required");
304 if (required_fields != NULL &&
305 json_object_get_type(required_fields) == json_type_array) {
306 log_validator_debug(
307 "Required fields found for '%s', matching...",
308 field_name);
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100309
Lawrence Tange407b4c2022-07-21 13:54:01 +0100310 int len = json_object_array_length(required_fields);
311 for (int i = 0; i < len; i++) {
312 //Get the required field from schema.
313 json_object *required_field =
314 json_object_array_get_idx(required_fields, i);
315 if (json_object_get_type(required_field) !=
316 json_type_string) {
317 log_validator_error(
318 error_message,
319 "Required field for object '%s' is not a string (schema violation).",
320 field_name);
321 return 0;
322 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100323
Lawrence Tange407b4c2022-07-21 13:54:01 +0100324 //Does it exist in the object?
325 const char *required_field_str =
326 json_object_get_string(required_field);
327 if (json_object_object_get(
328 object, required_field_str) == NULL) {
329 log_validator_error(
330 error_message,
331 "Required field '%s' was not present in object '%s'.",
332 required_field_str, field_name);
333 return 0;
334 }
335 }
336 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100337
Lawrence Tange407b4c2022-07-21 13:54:01 +0100338 //Get additional properties value in advance.
339 json_object *additional_properties =
340 json_object_object_get(schema, "additionalProperties");
341 int additional_properties_allowed = 0;
342 if (additional_properties != NULL &&
John Chungf8fc7052024-05-03 20:05:29 +0800343 json_object_get_type(additional_properties) == json_type_boolean) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100344 additional_properties_allowed =
345 json_object_get_boolean(additional_properties);
John Chungf8fc7052024-05-03 20:05:29 +0800346 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100347
Lawrence Tange407b4c2022-07-21 13:54:01 +0100348 //Run through the "properties" object and validate each of those in turn.
349 json_object *properties = json_object_object_get(schema, "properties");
350 if (properties != NULL &&
351 json_object_get_type(properties) == json_type_object) {
352 json_object_object_foreach(properties, key, value)
353 {
354 //If the given property name does not exist on the target object, ignore and continue next.
355 json_object *object_prop =
356 json_object_object_get(object, key);
John Chungf8fc7052024-05-03 20:05:29 +0800357 if (object_prop == NULL) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100358 continue;
John Chungf8fc7052024-05-03 20:05:29 +0800359 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100360
Lawrence Tange407b4c2022-07-21 13:54:01 +0100361 //Validate against the schema.
362 if (!validate_field(key, value, object_prop,
John Chungf8fc7052024-05-03 20:05:29 +0800363 error_message)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100364 return 0;
John Chungf8fc7052024-05-03 20:05:29 +0800365 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100366 }
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100367
Lawrence Tange407b4c2022-07-21 13:54:01 +0100368 //If additional properties are banned, validate that no additional properties exist.
369 if (!additional_properties_allowed) {
370 json_object_object_foreach(object, key, value)
371 {
John Chungf8fc7052024-05-03 20:05:29 +0800372 //Avoid compiler warning
373 (void)value;
374
Lawrence Tange407b4c2022-07-21 13:54:01 +0100375 //If the given property name does not exist on the schema object, fail validation.
John Chungf8fc7052024-05-03 20:05:29 +0800376 const json_object *schema_prop =
Lawrence Tange407b4c2022-07-21 13:54:01 +0100377 json_object_object_get(properties, key);
378 if (schema_prop == NULL) {
379 log_validator_error(
380 error_message,
381 "Invalid additional property '%s' detected on field '%s'.",
382 key, field_name);
383 return 0;
384 }
385 }
386 }
387 }
Lawrence Tangc4814592022-07-13 10:24:09 +0100388
Lawrence Tange407b4c2022-07-21 13:54:01 +0100389 return 1;
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100390}
391
392//Validates a single array value according to the given specification.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100393int validate_array(const char *field_name, json_object *schema,
394 json_object *object, char *error_message)
Lawrence Tang8a2d7372022-07-12 16:44:49 +0100395{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100396 //Iterate all items in the array, and validate according to the "items" schema.
397 json_object *items_schema = json_object_object_get(schema, "items");
398 if (items_schema != NULL &&
399 json_object_get_type(items_schema) == json_type_object) {
400 int array_len = json_object_array_length(object);
401 for (int i = 0; i < array_len; i++) {
402 if (!validate_field(field_name, items_schema,
403 json_object_array_get_idx(object,
404 i),
John Chungf8fc7052024-05-03 20:05:29 +0800405 error_message)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100406 return 0;
John Chungf8fc7052024-05-03 20:05:29 +0800407 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100408 }
409 }
Lawrence Tang45e04b02022-07-12 16:54:01 +0100410
Lawrence Tange407b4c2022-07-21 13:54:01 +0100411 return 1;
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100412}
413
414//Enables/disables debugging globally for the JSON validator.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100415void validate_schema_debug_enable()
416{
417 json_validator_debug = 1;
418}
419void validate_schema_debug_disable()
420{
421 json_validator_debug = 0;
422}
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100423
Lawrence Tang7cd13902022-07-13 16:59:25 +0100424//Logs an error message to the given error message location and (optionally) provides debug output.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100425void log_validator_error(char *error_message, const char *format, ...)
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100426{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100427 va_list args;
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100428
Lawrence Tange407b4c2022-07-21 13:54:01 +0100429 //Log error to error out.
430 va_start(args, format);
431 vsnprintf(error_message, JSON_ERROR_MSG_MAX_LEN, format, args);
432 va_end(args);
433
434 //Debug message if necessary.
435 va_start(args, format);
436 log_validator_msg(format, args);
437 va_end(args);
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100438}
439
Lawrence Tang7cd13902022-07-13 16:59:25 +0100440//Logs a debug message to stdout, if validator debug is enabled.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100441void log_validator_debug(const char *format, ...)
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100442{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100443 va_list args;
444 va_start(args, format);
445 log_validator_msg(format, args);
446 va_end(args);
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100447}
448
449//Logs a single validator debug/error message.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100450void log_validator_msg(const char *format, va_list args)
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100451{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100452 //Print debug output if debug is on.
453 if (json_validator_debug) {
454 //Make new format string for error.
455 const char *header = "json_validator: ";
456 char *new_format = malloc(strlen(header) + strlen(format) + 2);
457 strcpy(new_format, header);
458 strcat(new_format, format);
459 strcat(new_format, "\n");
Lawrence Tang8f793ac2022-07-13 10:17:09 +0100460
Lawrence Tange407b4c2022-07-21 13:54:01 +0100461 //Print & free format.
462 vfprintf(stdout, new_format, args);
463 free(new_format);
464 }
John Chungf8fc7052024-05-03 20:05:29 +0800465}