@@ -160,10 +160,64 @@ function _validate(x, schema, ::Val{:not}, val, path::String)
160160end
161161
162162# 9.2.2.1: if
163+ function _validate(x, schema, :: Val{:if} , val, path:: String )
164+ # ignore if without then or else
165+ if haskey(schema, " then" ) || haskey(schema, " else" )
166+ return _if_then_else(x, schema, path)
167+ end
168+ return
169+ end
163170
164171# 9.2.2.2: then
172+ function _validate(x, schema, :: Val{:then} , val, path:: String )
173+ # ignore then without if
174+ if haskey(schema, " if" )
175+ return _if_then_else(x, schema, path)
176+ end
177+ return
178+ end
165179
166180# 9.2.2.3: else
181+ function _validate(x, schema, :: Val{:else} , val, path:: String )
182+ # ignore else without if
183+ if haskey(schema, " if" )
184+ return _if_then_else(x, schema, path)
185+ end
186+ return
187+ end
188+
189+ """
190+ _if_then_else(x, schema, path)
191+
192+ The if, then and else keywords allow the application of a subschema based on the
193+ outcome of another schema. Details are in the link and the truth table is as
194+ follows:
195+
196+ ```
197+ ┌─────┬──────┬──────┬────────┐
198+ │ if │ then │ else │ result │
199+ ├─────┼──────┼──────┼────────┤
200+ │ T │ T │ n/a │ T │
201+ │ T │ F │ n/a │ F │
202+ │ F │ n/a │ T │ T │
203+ │ F │ n/a │ F │ F │
204+ │ n/a │ n/a │ n/a │ T │
205+ └─────┴──────┴──────┴────────┘
206+ ```
207+
208+ See https://json-schema.org/understanding-json-schema/reference/conditionals#ifthenelse
209+ for details.
210+ """
211+ function _if_then_else(x, schema, path)
212+ if _validate(x, schema[" if" ], path) != = nothing
213+ if haskey(schema, " else" )
214+ return _validate(x, schema[" else" ], path)
215+ end
216+ elseif haskey(schema, " then" )
217+ return _validate(x, schema[" then" ], path)
218+ end
219+ return
220+ end
167221
168222# ##
169223# ## Checks for Arrays.
0 commit comments