Schema

Schema

Class representing the definition and assertion methods for JSON Schema validation. Creates an immutable instance of a JSON Schema either immediately or lazily depending on your needs. When assigning a JSON Schema, it first validates the JSON Schema definition. Then it creates optimized assertion methods for each verified JSON Schema defined either at the root of the Schema or nested within complex Schemas. This allows for faster validations and the ability to perform partial Schema validations for nested definitions to test a change to a Model.

There are many ways to create a Schema instance, either instantly or lazily. The Schema class also supports fetching remote referenced JSON Schemas on a supported web client or Node.js service. Be mindful of the argument order, if omitting schema and/or refs, the desired arguments need to maintain the order in which they are defined.

Constructor

(async) new Schema(schemaopt, refsopt, asyncopt)

Properties:
Name Type Default Description
errors Array.<string> A copy of the List of error strings from the last time validate ran.
isAsync Boolean false A copy of the async validation setting.
Parameters:
Name Type Attributes Default Description
schema Object <optional>
Optional JSON Schema definition.
refs Object <optional>
Optional hash of cached JSON Schemas that are referenced in the main schema.
async Boolean <optional>
false Optional boolean flag to enable asynchronous validations.

Methods

(async) assign(schema, refsopt) → {Schema}

Method used to define and optimize the JSON Schema instance with the supplied JSON Schema definition and optionially cached references of other JSON Schema definitions.
Parameters:
Name Type Attributes Description
schema Object The supplied JSON Schema definition.
refs Object <optional>
Optionally supplied cached references of other JSON Schema definitions.
Returns:
Type:
Schema
The instance of the JSON Schema definition.

(async) validate(data, schemaopt) → {Boolean}

Method used to validate supplied data against the JSON Schema definition instance. Can be configured to be either synchronous or asynchronous using a wrapping function during construction. It defaults to synchronous for better performance.
Parameters:
Name Type Attributes Default Description
data The data to validate against the JSON Schema definition instance.
schema Schema <optional>
this Optionally pass nested JSON Schema definitions of the instance for partial schema validation or other instances of the JSON Schema class.
Returns:
Type:
Boolean
true if validation is successful, otherwise false.