|
3 | 3 | """
|
4 | 4 | from collections.abc import Sequence
|
5 | 5 | from functools import lru_cache
|
| 6 | +from re import A |
6 | 7 | from urllib.parse import unquote, urldefrag, urljoin, urlsplit
|
7 | 8 | from urllib.request import urlopen
|
8 | 9 | from warnings import warn
|
@@ -810,3 +811,35 @@ def validator_for(schema, default=_LATEST_VERSION):
|
810 | 811 | stacklevel=2,
|
811 | 812 | )
|
812 | 813 | 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