Skip to content

Commit

Permalink
Allow SchemaJsonMixin classes to define a validator method
Browse files Browse the repository at this point in the history
This accepts lib-cove's JSON Schema draft 4 validator class and its format checker, and returns a validator instance.
  • Loading branch information
jpmckinney authored and odscjames committed Oct 27, 2023
1 parent b04a6f2 commit 7ca6779
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
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

0 comments on commit 7ca6779

Please sign in to comment.