Skip to content

Commit 64ac97e

Browse files
committed
Add conditional test that uses $ref
1 parent cd13959 commit 64ac97e

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

tests/test_composition.py

+42-6
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ def _composition_example(composition="oneOf"):
1818

1919

2020
@pytest.mark.parametrize(
21-
"composition, value",
22-
[("oneOf", 10), ("allOf", 15), ("anyOf", 9)]
21+
"composition, value", [("oneOf", 10), ("allOf", 15), ("anyOf", 9)]
2322
)
2423
def test_composition(asserter, composition, value):
2524
asserter(_composition_example(composition), value, value)
2625

2726

2827
@pytest.mark.parametrize(
29-
"composition, value",
30-
[("oneOf", 2), ("anyOf", 2), ("allOf", 3)]
28+
"composition, value", [("oneOf", 2), ("anyOf", 2), ("allOf", 3)]
3129
)
3230
def test_ref_is_expanded_on_composition_error(composition, value):
3331
with pytest.raises(JsonSchemaValueException) as exc:
@@ -47,8 +45,7 @@ def test_ref_is_expanded_on_composition_error(composition, value):
4745

4846

4947
@pytest.mark.parametrize(
50-
"composition, value",
51-
[("oneOf", 2), ("anyOf", 2), ("allOf", 3)]
48+
"composition, value", [("oneOf", 2), ("anyOf", 2), ("allOf", 3)]
5249
)
5350
def test_ref_is_expanded_with_resolver(composition, value):
5451
repo = {
@@ -76,3 +73,42 @@ def test_ref_is_expanded_with_resolver(composition, value):
7673
assert composition == "allOf"
7774
assert "$ref" not in exc.value.definition
7875
assert exc.value.definition["type"] == "number"
76+
77+
78+
def test_ref_in_conditional():
79+
repo = {
80+
"sch://USA": {"properties": {"country": {"const": "United States of America"}}},
81+
"sch://USA-post-code": {
82+
"properties": {"postal_code": {"pattern": "[0-9]{5}(-[0-9]{4})?"}}
83+
},
84+
"sch://general-post-code": {
85+
"properties": {
86+
"postal_code": {"pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]"}
87+
}
88+
},
89+
}
90+
schema = {
91+
"type": "object",
92+
"properties": {
93+
"street_address": {"type": "string"},
94+
"country": {
95+
"default": "United States of America",
96+
"enum": ["United States of America", "Canada"],
97+
},
98+
},
99+
"if": {"allOf": [{"$ref": "sch://USA"}]},
100+
"then": {"oneOf": [{"$ref": "sch://USA-post-code"}]},
101+
"else": {"anyOf": [{"$ref": "sch://general-post-code"}]},
102+
}
103+
invalid = {
104+
"street_address": "1600 Pennsylvania Avenue NW",
105+
"country": "United States of America",
106+
"postal_code": "BS12 3FG",
107+
}
108+
109+
with pytest.raises(JsonSchemaValueException) as exc:
110+
fastjsonschema.validate(schema, invalid, handlers={"sch": repo.__getitem__})
111+
112+
assert exc.value.definition["oneOf"] == [
113+
{"properties": {"postal_code": {"pattern": "[0-9]{5}(-[0-9]{4})?"}}}
114+
]

0 commit comments

Comments
 (0)