Skip to content

Commit d2db7ac

Browse files
Stranger6667Zac-HD
authored andcommitted
Use a validator that corresponds to the input schema draft version
1 parent 181e7c7 commit d2db7ac

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Changelog
22

3+
- Use a validator that corresponds to the input schema draft version (#66)
4+
35
#### 0.17.4 - 2020-08-26
46
- fixed string schemas with different `format` keywords (#63)
57

src/hypothesis_jsonschema/_canonicalise.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
# (and writing a few steps by hand is a DoS attack on the AST walker in Pytest)
3030
JSONType = Union[None, bool, float, str, list, Dict[str, Any]]
3131
Schema = Dict[str, JSONType]
32+
JSONSchemaValidator = Union[
33+
jsonschema.validators.Draft4Validator,
34+
jsonschema.validators.Draft6Validator,
35+
jsonschema.validators.Draft7Validator,
36+
]
3237

3338
# Canonical type strings, in order.
3439
TYPE_STRINGS = ("null", "boolean", "integer", "number", "string", "array", "object")
@@ -66,16 +71,19 @@ def next_down(val: float) -> float:
6671
return out
6772

6873

69-
def make_validator(
70-
schema: Schema,
71-
) -> Union[
72-
jsonschema.validators.Draft3Validator,
73-
jsonschema.validators.Draft4Validator,
74-
jsonschema.validators.Draft6Validator,
75-
jsonschema.validators.Draft7Validator,
76-
]:
77-
validator_cls = jsonschema.validators.validator_for(schema)
78-
return validator_cls(schema)
74+
def _get_validator_class(schema: Schema) -> JSONSchemaValidator:
75+
try:
76+
validator = jsonschema.validators.validator_for(schema)
77+
validator.check_schema(schema)
78+
except jsonschema.exceptions.SchemaError:
79+
validator = jsonschema.Draft4Validator
80+
validator.check_schema(schema)
81+
return validator
82+
83+
84+
def make_validator(schema: Schema) -> JSONSchemaValidator:
85+
validator = _get_validator_class(schema)
86+
return validator(schema)
7987

8088

8189
class CanonicalisingJsonEncoder(json.JSONEncoder):
@@ -888,7 +896,7 @@ def merged(schemas: List[Any]) -> Optional[Schema]:
888896
if out == FALSEY:
889897
return FALSEY
890898
assert isinstance(out, dict)
891-
jsonschema.validators.validator_for(out).check_schema(out)
899+
_get_validator_class(out)
892900
return out
893901

894902

tests/test_canonicalise.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,21 @@ def test_canonicalise_is_only_valid_for_schemas():
521521
canonicalish("not a schema")
522522

523523

524+
def test_validators_use_proper_draft():
525+
# See GH-66
526+
schema = {
527+
"$schema": "http://json-schema.org/draft-04/schema#",
528+
"not": {
529+
"allOf": [
530+
{"exclusiveMinimum": True, "minimum": 0},
531+
{"exclusiveMaximum": True, "maximum": 10},
532+
]
533+
},
534+
}
535+
cc = canonicalish(schema)
536+
jsonschema.validators.validator_for(cc).check_schema(cc)
537+
538+
524539
# Expose fuzz targets in a form that FuzzBuzz can understand (no dotted names)
525540
fuzz_canonical_json_encoding = test_canonical_json_encoding.hypothesis.fuzz_one_input
526541
fuzz_merge_semantics = test_merge_semantics.hypothesis.fuzz_one_input

0 commit comments

Comments
 (0)