From 501298a4ff5c48320d6fbecc9649dd83a4584ebd Mon Sep 17 00:00:00 2001 From: Ludo Visser Date: Sun, 12 Sep 2021 22:32:21 +0200 Subject: [PATCH 1/2] Provide utility function to override $schema --- jsonschema/validators.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/jsonschema/validators.py b/jsonschema/validators.py index 70d46c2fd..f84c508eb 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -3,6 +3,7 @@ """ from collections.abc import Sequence from functools import lru_cache +from re import A from urllib.parse import unquote, urldefrag, urljoin, urlsplit from urllib.request import urlopen from warnings import warn @@ -810,3 +811,35 @@ def validator_for(schema, default=_LATEST_VERSION): stacklevel=2, ) return meta_schemas.get(schema[u"$schema"], _LATEST_VERSION) + + +def set_validator_for(schema, validator, meta_schema_id=None): + """ + Register a validator class for the given schema. + + Uses the `$schema` property of the given schema as key in the validator + lookup table, unless `meta_schema_id` is given to override this. + + This method allows you to set a validator for a particular schema in cases + where automatic lookup fails, for example when a schema specificies a + `$schema` that is not resolvable. + + Arguments: + + schema (collections.abc.Mapping): + + the schema to register a validator for + + validator: + + the validator to use for this schema + + meta_schema_id: + + if not `None`, the given value is used as key in the lookup table. + """ + if meta_schema_id is None: + meta_schema_id = schema.get(u"$schema", u"") + + if meta_schema_id: + meta_schemas[meta_schema_id] = validator From a4d6fb977dd8a07b27d0b1dd3aed4486a57e5f20 Mon Sep 17 00:00:00 2001 From: Ludo Visser Date: Sun, 12 Sep 2021 22:40:38 +0200 Subject: [PATCH 2/2] Remove accidentally added import. --- jsonschema/validators.py | 1 - 1 file changed, 1 deletion(-) diff --git a/jsonschema/validators.py b/jsonschema/validators.py index f84c508eb..be36c14aa 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -3,7 +3,6 @@ """ from collections.abc import Sequence from functools import lru_cache -from re import A from urllib.parse import unquote, urldefrag, urljoin, urlsplit from urllib.request import urlopen from warnings import warn