diff --git a/akm_tools/validation/data_instance_validators.py b/akm_tools/validation/data_instance_validators.py index d729738..cc3f8a6 100644 --- a/akm_tools/validation/data_instance_validators.py +++ b/akm_tools/validation/data_instance_validators.py @@ -83,8 +83,8 @@ def validate(self, instance: dict, **kwargs): self.object_validators_dict[instance["entityTypeID"]].iter_errors(instance), key=lambda e: e.path, ) - base_error_msg += "\n".join(x.message for x in additioanl_error_info) - base_error_msg += "\n" + base_error_msg += "\n".join(x.message for x in additioanl_error_info) + base_error_msg += "\n" return False, base_error_msg except Exception as e: raise e diff --git a/tests/conftest.py b/tests/conftest.py index c71549e..2dfdf9b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,8 +12,9 @@ def simple_schema(): "type": {"type": "string"}, "age": {"type": "number"}, "id": {"type": "string"}, + "entityTypeID": {"type": "string"}, }, - "required": ["id"], + "required": ["id", "entityTypeID"], "additionalProperties": False, } return schema @@ -22,8 +23,8 @@ def simple_schema(): @pytest.fixture def simple_data(): data = [ - {"type": "John", "age": 30, "id": "unique_id_1"}, - {"type": "Jane", "age": 25, "id": "unique_id_2"}, + {"type": "John", "age": 30, "id": "unique_id_1", "entityTypeID": "type1"}, + {"type": "Jane", "age": 25, "id": "unique_id_2", "entityTypeID": "type2"}, ] return data @@ -31,15 +32,18 @@ def simple_data(): @pytest.fixture def simple_data_with_more_attributes(): data = [ - {"type": "John", "age": 30, "id": "unique_id_1", "extra_attribute": "wild"}, - {"type": "Jane", "age": 25, "id": "unique_id_2", "extra_attribute": "grass"}, + {"type": "John", "age": 30, "id": "unique_id_1", "entityTypeID": "type1", "extra_attribute": "wild"}, + {"type": "Jane", "age": 25, "id": "unique_id_2", "entityTypeID": "type2", "extra_attribute": "grass"}, ] return data @pytest.fixture def simple_data_without_required_attribute(): - data = [{"type": "John", "age": 30}, {"type": "Jane", "age": 25}] + data = [ + {"type": "John", "age": 30, "entityTypeID": "type1"}, + {"type": "Jane", "age": 25, "entityTypeID": "type2"} + ] return data @@ -58,9 +62,10 @@ def complex_schema_with_defs(): "type": "object", "properties": { "id": {"type": "string"}, + "entityTypeID": {"type": "string"}, "definition": {"type": "string"}, }, - "required": ["id"], + "required": ["id", "entityTypeID"], }, "ObjectType1": { "$id": "complexSchema.ObjectType1", @@ -69,7 +74,6 @@ def complex_schema_with_defs(): "properties": { "name": {"type": "string"}, "description": {"type": "string"}, - "type": {"type": "string", "const": "ObjectType1"}, }, "required": ["name", "type"], "unevaluatedProperties": False, @@ -80,7 +84,6 @@ def complex_schema_with_defs(): "allOf": [{"$ref": "complexSchema.BaseClass"}], "properties": { "age": {"type": "number"}, - "type": {"type": "string", "const": "ObjectType2"}, }, "required": ["type"], "unevaluatedProperties": False, @@ -97,27 +100,25 @@ def complex_data(): data = [ { "id": "unique_id_1", + "entityTypeID": "type1", "definition": "Some def1", "name": "AttributeName", - "type": "ObjectType1", "description": "some desc", }, - {"id": "unique_id_2", "type": "ObjectType2", "age": 10}, + {"id": "unique_id_2", "entityTypeID": "type2", "age": 10}, ] return data @pytest.fixture -def complex_data_missing_required_attributes(): ## id/type is missing. +def complex_data_missing_required_attributes(): ## id/entityTypeID is missing. data = [ { "definition": "Some def1", "name": "AttributeName", - "type": "ObjectType1", "description": "some desc", }, { - "type": "ObjectType2", "age": 10, }, ] @@ -129,15 +130,15 @@ def complex_data_with_additional_attributes(): data = [ { "id": "unique_id_1", + "entityTypeID": "typObjectType1e1", "definition": "Some def1", "name": "AttributeName", - "type": "ObjectType1", "description": "some desc", "extra_attribute": "wild", }, { "id": "unique_id_2", - "type": "ObjectType2", + "entityTypeID": "ObjectType2", "age": 10, "extra_attribute": "grass", }, @@ -150,16 +151,16 @@ def data_with_duplicate_ids(): data = [ { "id": "unique_id_1", + "entityTypeID": "type1", "definition": "Some def1", "name": "AttributeName", - "type": "ObjectType1", "description": "some desc", }, { "id": "unique_id_1", + "entityTypeID": "type1", "definition": "Some def2", "name": "AttributeName2", - "type": "ObjectType2", "description": "some desc2", }, ] @@ -167,7 +168,7 @@ def data_with_duplicate_ids(): @pytest.fixture -def scehma_with_extensions(): +def schema_with_extensions(): schema = { "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "schema_with_extensions", @@ -180,7 +181,7 @@ def scehma_with_extensions(): "description": {"type": "string"}, "entityType": {"type": "string", "const": "ObjectType3"}, }, - "required": ["entityType"], + "required": ["id","entityType"], "unevaluatedProperties": False, } schema_extension = { @@ -225,7 +226,7 @@ def overlay_existing_data_with_addional_properties(): @pytest.fixture -def ovewrite_existing_data(): +def overwrite_existing_data(): data = [ { "id": "unique_id1", diff --git a/tests/test_CoreJsonSchemaValidator.py b/tests/test_CoreJsonSchemaValidator.py index a84d678..3d243e1 100644 --- a/tests/test_CoreJsonSchemaValidator.py +++ b/tests/test_CoreJsonSchemaValidator.py @@ -68,8 +68,8 @@ def test_complex_data_validator_with_invalid_attribute(complex_schema_with_defs, assert all(valid_data) == False -def test_complex_data_validator_with_extended_data(scehma_with_extensions, data_with_extended_properties): - schema, registry = scehma_with_extensions +def test_complex_data_validator_with_extended_data(schema_with_extensions, data_with_extended_properties): + schema, registry = schema_with_extensions complex_data_validator = CoreJsonSchemaValidator(schema=schema, extended_schema_dir=None) complex_data_validator.configure_registry(registry) valid_data = [] @@ -79,8 +79,8 @@ def test_complex_data_validator_with_extended_data(scehma_with_extensions, data_ assert all(valid_data) == True -def test_complex_data_validator_with_extended_data(scehma_with_extensions, overlay_existing_data_with_addional_properties): - schema, registry = scehma_with_extensions +def test_complex_data_validator_with_overlaid_data(schema_with_extensions, overlay_existing_data_with_addional_properties): + schema, registry = schema_with_extensions complex_data_validator = CoreJsonSchemaValidator(schema=schema, extended_schema_dir=None) complex_data_validator.configure_registry(registry) valid_data = [] @@ -88,3 +88,14 @@ def test_complex_data_validator_with_extended_data(scehma_with_extensions, overl is_valid, _ = complex_data_validator.validate(instance=instance) valid_data.append(is_valid) assert all(valid_data) == True + + +def test_complex_data_validator_with_overwritten_data(schema_with_extensions, overwrite_existing_data): + schema, registry = schema_with_extensions + complex_data_validator = CoreJsonSchemaValidator(schema=schema, extended_schema_dir=None) + complex_data_validator.configure_registry(registry) + valid_data = [] + for instance in overwrite_existing_data: + is_valid, _ = complex_data_validator.validate(instance=instance) + valid_data.append(is_valid) + assert all(valid_data) == False