From dd91d7975ec11ca1d8912050e68f67f99b47387b Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 16 May 2024 17:32:12 +0200 Subject: [PATCH] Remove jsonschema dependency (we use fastjsonschema) --- datacontract/imports/jsonschema_importer.py | 18 ++++++++++++++---- pyproject.toml | 1 - tests/fixtures/import/orders-datacontract.yml | 1 - tests/fixtures/import/orders.json | 3 +-- tests/test_import_jsonschema.py | 1 + 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/datacontract/imports/jsonschema_importer.py b/datacontract/imports/jsonschema_importer.py index e185eaa5..c8ef2977 100644 --- a/datacontract/imports/jsonschema_importer.py +++ b/datacontract/imports/jsonschema_importer.py @@ -1,8 +1,10 @@ +import json + +import fastjsonschema + from datacontract.model.data_contract_specification import \ DataContractSpecification, Model, Field, Definition from datacontract.model.exceptions import DataContractException -import json -import jsonschema def convert_json_schema_properties(properties, is_definition=False): @@ -72,8 +74,8 @@ def import_jsonschema(data_contract_specification: DataContractSpecification, so try: with open(source, "r") as file: json_schema = json.loads(file.read()) - validator = jsonschema.Draft7Validator({}) - validator.check_schema(json_schema) + validator = fastjsonschema.compile({}) + validator(json_schema) model = Model( description=json_schema.get('description'), @@ -127,6 +129,14 @@ def import_jsonschema(data_contract_specification: DataContractSpecification, so definition = Definition(name=def_name, **definition_kwargs) data_contract_specification.definitions[def_name] = definition + except fastjsonschema.JsonSchemaException as e: + raise DataContractException( + type="schema", + name="Parse json schema", + reason=f"Failed to parse json schema from {source}: {e}", + engine="datacontract" + ) + except Exception as e: raise DataContractException( type="schema", diff --git a/pyproject.toml b/pyproject.toml index 59dffb99..590240a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,7 +41,6 @@ dependencies = [ "deltalake~=0.17.0", "boto3>=1.34.41,<1.34.99", "botocore>=1.34.41,<1.34.99", - "jsonschema>=4.22", "jinja_partials >= 0.2.1" ] diff --git a/tests/fixtures/import/orders-datacontract.yml b/tests/fixtures/import/orders-datacontract.yml index c51be0f2..93b38fc6 100644 --- a/tests/fixtures/import/orders-datacontract.yml +++ b/tests/fixtures/import/orders-datacontract.yml @@ -12,7 +12,6 @@ models: order_id: title: Order ID type: string - format: uuid required: true description: Unique identifier for the order order_timestamp: diff --git a/tests/fixtures/import/orders.json b/tests/fixtures/import/orders.json index 1e4ed27a..03d12050 100644 --- a/tests/fixtures/import/orders.json +++ b/tests/fixtures/import/orders.json @@ -1,12 +1,11 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", + "$schema": "http://json-schema.org/draft-07/schema", "title": "OrderSchema", "description": "Schema for order details", "type": "object", "properties": { "order_id": { "type": "string", - "format": "uuid", "title": "Order ID", "description": "Unique identifier for the order" }, diff --git a/tests/test_import_jsonschema.py b/tests/test_import_jsonschema.py index 9748b941..0273d8d9 100644 --- a/tests/test_import_jsonschema.py +++ b/tests/test_import_jsonschema.py @@ -34,6 +34,7 @@ def test_import_json_schema_orders(): assert yaml.safe_load(result.to_yaml()) == yaml.safe_load(expected) assert DataContract(data_contract_str=expected).lint(enabled_linters="none").has_passed() + def test_import_json_schema_football(): result = DataContract().import_from_source("jsonschema", "fixtures/import/football.json")