Skip to content

Cannot use error.schema_path to index into error.schema when in a subschema #120

@martinpengellyphillips

Description

@martinpengellyphillips

It seems that error.schema_path always refers to a path in the full schema, whilst error.schema may only be a subschema fragment. This makes it hard (impossible?) to use error.schema_path to index into the relevant schema.

from jsonschema import Draft4Validator as Validator

schema = {
    'type': 'object',
    'properties': {
        'a': {
            'type': 'object',
            'properties': {
                'nested': {
                    'type': 'string',
                    'pattern': '^hello$'
                }
            }
        }
    }
}

errors = Validator(schema).iter_errors({'a': {'nested':'world'}})
for error in errors:
    print error.schema_path, error.schema

# deque(['properties', 'a', 'properties', 'nested', 'pattern']) {'pattern': '^hello$', 'type': 'string'}

Note: For my purposes it would be more useful to access the full schema from the error rather than constrict the schema_path.

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