allOf : Array<Schema>
allOf
Array<Schema>An instance validates successfully against this keyword if it validates successfully against all schemas defined by this keyword’s value.
Value |
This keyword must be set to a non-empty array, where each item is a valid JSON Schema
Hint: Use the jsonschema metaschema and jsonschema lint commands to catch keywords set to invalid values
|
---|---|
Kind | Applicator |
Applies To | Any |
Base Dialect | Draft 4 |
Changed In | None |
Introduced In | Draft 4 |
Vocabulary | Validation |
Specification | https://json-schema.org/draft-04/draft-fge-json-schema-validation-00#rfc.section.5.5.3 |
Metaschema | http://json-schema.org/draft-04/schema# |
Official Tests | draft4/allOf.json |
Default |
As if it was set to the (invalid) value:
[]
|
Annotation | None |
Affected By | None |
Affects | None |
Also See |
|
The allOf keyword restricts instances to validate against every given subschema. This keyword can be thought of as a logical conjunction (AND) operation, as instances are valid if they satisfy every constraint of every subschema (the intersection of the constraints).
Common Pitfall
Note that in JSON Schema Draft 7 and earlier
versions, any subschema declaring the $ref
keyword is considered to be a
reference object and any other sibling keyword will be silently ignored. To
avoid this, wrap subschemas with references that make use of other keywords
using the allOf
keyword.
Best Practice
This keyword typically has a single use case: combining one or more schemas through the use of (internal or external) references. If this is not the case, prefer elevating the keywords of every subschema to the outer schema and avoid using this keyword.
This keyword is equivalent to the &&
operator found in most programming
languages. For example:
bool valid = A && B && C;
As a reference, the following boolean truth table considers the evaluation result of this keyword given 3 subschemas: A, B, and C.
allOf |
Subschema A | Subschema B | Subschema C |
---|---|---|---|
Invalid | Invalid | Invalid | Invalid |
Invalid | Invalid | Invalid | Valid |
Invalid | Invalid | Valid | Invalid |
Invalid | Invalid | Valid | Valid |
Invalid | Valid | Invalid | Invalid |
Invalid | Valid | Invalid | Valid |
Invalid | Valid | Valid | Invalid |
Valid | Valid | Valid | Valid |
Examples
{
"$schema": "http://json-schema.org/draft-04/schema#",
"allOf": [
{ "$ref": "#/$defs/foo" },
{ "$ref": "#/$defs/bar" }
],
"$defs": {
"foo": { "type": "number" },
"bar": { "type": "integer" }
}
}
12345
3.14
"Hello World"
The allOf keyword restricts instances to validate against every given subschema. This keyword can be thought of as a logical conjunction (AND) operation, as instances are valid if they satisfy every constraint of every subschema (the intersection of the constraints).
Common Pitfall
Note that in JSON Schema Draft 7 and earlier
versions, any subschema declaring the $ref
keyword is considered to be a
reference object and any other sibling keyword will be silently ignored. To
avoid this, wrap subschemas with references that make use of other keywords
using the allOf
keyword.
Best Practice
This keyword typically has a single use case: combining one or more schemas through the use of (internal or external) references. If this is not the case, prefer elevating the keywords of every subschema to the outer schema and avoid using this keyword.
This keyword is equivalent to the &&
operator found in most programming
languages. For example:
bool valid = A && B && C;
As a reference, the following boolean truth table considers the evaluation result of this keyword given 3 subschemas: A, B, and C.
allOf |
Subschema A | Subschema B | Subschema C |
---|---|---|---|
Invalid | Invalid | Invalid | Invalid |
Invalid | Invalid | Invalid | Valid |
Invalid | Invalid | Valid | Invalid |
Invalid | Invalid | Valid | Valid |
Invalid | Valid | Invalid | Invalid |
Invalid | Valid | Invalid | Valid |
Invalid | Valid | Valid | Invalid |
Valid | Valid | Valid | Valid |
Examples
{
"$schema": "http://json-schema.org/draft-04/schema#",
"allOf": [
{ "$ref": "#/definitions/foo" },
{ "$ref": "#/definitions/bar" }
],
"definitions": {
"foo": { "type": "number" },
"bar": { "type": "integer" }
}
}
12345
3.14
"Hello World"