Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore scheme and trailing slashes in meta_schemes key #822

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,3 +810,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