Skip to content

Commit 783c930

Browse files
authored
Support if-then-else #37 (#53)
1 parent 4ecadc7 commit 783c930

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/validation.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,64 @@ function _validate(x, schema, ::Val{:not}, val, path::String)
160160
end
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.

test/runtests.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ write(
131131
}]""",
132132
)
133133

134-
@testset "Draft 4/6" begin
134+
@testset "Draft 4/6/7" begin
135135
# Note(odow): I didn't want to use a mutable reference like this for the web-server.
136136
# The obvious thing to do is to start a new server for each value of `draft_folder`,
137137
# however, shutting down the webserver asynchronously doesn't play well with
@@ -148,6 +148,7 @@ write(
148148
@testset "$(draft_folder)" for draft_folder in [
149149
"draft4",
150150
"draft6",
151+
"draft7",
151152
basename(abspath(LOCAL_TEST_DIR)),
152153
]
153154
test_dir = joinpath(SCHEMA_TEST_DIR, draft_folder)
@@ -190,6 +191,7 @@ end
190191
@testset "$(draft_folder)" for draft_folder in [
191192
"draft4",
192193
"draft6",
194+
"draft7",
193195
basename(abspath(LOCAL_TEST_DIR)),
194196
]
195197
test_dir = joinpath(SCHEMA_TEST_DIR, draft_folder)

0 commit comments

Comments
 (0)