Overview
Keywords may exhibit one or more of three fundamental behaviors:- Assertions - Validate that an instance satisfies constraints
- Annotations - Attach information to instance locations
- Applicators - Apply subschemas and combine their results
Implementations should not add unspecified behaviors to keywords.
Assertions
Assertions validate that an instance satisfies constraints, producing a boolean result:true if the constraints are satisfied; false otherwise.
When an instance “validates against a keyword,” it means the keyword produces an assertion result of true if the instance satisfies the given constraint; otherwise an assertion result of false is produced.
Type-Specific Constraints
Most assertions only constrain values within a certain primitive type. When the instance type doesn’t match the type targeted by the keyword, the instance is considered to conform to the assertion. Example: ThemaxLength keyword only restricts strings that are too long. If the instance is a number, boolean, null, array, or object, it is valid against this assertion.
maxLength also restricted the instance type to be a string, this would be more cumbersome to express.
Failed Assertions
An instance can only fail an assertion that is present in the schema. JSON Schema implementations produce a single boolean result when evaluating an instance against schema assertions.Annotations
Annotations attach information to specific locations in an instance for application use. Unlike assertions, annotation data can take various forms and is provided to applications to use as they see fit.Annotation Collection
JSON Schema can annotate an instance with information whenever the instance validates against the schema object containing the annotation, and all of its parent schema objects. A collected annotation must include:- The name of the keyword that produces the annotation
- The instance location (as a JSON Pointer)
- The evaluation path
- The absolute schema location (as an IRI)
- The attached value(s)
Default Annotation Values
Unless otherwise specified, a keyword’s annotation value is the value of the keyword itself. However, other behaviors are possible. Example:Annotations and Failed Assertions
Example:"This is a string", the title annotation “Integer Value” is discarded because the type assertion fails. The title “String Value” is kept.
Short-Circuit Evaluation
While “short-circuit” evaluation is possible for assertions, collecting annotations requires examining all schemas that apply to an instance location, even if they cannot change the overall assertion result.The only exception is that subschemas of a schema object that has failed validation may be skipped, as annotations are not retained for failing schemas.
Applicators
Applicators apply subschemas to parts of the instance and combine or modify their results. They allow for building more complex schemas than can be accomplished with a single schema object.How Applicators Work
Applicators determine which additional schemas are applied during evaluation. Such schemas may be:- Present as subschemas comprising all or part of the keyword’s value
- Referenced from elsewhere in the same schema document
- Referenced from a different schema document
Assertion Behavior
Applicator keywords behave as assertions, using the assertion results of each subschema or referenced schema. These boolean results are:- Modified - e.g., the
notkeyword negates its subschema’s assertion - Combined - e.g.,
allOftakes the conjunction of its subschemas’ assertions
Annotation Preservation
Annotation results from subschemas are preserved so applications can decide how to interpret multiple values. Applicator keywords do not play a direct role in this preservation.Keyword Interactions
Unless otherwise specified, keywords act independently. However, keywords may modify their behavior based on the presence, absence, or value of another keyword in the same schema object.Dependency Types
Static dependencies - The dependency relies on the presence or contents of another keyword. Dynamic dependencies - The dependency relies on the evaluation of another keyword against an instance. This may depend on either:- Annotations produced by the keyword
- Validation result of its subschema(s)
Default Behaviors
A missing keyword:- Must not produce a false assertion result
- Must not produce annotation results
- Must not cause any other schema to be evaluated as part of its behavioral definition
Because annotation collection can add significant cost in computation and memory, implementations may opt out of this feature. Keywords specified in terms of collected annotations should describe reasonable alternate approaches when appropriate.