Validates a JSON file against a JSON schema.
Syntax |
Description |
---|---|
jsonvalidate(data, schema); |
Validates a JSON file against a JSON schema. "data" and "schema" can be either a struct with the data or a filename. The schema must have the $schema on the first level. This command returns the "Validation" struct with the "generalMsg" field 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 schema";
test_json = "json_schema_validation_port_data.json";
jsonload("json_schema_validation_partial_schema_struct.json");
jsonload(test_json);
port_data = {"port_list":port_list};
jsonvalidate(port_data,schema);
?Validation.generalMsg;
assert("Validation failed", Validation.generalMsg == "Validation successful");
?"-----------------";
? "Test 2 : Invalid data type";
assgined_value = 1;
port_list.connected_port_list.opt_1.name = assgined_value;
jsonsave("port_data_temp.json", port_list);
jsonload("port_data_temp.json");
port_data = {"port_list":port_list};
jsonvalidate(port_data,schema);
?Validation.generalMsg;
?Validation.specificMsg;
?Validation.value;
?Validation.location;
assert("Validation should fail", Validation.generalMsg != "Validation successful");
assert("pos should be in the json file", Validation.value == num2str(assgined_value));
assert("Found error in connected_port_list", Validation.location == "/port_list/connected_port_list/opt_1/name");
?"-----------------";
? "Test 3: missing fields";
jsonload(test_json);
port_list.connected_port_list.opt_1 = {"loc" : 0.3};
jsonsave("port_data_temp.json", port_list);
jsonload("port_data_temp.json");
port_data = {"port_list":port_list};
jsonvalidate(port_data,schema);
?Validation.generalMsg;
?Validation.specificMsg;
?Validation.value;
?Validation.location;
assert("Validation should fail", Validation.generalMsg != "Validation successful");
assert("name should be in the json file", findstring(Validation.specificMsg, "name"));
assert("Found error in connected_port_list", Validation.location == "/port_list/connected_port_list/opt_1");
?"-----------------";
? "Test 4 : include $schema";
schema = {"$schema" : "something"};
jsonsave("schema_temp.json", schema);
jsonload("schema_temp.json");
try{jsonvalidate(port_data,schema);}catch(ErrMsg);
assert("schema should be string or struct", ErrMsg == "");
?Validation.generalMsg;
?"-----------------";
? "Test 5 : invalid schema";
schema = 1;
jsonsave("schema_temp.json", schema);
jsonload("schema_temp.json");
try{jsonvalidate(port_data,schema);}catch(ErrMsg);
assert("schema should be string or struct", ErrMsg != "");
?Validation.generalMsg;
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 schema
Validation successful
-----------------
Test 2 : Invalid data type
at least one subschema has failed, but all of them are required to validate
unexpected instance type
1
/port_list/connected_port_list/opt_1/name
-----------------
Test 3: missing fields
at least one subschema has failed, but all of them are required to validate
required property 'name' not found in object
{"loc":0.3}
/port_list/connected_port_list/opt_1
-----------------
Test 4 : include $schema
Validation successful
-----------------
Test 5 : invalid schema
See Also
jsonload, jsonloads, jsonread, jsonreads, jsonsave, jsonsaves, jsonwrite, jsonerites