Skip to content

Validation does not find missing data for nested schema #1403

@vkuehn

Description

@vkuehn

this code does not show any validation error

from referencing import Registry
from referencing.jsonschema import DRAFT7
from jsonschema import Draft7Validator

schema = {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "test_schema_II.json",
    "type": "object",
    "definitions": {
        "step" : {
            "personal": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "The name of the person"
                    },
                    "address": { "$ref": "../address/json_schema.json#/definitions/address" }
                },
                "required": ["name", "address"]
            }
        }
    }
}


base_resource = DRAFT7.create_resource(schema)
draft7_resources = [('/', base_resource)]
        
schema_address = {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "json_schema.json",
    "definitions": {
        "address": {
            "type": "object",
            "properties": {
                "street": {
                    "type": "string"
                },
                "city": {
                    "type": "string"
                },
                "postalCode": {
                    "type": "string"
                }
            },
            "required": ["street", "city", "postalCode"]
        }
    }
}


draft7_resources.append(('./address/json_schema.json',DRAFT7.create_resource(schema_address)))

registry = Registry().with_resources(draft7_resources)
print("Registry created with resources.")
# no stats method for registry, but we can print and get number of uncrawled
print(registry)
validator = Draft7Validator(schema, registry=registry)

# valid and ok
data_good = {
    "HEADER": {
        "file_type": "test data",
        "file_version": 1
    },
    "definitions": {
        "step": {
            "personal": {
                "name": "Test Data",
                "address": {
                    "street": "123 Main St",
                    "city": "Anytown",
                    "postalCode": "12345"
                }
            }
        }
    }
}
result = validator.is_valid(data_good)
print(f"Validation result: for data good is {'valid' if result else 'not valid'}")

data_bad = {
    "HEADER": {
        "file_type": "test data",
        "file_version": 1
    },
    "definitions": {
        "step": {
            "personal": {
                "name": "Test Data",
                "address": {
                    "street": "123 Main St",
                }
            }
        }
    }
}

# !!!! should not be valid !!!
result_bad = validator.is_valid(data_bad)
print(f"Validation result: for data bad is {'valid' if result_bad else 'not valid'}")

# !!!! should have entries !!!
print("For data_bad validation errors:")
for error in validator.iter_errors(data_bad):
    print(f"- {error.message}")

for a less nested schema

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "json_schema.json",
    "type": "object",
    "properties": {
        "address": { "$ref": "../address/json_schema.json#/definitions/address" }
    },
    "required": ["address"]
}

it shows for this data

{
    "HEADER": {
        "file_type": "test data",
        "file_version": 1
    },
    "name": "Test Data",
    "address": {
        "street": "123 Main St"
    }
  • 'city' is a required property
  • 'postalCode' is a required property

and I would have expected the same result for the nested schema/data.
Is there an explanation why this does not happen ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions