Skip to content

Problem with custom types: they are used for both instance and schema #380

@mitar

Description

@mitar

It seems this package is using custom types for both instance and schema. This is making a problem for me because I have instances which are immutable objects (I use tuples instead of lists, and frozendict instead of dicts) and I would like to validate them against a JSON schema which are normal dicts and lists. But this does not seem possible because code uses things like: validator.is_type(items, "object") to check if schema is describing an object or not, but if object is a frozendict, this breaks validation. Even if my schema itself passed validation.

Example.

import frozendict
import jsonschema

schema = {
    'type': 'object',
    'properties': {
        'tags': {
            'type': 'array',
            'items': {
                'type': 'string'
            }
        }
    }
}

jsonschema.Draft4Validator.check_schema(schema)

validator = jsonschema.Draft4Validator(
    schema=schema,
    types=dict(jsonschema.Draft4Validator.DEFAULT_TYPES, **{
        'array': tuple,
        'object': frozendict.frozendict
    })
)

validator.validate(frozendict.frozendict({'tags': ('foobar',)}))

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