From 760ebe5fbf33f8d0029e7c8947f2739f1321ec41 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Mon, 14 May 2018 08:03:22 -0400 Subject: [PATCH 1/4] Merge in the minor validator_for shuffling. --- jsonschema/validators.py | 54 +++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/jsonschema/validators.py b/jsonschema/validators.py index 56ce06b64..55eba6484 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -23,8 +23,6 @@ ErrorTree -_unset = _utils.Unset() - validators = {} meta_schemas = _utils.URIDict() @@ -435,6 +433,8 @@ def extend(validator, validators=(), version=None, type_checker=None): version="draft4", ) +_LATEST_VERSION = Draft4Validator + class RefResolver(object): """ @@ -695,32 +695,6 @@ def resolve_remote(self, uri): return result -def validator_for(schema, default=_unset): - """ - Retrieve the validator class appropriate for validating the given schema. - - Uses the :validator:`$schema` property that should be present in the given - schema to look up the appropriate validator class. - - Arguments: - - schema (dict): - - the schema to look at - - default: - - the default to return if the appropriate validator class cannot be - determined. - - If unprovided, the default is to return - `jsonschema.Draft4Validator`. - """ - if default is _unset: - default = Draft4Validator - return meta_schemas.get(schema.get(u"$schema", u""), default) - - def validate(instance, schema, cls=None, *args, **kwargs): """ Validate an instance under the given schema. @@ -779,3 +753,27 @@ def validate(instance, schema, cls=None, *args, **kwargs): cls = validator_for(schema) cls.check_schema(schema) cls(schema, *args, **kwargs).validate(instance) + + +def validator_for(schema, default=_LATEST_VERSION): + """ + Retrieve the validator class appropriate for validating the given schema. + + Uses the :validator:`$schema` property that should be present in the given + schema to look up the appropriate validator class. + + Arguments: + + schema (collections.Mapping or bool): + + the schema to look at + + default: + + the default to return if the appropriate validator class cannot be + determined. + + If unprovided, the default is to return + the latest supported draft. + """ + return meta_schemas.get(schema.get(u"$schema", u""), default) From cffdf7a0f12a01462803e90ab36385ade9c23768 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Mon, 14 May 2018 08:32:24 -0400 Subject: [PATCH 2/4] Start populating the CHANGELOG --- CHANGELOG.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2843a7299..998008c1f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,7 @@ +v3.0.0 +------ + +* New ``TypeChecker`` object for more complex type definitions (and overrides) v2.6.0 ------ From d1de20f7d8ee65b89b36697af19ee48d3f9a8cbe Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Mon, 14 May 2018 08:33:42 -0400 Subject: [PATCH 3/4] Stop falling back to isodate. --- CHANGELOG.rst | 3 +++ docs/validate.rst | 7 ------- jsonschema/_format.py | 11 +---------- 3 files changed, 4 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 998008c1f..dccd4cbfe 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,9 @@ v3.0.0 ------ * New ``TypeChecker`` object for more complex type definitions (and overrides) +* Falling back to isodate for the date-time format checker is no longer + attempted, in accordance with the specification + v2.6.0 ------ diff --git a/docs/validate.rst b/docs/validate.rst index 43b6f0045..6449d2fcc 100644 --- a/docs/validate.rst +++ b/docs/validate.rst @@ -345,13 +345,6 @@ color requires webcolors_ ========== ==================== -.. [#] For backwards compatibility, isodate_ is also supported, but it will - allow any `ISO 8601 `_ date-time, - not just `RFC 3339 `_ as mandated by - the JSON Schema specification. - - -.. _isodate: http://pypi.python.org/pypi/isodate/ .. _rfc3987: http://pypi.python.org/pypi/rfc3987/ .. _strict-rfc3339: http://pypi.python.org/pypi/strict-rfc3339/ .. _webcolors: http://pypi.python.org/pypi/webcolors/ diff --git a/jsonschema/_format.py b/jsonschema/_format.py index acef74631..d0f265cb6 100644 --- a/jsonschema/_format.py +++ b/jsonschema/_format.py @@ -205,16 +205,7 @@ def is_uri(instance): try: import strict_rfc3339 except ImportError: - try: - import isodate - except ImportError: - pass - else: - @_checks_drafts("date-time", raises=(ValueError, isodate.ISO8601Error)) - def is_datetime(instance): - if not isinstance(instance, str_types): - return True - return isodate.parse_datetime(instance) + pass else: @_checks_drafts("date-time") def is_datetime(instance): From 7695804c0f9947a741775f80f24f2d50a4379705 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Mon, 14 May 2018 08:35:23 -0400 Subject: [PATCH 4/4] Minor style. --- jsonschema/_format.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/jsonschema/_format.py b/jsonschema/_format.py index d0f265cb6..8a06e3dd3 100644 --- a/jsonschema/_format.py +++ b/jsonschema/_format.py @@ -132,9 +132,9 @@ def conforms(self, instance, format): _draft_checkers = {"draft3": [], "draft4": []} -def _checks_drafts(both=None, draft3=None, draft4=None, raises=()): - draft3 = draft3 or both - draft4 = draft4 or both +def _checks_drafts(name=None, draft3=None, draft4=None, raises=()): + draft3 = draft3 or name + draft4 = draft4 or name def wrap(func): if draft3: @@ -147,7 +147,7 @@ def wrap(func): return wrap -@_checks_drafts("email") +@_checks_drafts(name="email") def is_email(instance): if not isinstance(instance, str_types): return True @@ -167,7 +167,7 @@ def is_ipv4(instance): if hasattr(socket, "inet_pton"): - @_checks_drafts("ipv6", raises=socket.error) + @_checks_drafts(name="ipv6", raises=socket.error) def is_ipv6(instance): if not isinstance(instance, str_types): return True @@ -195,7 +195,7 @@ def is_host_name(instance): except ImportError: pass else: - @_checks_drafts("uri", raises=ValueError) + @_checks_drafts(name="uri", raises=ValueError) def is_uri(instance): if not isinstance(instance, str_types): return True @@ -207,14 +207,14 @@ def is_uri(instance): except ImportError: pass else: - @_checks_drafts("date-time") + @_checks_drafts(name="date-time") def is_datetime(instance): if not isinstance(instance, str_types): return True return strict_rfc3339.validate_rfc3339(instance) -@_checks_drafts("regex", raises=re.error) +@_checks_drafts(name="regex", raises=re.error) def is_regex(instance): if not isinstance(instance, str_types): return True