Skip to content

Commit 501298a

Browse files
committed
Provide utility function to override $schema
1 parent 61382d7 commit 501298a

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

jsonschema/validators.py

+33
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
from collections.abc import Sequence
55
from functools import lru_cache
6+
from re import A
67
from urllib.parse import unquote, urldefrag, urljoin, urlsplit
78
from urllib.request import urlopen
89
from warnings import warn
@@ -810,3 +811,35 @@ def validator_for(schema, default=_LATEST_VERSION):
810811
stacklevel=2,
811812
)
812813
return meta_schemas.get(schema[u"$schema"], _LATEST_VERSION)
814+
815+
816+
def set_validator_for(schema, validator, meta_schema_id=None):
817+
"""
818+
Register a validator class for the given schema.
819+
820+
Uses the `$schema` property of the given schema as key in the validator
821+
lookup table, unless `meta_schema_id` is given to override this.
822+
823+
This method allows you to set a validator for a particular schema in cases
824+
where automatic lookup fails, for example when a schema specificies a
825+
`$schema` that is not resolvable.
826+
827+
Arguments:
828+
829+
schema (collections.abc.Mapping):
830+
831+
the schema to register a validator for
832+
833+
validator:
834+
835+
the validator to use for this schema
836+
837+
meta_schema_id:
838+
839+
if not `None`, the given value is used as key in the lookup table.
840+
"""
841+
if meta_schema_id is None:
842+
meta_schema_id = schema.get(u"$schema", u"")
843+
844+
if meta_schema_id:
845+
meta_schemas[meta_schema_id] = validator

0 commit comments

Comments
 (0)