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

Allow SchemaJsonMixin classes to define a validator method #136

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed

- Restore jsonschema's type validator, as its performance has improved in recent Python versions https://github.com/OpenDataServices/lib-cove/pull/127
- Allow `SchemaJsonMixin` classes to define a `validator` method, that accepts lib-cove's JSON Schema draft 4 validator class and its format checker, and returns a validator instance. https://github.com/OpenDataServices/lib-cove/pull/128

## [0.31.0] - 2023-07-06

Expand Down
46 changes: 26 additions & 20 deletions libcove/lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ def dependencies_extra_data(validator, dependencies, instance, schema):
# Properties this class might look for
# * cache_schema, boolean. This is deprecated, use the 'cache_all_requests' option in config instead
# * config, an instance of a config class.
# Methods that might be looked for on this class:
# * validator - passed validator, format_checker and returns a validator instance
class SchemaJsonMixin:
@cached_property
def schema_str(self):
Expand Down Expand Up @@ -792,32 +794,36 @@ def get_schema_validation_errors(
if extra_checkers:
format_checker.checkers.update(extra_checkers)

if getattr(schema_obj, "extended", None):
resolver = CustomRefResolver(
"",
pkg_schema_obj,
config=getattr(schema_obj, "config", None),
schema_url=schema_obj.schema_host,
schema_file=schema_obj.extended_schema_file,
file_schema_name=schema_obj.schema_name,
)
else:
resolver = CustomRefResolver(
"",
pkg_schema_obj,
config=getattr(schema_obj, "config", None),
schema_url=schema_obj.schema_host,
)

# Force jsonschema to use our validator.
# https://github.com/python-jsonschema/jsonschema/issues/994
jsonschema.validators.validates("http://json-schema.org/draft-04/schema#")(
validator
)

our_validator = validator(
pkg_schema_obj, format_checker=format_checker, resolver=resolver
)
if hasattr(schema_obj, "validator"):
our_validator = schema_obj.validator(validator, format_checker)
else:
if getattr(schema_obj, "extended", None):
resolver = CustomRefResolver(
"",
pkg_schema_obj,
config=getattr(schema_obj, "config", None),
schema_url=schema_obj.schema_host,
schema_file=schema_obj.extended_schema_file,
file_schema_name=schema_obj.schema_name,
)
else:
resolver = CustomRefResolver(
"",
pkg_schema_obj,
config=getattr(schema_obj, "config", None),
schema_url=schema_obj.schema_host,
)

our_validator = validator(
pkg_schema_obj, format_checker=format_checker, resolver=resolver
)

for e in our_validator.iter_errors(json_data):
message = e.message
path = "/".join(str(item) for item in e.path)
Expand Down
Loading