@@ -27,3 +27,126 @@ related:
2727 - vocabulary : applicator
2828 keyword : not
2929---
30+
31+ The {{<link keyword =" allOf " vocabulary =" applicator " >}} keyword restricts
32+ instances to validate against _ every_ given subschema. This keyword can be
33+ thought of as a [ logical
34+ conjunction] ( https://en.wikipedia.org/wiki/Logical_conjunction ) (AND)
35+ operation, as instances are valid if they satisfy every constraint of every
36+ subschema (the intersection of the constraints).
37+
38+ {{<common-pitfall >}} Wrapping a single instance of the [ ` $ref ` ] ( ../../core/ref )
39+ or [ ` $recursiveRef ` ] ( ../../core/recursiveref ) keyword in an ` allOf ` operator is
40+ an anti-pattern.
41+
42+ This practice has historical roots. In JSON Schema [ Draft 7] ( /draft7 ) and
43+ earlier versions, any subschema declaring the ` $ref ` keyword was considered to
44+ be a _ reference object_ and any other sibling keyword was silently ignored. As
45+ a consequence, subschemas with references that made use of other keywords had
46+ to artificially wrap the reference into its own subschema.
47+ {{</common-pitfall >}}
48+
49+ {{<best-practice >}}This keyword typically has a single use case: combining
50+ _ multiple_ schemas through the use of (internal or external) references. If
51+ this is not the case, prefer elevating the keywords of every subschema to the
52+ outer schema and avoid using this keyword. {{</best-practice >}}
53+
54+ This keyword is equivalent to the ` && ` operator found in most programming
55+ languages. For example:
56+
57+ ``` c
58+ bool valid = A && B && C;
59+ ```
60+
61+ As a reference, the following boolean [ truth
62+ table] ( https://en.wikipedia.org/wiki/Truth_table ) considers the evaluation
63+ result of this keyword given 3 subschemas: A, B, and C.
64+
65+ <table class =" table table-borderless border " >
66+ <thead >
67+ <tr class="table-light">
68+ <th><code>allOf</code></th>
69+ <th>Subschema A</th>
70+ <th>Subschema B</th>
71+ <th>Subschema C</th>
72+ </tr>
73+ </thead >
74+ <tbody >
75+ <tr class="table-danger">
76+ <td class="fw-bold"><i class="bi bi-x-circle-fill me-1"></i> Invalid</td>
77+ <td><i class="bi bi-x-circle"></i> Invalid</td>
78+ <td><i class="bi bi-x-circle"></i> Invalid</td>
79+ <td><i class="bi bi-x-circle"></i> Invalid</td>
80+ </tr>
81+ <tr class="table-danger">
82+ <td class="fw-bold"><i class="bi bi-x-circle-fill me-1"></i> Invalid</td>
83+ <td><i class="bi bi-x-circle"></i> Invalid</td>
84+ <td><i class="bi bi-x-circle"></i> Invalid</td>
85+ <td><i class="bi bi-check-circle"></i> Valid</td>
86+ </tr>
87+ <tr class="table-danger">
88+ <td class="fw-bold"><i class="bi bi-x-circle-fill me-1"></i> Invalid</td>
89+ <td><i class="bi bi-x-circle"></i> Invalid</td>
90+ <td><i class="bi bi-check-circle"></i> Valid</td>
91+ <td><i class="bi bi-x-circle"></i> Invalid</td>
92+ </tr>
93+ <tr class="table-danger">
94+ <td class="fw-bold"><i class="bi bi-x-circle-fill me-1"></i> Invalid</td>
95+ <td><i class="bi bi-x-circle"></i> Invalid</td>
96+ <td><i class="bi bi-check-circle"></i> Valid</td>
97+ <td><i class="bi bi-check-circle"></i> Valid</td>
98+ </tr>
99+ <tr class="table-danger">
100+ <td class="fw-bold"><i class="bi bi-x-circle-fill me-1"></i> Invalid</td>
101+ <td><i class="bi bi-check-circle"></i> Valid</td>
102+ <td><i class="bi bi-x-circle"></i> Invalid</td>
103+ <td><i class="bi bi-x-circle"></i> Invalid</td>
104+ </tr>
105+ <tr class="table-danger">
106+ <td class="fw-bold"><i class="bi bi-x-circle-fill me-1"></i> Invalid</td>
107+ <td><i class="bi bi-check-circle"></i> Valid</td>
108+ <td><i class="bi bi-x-circle"></i> Invalid</td>
109+ <td><i class="bi bi-check-circle"></i> Valid</td>
110+ </tr>
111+ <tr class="table-danger">
112+ <td class="fw-bold"><i class="bi bi-x-circle-fill me-1"></i> Invalid</td>
113+ <td><i class="bi bi-check-circle"></i> Valid</td>
114+ <td><i class="bi bi-check-circle"></i> Valid</td>
115+ <td><i class="bi bi-x-circle"></i> Invalid</td>
116+ </tr>
117+ <tr class="table-success">
118+ <td class="fw-bold"><i class="bi bi-check-circle-fill me-1"></i> Valid</td>
119+ <td><i class="bi bi-check-circle"></i> Valid</td>
120+ <td><i class="bi bi-check-circle"></i> Valid</td>
121+ <td><i class="bi bi-check-circle"></i> Valid</td>
122+ </tr>
123+ </tbody >
124+ </table >
125+
126+ ## Examples
127+
128+ {{<schema ` A schema that constrains instances with two internally referenced schemas ` >}}
129+ {
130+ "$schema": "https://json-schema.org/draft/2019-09/schema ",
131+ "allOf": [
132+ { "$ref": "#/$defs/foo" },
133+ { "$ref": "#/$defs/bar" }
134+ ] ,
135+ "$defs": {
136+ "foo": { "type": "number" },
137+ "bar": { "type": "integer" }
138+ }
139+ }
140+ {{</schema >}}
141+
142+ {{<instance-pass ` A value that matches both subschemas is valid ` >}}
143+ 12345
144+ {{</instance-pass >}}
145+
146+ {{<instance-fail ` A value that only matches one of the subschemas is invalid ` >}}
147+ 3.14
148+ {{</instance-fail >}}
149+
150+ {{<instance-fail ` A value that does not match any of the subschemas is invalid ` >}}
151+ "Hello World"
152+ {{</instance-fail >}}
0 commit comments