1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
import pytest
import fastjsonschema
from fastjsonschema import JsonSchemaValueException
def _composition_example(composition="oneOf"):
return {
"definitions": {
"multiple-of-3": {"type": "number", "multipleOf": 3},
"multiple-of-5": {"type": "number", "multipleOf": 5},
},
composition: [
{"$ref": "#/definitions/multiple-of-3"},
{"$ref": "#/definitions/multiple-of-5"},
],
}
@pytest.mark.parametrize(
"composition, value", [("oneOf", 10), ("allOf", 15), ("anyOf", 9)]
)
def test_composition(asserter, composition, value):
asserter(_composition_example(composition), value, value)
@pytest.mark.parametrize(
"composition, value", [("oneOf", 2), ("anyOf", 2), ("allOf", 3)]
)
def test_ref_is_expanded_on_composition_error(composition, value):
with pytest.raises(JsonSchemaValueException) as exc:
fastjsonschema.validate(_composition_example(composition), value)
if composition in exc.value.definition:
assert exc.value.definition[composition] == [
{"type": "number", "multipleOf": 3},
{"type": "number", "multipleOf": 5},
]
else:
# allOf will fail on the first invalid item,
# so the error message will not refer to the entire composition
assert composition == "allOf"
assert "$ref" not in exc.value.definition
assert exc.value.definition["type"] == "number"
@pytest.mark.parametrize(
"composition, value", [("oneOf", 2), ("anyOf", 2), ("allOf", 3)]
)
def test_ref_is_expanded_with_resolver(composition, value):
repo = {
"sch://multiple-of-3": {"type": "number", "multipleOf": 3},
"sch://multiple-of-5": {"type": "number", "multipleOf": 5},
}
schema = {
composition: [
{"$ref": "sch://multiple-of-3"},
{"$ref": "sch://multiple-of-5"},
]
}
with pytest.raises(JsonSchemaValueException) as exc:
fastjsonschema.validate(schema, value, handlers={"sch": repo.__getitem__})
if composition in exc.value.definition:
assert exc.value.definition[composition] == [
{"type": "number", "multipleOf": 3},
{"type": "number", "multipleOf": 5},
]
else:
# allOf will fail on the first invalid item,
# so the error message will not refer to the entire composition
assert composition == "allOf"
assert "$ref" not in exc.value.definition
assert exc.value.definition["type"] == "number"
def test_ref_in_conditional():
repo = {
"sch://USA": {"properties": {"country": {"const": "United States of America"}}},
"sch://USA-post-code": {
"properties": {"postal_code": {"pattern": "[0-9]{5}(-[0-9]{4})?"}}
},
"sch://general-post-code": {
"properties": {
"postal_code": {"pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]"}
}
},
}
schema = {
"type": "object",
"properties": {
"street_address": {"type": "string"},
"country": {
"default": "United States of America",
"enum": ["United States of America", "Canada"],
},
},
"if": {"allOf": [{"$ref": "sch://USA"}]},
"then": {"oneOf": [{"$ref": "sch://USA-post-code"}]},
"else": {"anyOf": [{"$ref": "sch://general-post-code"}]},
}
invalid = {
"street_address": "1600 Pennsylvania Avenue NW",
"country": "United States of America",
"postal_code": "BS12 3FG",
}
with pytest.raises(JsonSchemaValueException) as exc:
fastjsonschema.validate(schema, invalid, handlers={"sch": repo.__getitem__})
assert exc.value.definition["oneOf"] == [
{"properties": {"postal_code": {"pattern": "[0-9]{5}(-[0-9]{4})?"}}}
]
|