Validates a JSON file against a JSON schema.
Syntax |
Description |
---|---|
jsonvalidate(data, schema); |
Validates a JSON file against a JSON schema, which itself is validated against draft 7 of the standard. "data" and "schema" can be either a struct with the data or a filename. The schema uses standard conforming data and schemas. This command returns the "Validation" struct with the "generalMsg" and "valid" fields to indicate the validation result. |
Example
The following code is an example of validating the JSON file "json_schema_validation_port_data.json" against the JSON schema saved in the file "json_schema_validation_partial_schema_struct.json" (files attached). There are 5 tests in the script that generates different validation results.
?"Test 1 : Correct Json data and schema using filenames";
ValidationResult = jsonvalidate("json_schema_validation_port_data_original.json", "json_schema_validation_partial_schema_struct.json");
?ValidationResult.valid;
assert("Validation failed", ValidationResult.valid == 1.);
?"-----------------";
clear;
?"Test 2 : Correct Json data and schema using jsonread";
ValidationResult = jsonvalidate(jsonread("json_schema_validation_port_data_original.json"), jsonread("json_schema_validation_partial_schema_struct.json"));
?ValidationResult.valid;
assert("Validation failed", ValidationResult.valid == 1.);
?"-----------------";
clear;
? "Test 3 : Invalid data type - number instead of required string";
json_data = jsonread("json_schema_validation_port_data_original.json");
json_schema = jsonread("json_schema_validation_partial_schema_struct.json");
incorrect_data_value = 1.0;
json_data.port_list.connected_port_list.opt_1.name = incorrect_data_value;
jsonwrite("port_data_temp.json", json_data);
ValidationResult = jsonvalidate("port_data_temp.json", json_schema);
?ValidationResult.valid;
?ValidationResult.specificMsg;
?ValidationResult.value;
?ValidationResult.location;
assert("Validation should fail", ValidationResult.valid != 1.0);
assert("pos should be in the json file", str2num(ValidationResult.value) == incorrect_data_value);
assert("Found error in connected_port_list", ValidationResult.location == "/port_list/connected_port_list/opt_1/name");
?"-----------------";
clear;
? "Test 4: Invalid data - missing required properties";
json_data = jsonread("json_schema_validation_port_data_original.json");
json_data.port_list.connected_port_list.opt_1 = {"loc" : 0.3};
ValidationResult = jsonvalidate(json_data, "json_schema_validation_partial_schema_struct.json");
?ValidationResult.valid;
?ValidationResult.specificMsg;
?ValidationResult.value;
?ValidationResult.location;
assert("Validation should fail", ValidationResult.valid != 1.0);
assert("name should be in the json file", findstring(ValidationResult.specificMsg, "name"));
assert("Found error in connected_port_list", ValidationResult.location == "/port_list/connected_port_list/opt_1");
?"-----------------";
clear;
? "Test 5 : invalid schema input";
schema = 1;
try{jsonvalidate("json_schema_validation_port_data_original.json", schema);}catch(ErrMsg);
assert("schema should be string or struct", ErrMsg != "");
And the 5 tests generate the following test result messages based on different problems in the JSON file. Note that the 5 tests are for a successful validation, an invalid data type, missing fields, a successful validation, and an invalid schema to test against, respectively.
Test 1 : Correct Json data and schema using filenames
result:
1
-----------------
Test 2 : Correct Json data and schema using jsonread
result:
1
-----------------
Test 3 : Invalid data type - number instead of required string
Validation failed, see return value for more details
result:
0
unexpected instance type
1.0
/port_list/connected_port_list/opt_1/name
-----------------
Test 4: Invalid data - missing required properties
Validation failed, see return value for more details
result:
0
required property 'name' not found in object
{"loc":0.3}
/port_list/connected_port_list/opt_1
-----------------
Test 5 : invalid schema input
See Also
jsonload, jsonloads, jsonread, jsonreads, jsonsave, jsonsaves, jsonwrite, jsonerites