Skip to content

Commit ecacaa7

Browse files
committed
Can process if / then / else keywords
1 parent 32dbd11 commit ecacaa7

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111
- Cache results of processing. Repeated calls to get methods will be faster.
1212
- Added audit functionality. Thanks to Duncan and https://github.com/OpenDataServices/json-schema-auditor
1313
- Can process dependentSchemas keyword
14+
- Can process if / then / else keywords
1415

1516
### Removed
1617

compiletojsonschema/compiletojsonschema.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ def __process_data(self, source):
122122
for k, v in source["dependentSchemas"].items():
123123
out["dependentSchemas"][k] = self.__process_data(v)
124124

125+
for keyword in ["if", "then", "else"]:
126+
if keyword in source:
127+
out[keyword] = self.__process_data(source[keyword])
128+
125129
if "codelist" in source and (
126130
"openCodelist" not in source or not source["openCodelist"]
127131
):
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"name": {
5+
"type": "string"
6+
}
7+
},
8+
"if": {
9+
"properties": {
10+
"credit_card": {
11+
"type": "number"
12+
}
13+
}
14+
},
15+
"then": {
16+
"properties": {
17+
"address": {
18+
"$ref": "file-definitions.json#/definition/address",
19+
"title": "Must have an address with a credit card"
20+
}
21+
}
22+
},
23+
"else": {
24+
"properties": {
25+
"cash_on_delivery": { "const": "Yup" }
26+
}
27+
}
28+
}

tests/test_simple.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,34 @@ def test_file_dependentSchemas():
188188
"title",
189189
"type",
190190
] == ctjs.get_keywords_used()
191+
192+
193+
def test_file_if_then_else():
194+
195+
input_filename = os.path.join(
196+
os.path.dirname(os.path.realpath(__file__)),
197+
"fixtures",
198+
"simple",
199+
"file-if-then-else.json",
200+
)
201+
202+
ctjs = CompileToJsonSchema(input_filename=input_filename)
203+
out = ctjs.get()
204+
205+
assert (
206+
out["then"]["properties"]["address"]["title"]
207+
== "Must have an address with a credit card"
208+
)
209+
210+
assert ["number", "object", "string"] == ctjs.get_types_used()
211+
assert [
212+
"$ref",
213+
"const",
214+
"description",
215+
"else",
216+
"if",
217+
"properties",
218+
"then",
219+
"title",
220+
"type",
221+
] == ctjs.get_keywords_used()

0 commit comments

Comments
 (0)