Skip to content

Commit

Permalink
Use a validator that corresponds to the input schema draft version
Browse files Browse the repository at this point in the history
  • Loading branch information
Stranger6667 committed Sep 9, 2020
1 parent 181e7c7 commit 0d251ba
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

- Use a validator that corresponds to the input schema draft version (#66)

#### 0.17.4 - 2020-08-26
- fixed string schemas with different `format` keywords (#63)

Expand Down
31 changes: 20 additions & 11 deletions src/hypothesis_jsonschema/_canonicalise.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
# (and writing a few steps by hand is a DoS attack on the AST walker in Pytest)
JSONType = Union[None, bool, float, str, list, Dict[str, Any]]
Schema = Dict[str, JSONType]
JSONSchemaValidator = Union[
jsonschema.validators.Draft3Validator,
jsonschema.validators.Draft4Validator,
jsonschema.validators.Draft6Validator,
jsonschema.validators.Draft7Validator,
]

# Canonical type strings, in order.
TYPE_STRINGS = ("null", "boolean", "integer", "number", "string", "array", "object")
Expand Down Expand Up @@ -66,16 +72,19 @@ def next_down(val: float) -> float:
return out


def make_validator(
schema: Schema,
) -> Union[
jsonschema.validators.Draft3Validator,
jsonschema.validators.Draft4Validator,
jsonschema.validators.Draft6Validator,
jsonschema.validators.Draft7Validator,
]:
validator_cls = jsonschema.validators.validator_for(schema)
return validator_cls(schema)
def _get_validator_class(schema: Schema) -> JSONSchemaValidator:
try:
validator = jsonschema.validators.validator_for(schema)
validator.check_schema(schema)
except jsonschema.exceptions.SchemaError:
validator = jsonschema.Draft4Validator
validator.check_schema(schema)
return validator


def make_validator(schema: Schema) -> JSONSchemaValidator:
validator = _get_validator_class(schema)
return validator(schema)


class CanonicalisingJsonEncoder(json.JSONEncoder):
Expand Down Expand Up @@ -888,7 +897,7 @@ def merged(schemas: List[Any]) -> Optional[Schema]:
if out == FALSEY:
return FALSEY
assert isinstance(out, dict)
jsonschema.validators.validator_for(out).check_schema(out)
_get_validator_class(out)
return out


Expand Down
2 changes: 1 addition & 1 deletion src/hypothesis_jsonschema/_from_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def regex_patterns(draw: Any) -> str:
assert isinstance(result, str)
try:
re.compile(result)
except (re.error, FutureWarning):
except re.error:
assume(False)
return result

Expand Down
15 changes: 15 additions & 0 deletions tests/test_canonicalise.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,21 @@ def test_canonicalise_is_only_valid_for_schemas():
canonicalish("not a schema")


def test_validators_use_proper_draft():
# See GH-66
schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"not": {
"allOf": [
{"exclusiveMinimum": True, "minimum": 0},
{"exclusiveMaximum": True, "maximum": 10},
]
},
}
cc = canonicalish(schema)
jsonschema.validators.validator_for(cc).check_schema(cc)


# Expose fuzz targets in a form that FuzzBuzz can understand (no dotted names)
fuzz_canonical_json_encoding = test_canonical_json_encoding.hypothesis.fuzz_one_input
fuzz_merge_semantics = test_merge_semantics.hypothesis.fuzz_one_input
Expand Down

0 comments on commit 0d251ba

Please sign in to comment.