Skip to content

Commit

Permalink
fix: Force jsonschema to use our validator
Browse files Browse the repository at this point in the history
#118

Ben has edited the changelog only.

Co-authored-by: James McKinney <26463+jpmckinney@users.noreply.github.com>
Co-authored-by: Ben Webb <ben.webb@opendataservices.coop>
  • Loading branch information
jpmckinney and Bjwebb committed Jul 6, 2023
1 parent ec4d5ee commit cf97ed8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Fix crash when tmp directory is on different filesystem https://github.com/OpenDataServices/lib-cove/issues/84
- Use sentence case consistently in validation error messages https://github.com/OpenDataServices/lib-cove/issues/28
- Support jsonschema>=4.10 https://github.com/OpenDataServices/lib-cove/pull/118

## Changed

Expand All @@ -19,7 +20,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Allow jsonschema version 4.
- Support arrays of strings that must be on a codelist https://github.com/ThreeSixtyGiving/dataquality/issues/80


## [0.29.0] - 2022-12-14

- Add `SchemaJsonMixin.process_codelists` (previously only existed in lib-cove-ocds, will be used by 360 CoVE) https://github.com/OpenDataServices/lib-cove/pull/109
Expand Down
17 changes: 13 additions & 4 deletions libcove/lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,11 +721,9 @@ def get_additional_codelist_values(schema_obj, json_data):
path_string = "/".join(path_no_num)

if path_string not in additional_codelist_values:

codelist_url = schema_obj.codelists + codelist
codelist_amend_urls = []
if hasattr(schema_obj, "extended_codelist_urls"):

# Replace URL if this codelist is overridden by an extension.
# Last one to be applied wins.
if schema_obj.extended_codelist_urls.get(codelist):
Expand Down Expand Up @@ -772,7 +770,6 @@ def get_additional_fields_info(json_data, schema_fields, context, fields_regex=F
root_additional_fields = set()

for field, field_info in fields_present.items():

if field in schema_fields:
continue
if fields_regex and LANGUAGE_RE.search(field.split("/")[-1]):
Expand Down Expand Up @@ -805,7 +802,6 @@ def get_counts_additional_fields(
fields_regex=False,
additional_fields_info=None,
):

if not additional_fields_info:
schema_fields = schema_obj.get_pkg_schema_fields()
additional_fields_info = get_additional_fields_info(
Expand Down Expand Up @@ -851,6 +847,12 @@ def get_schema_validation_errors(
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
)
Expand Down Expand Up @@ -1001,6 +1003,13 @@ def get_schema_validation_errors(
validation_errors[
json.dumps(unique_validator_key, default=decimal_default)
].append(value)

# Restore jsonschema's default validator, to not interfere with other software.
# https://github.com/python-jsonschema/jsonschema/issues/994
jsonschema.validators.validates("http://json-schema.org/draft-04/schema#")(
jsonschema.validators.Draft4Validator
)

return dict(validation_errors)


Expand Down
6 changes: 0 additions & 6 deletions tests/lib/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ def test_get_schema_deprecated_paths():


def test_schema_dict_fields_generator_release_schema_deprecated_fields():

with open(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
Expand Down Expand Up @@ -362,7 +361,6 @@ def test_schema_dict_fields_generator_release_schema_deprecated_fields():


def test_schema_dict_fields_generator_schema_with_list_and_oneof():

with open(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
Expand Down Expand Up @@ -396,7 +394,6 @@ def test_schema_dict_fields_generator_schema_with_list_and_oneof():


def test_fields_present_generator_tenders_releases_2_releases():

with open(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
Expand Down Expand Up @@ -449,7 +446,6 @@ def test_fields_present_generator_tenders_releases_2_releases():


def test_fields_present_generator_data_root_is_list():

with open(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
Expand Down Expand Up @@ -501,7 +497,6 @@ def test_fields_present_generator_data_root_is_list():


def test_get_additional_fields_info():

simple_data = {
"non_additional_field": "a",
"non_additional_list": [1, 2],
Expand Down Expand Up @@ -1227,7 +1222,6 @@ def test_get_field_coverage_oc4ids():
),
)
def test_oneOfEnumSelectorField(data, count, errors):

with open(common_fixtures("schema_with_one_of_enum_selector_field.json")) as fp:
schema = json.load(fp)

Expand Down
5 changes: 0 additions & 5 deletions tests/lib/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


def test_convert_json_1():

cove_temp_folder = tempfile.mkdtemp(
prefix="lib-cove-ocds-tests-", dir=tempfile.gettempdir()
)
Expand Down Expand Up @@ -55,7 +54,6 @@ def test_convert_json_1():


def test_convert_activity_xml_1():

cove_temp_folder = tempfile.mkdtemp(
prefix="lib-cove-iati-tests-", dir=tempfile.gettempdir()
)
Expand Down Expand Up @@ -110,7 +108,6 @@ def test_convert_activity_xml_1():


def test_convert_org_xml_1():

cove_temp_folder = tempfile.mkdtemp(
prefix="lib-cove-iati-tests-", dir=tempfile.gettempdir()
)
Expand Down Expand Up @@ -166,7 +163,6 @@ def test_convert_org_xml_1():


def test_convert_json_root_is_list_1():

cove_temp_folder = tempfile.mkdtemp(
prefix="lib-cove-ocds-tests-", dir=tempfile.gettempdir()
)
Expand Down Expand Up @@ -214,7 +210,6 @@ def test_convert_json_root_is_list_1():


def test_convert_csv_1():

cove_temp_folder = tempfile.mkdtemp(
prefix="lib-cove-ocds-tests-", dir=tempfile.gettempdir()
)
Expand Down

0 comments on commit cf97ed8

Please sign in to comment.