Pike v8.0 release 1738

Class Standards.JSON.Validator

Description

An instance of this class can be used to validate a JSON object against a JSON schema.

Example

string schema_s = "{\n" + " \"name\": \"SomeExample\",\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"name\": { \"type\": \"string\" },\n" + " \"id\": {\n" + " \"type\": \"integer\",\n" + " \"minimum\": 0\n" + " }\n" + " }\n" + "}"; string example_s = "{\n" + " \"name\": \"An Example\",\n" + " \"id\": 17\n" + "}"; mixed schema = Standards.JSON.decode(schema_s); mixed example = Standards.JSON.decode(example_s); if (string error = Standards.JSON.Validator(schema).validate(example)) werror("Error: JSON string %O did not validate: %s\n", example_s, error); else write("JSON ok\n");

Note

This class validates only a subset of the JSON schema specification. Currently dependencies and references are not handled and regular expressions (for pattern properties) are limited to those that can be handled by Regexp.SimpleRegexp.

For more information of JSON schema look at http://json-schema.org/documentation.html "The home of JSON Schema".


Method create

Standards.JSON.Validator Standards.JSON.Validator(mixed _schema)

Description

Create a JSON validator for some JSON schema.

Parameter _schema

The JSON schema to use in validate(). This must be a valid JSON object.

Throws

Throws an error if the schema is invalid.