From 8c2dbd3f78648e9470c921f6dfc4e255da91e4f7 Mon Sep 17 00:00:00 2001 From: James Date: Wed, 9 Nov 2022 09:37:04 +0000 Subject: [PATCH] jsonschemavalidate: Add format checkers https://github.com/Open-Telecoms-Data/lib-cove-ofds/issues/28 --- CHANGELOG.md | 5 +++++ libcoveofds/jsonschemavalidate.py | 5 ++++- make_expected_test_data.sh | 1 + .../bad_uuid_1.expected.json | 22 +++++++++++++++++++ .../jsonschemavalidate/bad_uuid_1.input.json | 13 +++++++++++ tests/test_jsonschemavalidate.py | 2 ++ 6 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/jsonschemavalidate/bad_uuid_1.expected.json create mode 100644 tests/fixtures/jsonschemavalidate/bad_uuid_1.input.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f70a37..bdb37c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + +- JSONSchema validate: + - Add format checkers + ## [0.3.0] - 2022-11-08 ### Added diff --git a/libcoveofds/jsonschemavalidate.py b/libcoveofds/jsonschemavalidate.py index 48f2745..2aa8c1b 100644 --- a/libcoveofds/jsonschemavalidate.py +++ b/libcoveofds/jsonschemavalidate.py @@ -1,3 +1,4 @@ +from jsonschema import FormatChecker from jsonschema.exceptions import ValidationError as JSONSchemaExceptionsValidationError from jsonschema.validators import Draft202012Validator @@ -9,7 +10,9 @@ def __init__(self, schema: OFDSSchema): self._schema = schema def validate(self, json_data: dict) -> list: - validator = Draft202012Validator(schema=self._schema.get_package_schema()) + validator = Draft202012Validator( + schema=self._schema.get_package_schema(), format_checker=FormatChecker() + ) output = [] for e in validator.iter_errors(json_data): output.append(ValidationError(e, json_data, self._schema)) diff --git a/make_expected_test_data.sh b/make_expected_test_data.sh index 04da812..8544aa1 100755 --- a/make_expected_test_data.sh +++ b/make_expected_test_data.sh @@ -25,6 +25,7 @@ libcoveofds jtogj --outputmetafilename tests/fixtures/json_to_geojson/no_geometr # JSON Schema validate libcoveofds jsv tests/fixtures/jsonschemavalidate/basic_1.input.json > tests/fixtures/jsonschemavalidate/basic_1.expected.json +libcoveofds jsv tests/fixtures/jsonschemavalidate/bad_uuid_1.input.json > tests/fixtures/jsonschemavalidate/bad_uuid_1.expected.json # Python validate # TODO diff --git a/tests/fixtures/jsonschemavalidate/bad_uuid_1.expected.json b/tests/fixtures/jsonschemavalidate/bad_uuid_1.expected.json new file mode 100644 index 0000000..3d1f7b5 --- /dev/null +++ b/tests/fixtures/jsonschemavalidate/bad_uuid_1.expected.json @@ -0,0 +1,22 @@ +[ + { + "message": "'1' is not a 'uuid'", + "path": [ + "networks", + 0, + "id" + ], + "schema_path": [ + "properties", + "networks", + "items", + "properties", + "id", + "format" + ], + "validator": "format", + "data_ids": { + "network_id": "1" + } + } +] diff --git a/tests/fixtures/jsonschemavalidate/bad_uuid_1.input.json b/tests/fixtures/jsonschemavalidate/bad_uuid_1.input.json new file mode 100644 index 0000000..0cebe72 --- /dev/null +++ b/tests/fixtures/jsonschemavalidate/bad_uuid_1.input.json @@ -0,0 +1,13 @@ +{ + "networks": [ + { + "id": "1", + "links": [ + { + "rel": "describedby", + "href": "https://raw.githubusercontent.com/Open-Telecoms-Data/open-fibre-data-standard/0__1__0__beta/schema/network-schema.json" + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/test_jsonschemavalidate.py b/tests/test_jsonschemavalidate.py index 1687cf4..62459d6 100644 --- a/tests/test_jsonschemavalidate.py +++ b/tests/test_jsonschemavalidate.py @@ -9,6 +9,8 @@ JSONSCHEMAVALIDATE_FILES = [ # basic example ("basic_1"), + # Bad data + ("bad_uuid_1"), ]