Skip to content

Commit

Permalink
Remove jsonschema dependency (we use fastjsonschema)
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenchrist committed May 16, 2024
1 parent 7a4fcae commit dd91d79
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
18 changes: 14 additions & 4 deletions datacontract/imports/jsonschema_importer.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]

Expand Down
1 change: 0 additions & 1 deletion tests/fixtures/import/orders-datacontract.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ models:
order_id:
title: Order ID
type: string
format: uuid
required: true
description: Unique identifier for the order
order_timestamp:
Expand Down
3 changes: 1 addition & 2 deletions tests/fixtures/import/orders.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down
1 change: 1 addition & 0 deletions tests/test_import_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit dd91d79

Please sign in to comment.