JSON Schema - Boolean JSON Schema combination (original) (raw)

Introduction

JSON Schema includes a few keywords for combining schemas together. Note that this doesn't necessarily mean combining schemas from multiple files or JSON trees. To learn how to combine reusable JSON Schemas, see Modular JSON Schema combination.

Here you will learn to combine schemas by allowing a value to be validated against multiple criteria at the same time. This task requires using keywords that correspond to well known boolean algebra concepts like AND, OR, XOR, and NOT. The keywords used to combine schemas are the following:

All of these keywords must be set to an array, where each item is a schema. Be careful with recursive schemas as they can exponentially increase processing times.

In the following sections you will learn to use these keywords to express complex constraints that can't otherwise be expressed with standard JSON Schema keywords.

allOf

To validate against allOf, the given data must be valid against all of the given subschemas.

allOf can not be used to "extend" a schema to add more details to it in the sense of object-oriented inheritance. Instancesmust independently be valid against "all of" the schemas in theallOf. See the section on Extending Closed Schemas for more information.

anyOf

To validate against anyOf, the given data must be valid against any (one or more) of the given subschemas.

oneOf

To validate against oneOf, the given data must be valid against exactly one of the given subschemas.

Not a multiple of either 5 or 3.

Multiple of both 5 and 3 is rejected.

info yellow

Careful consideration should be taken when using oneOf entries as the nature of it requires verification of every sub-schema which can lead to increased processing times. Prefer anyOf where possible.

not

The not keyword declares that an instance validates if it doesn't validate against the given subschema.

For example, the following schema validates against anything that is not a string:

Properties of Schema Composition

Illogical Schemas

Note that it's quite easy to create schemas that are logical impossibilities with these keywords. The following example creates a schema that won't validate against anything (since something may not be both a string and a number at the same time):

Factoring Schemas

Note that it's possible to "factor" out the common parts of the subschemas. The following two schemas are equivalent.

Need Help?

Did you find these docs helpful?