N
N
Nast22019-07-18 16:19:31
Python
Nast2, 2019-07-18 16:19:31

Python jsonschema, why does schema validation fail?

I have :
python: 3.7
jsonschema: 3.0.1 I
define the following schema:

schema

{
        'definitions': {
            'territory_item': {
                'type': 'object',
                'properties': {
                    'territory_guid': {
                        'type': 'string', 'format': 'uuid'
                    },
                    'name': {
                        'type': 'string'
                    },
                    'child_territories': {'$ref': '#/definitions/territory'},
                },
                'required': ['territory_guid', 'name']
            },
            'territory': {
                'oneOf': [
                    {'type': 'object', 'properties': {'$ref': '#/definitions/territory_item'}},
                    {'type': 'array', 'items': {'$ref': '#/definitions/territory_item'}}
                ]
            }
        },
        
        'type': 'object',
        'properties': {'$ref': '#/definitions/territory'}
}


But when I try to use it for validation, I get the following error:
exception

SchemaError: '#/definitions/territory_item' is not of type 'object', 'boolean'

Failed validating 'type' in metaschema['properties']['definitions']['additionalProperties']['properties']['oneOf']['items']['properties']['properties']['additionalProperties']:
    {'$id': 'http://json-schema.org/draft-07/schema#',
     '$schema': 'http://json-schema.org/draft-07/schema#',
     'default': True,
     'definitions': {'nonNegativeInteger': {'minimum': 0,
                                            'type': 'integer'},
                     'nonNegativeIntegerDefault0': {'allOf': [{'$ref': '#/definitions/nonNegativeInteger'},
                                                              {'default': 0}]},
                     'schemaArray': {'items': {'$ref': '#'},
                                     'minItems': 1,
                                     'type': 'array'},
                     'simpleTypes': {'enum': ['array',
                                              'boolean',
                                              'integer',
                                              'null',
                                              'number',
                                              'object',
                                              'string']},
                     'stringArray': {'default': [],
                                     'items': {'type': 'string'},
                                     'type': 'array',
                                     'uniqueItems': True}},
     'properties': {'$comment': {'type': 'string'},
                    '$id': {'format': 'uri-reference', 'type': 'string'},
                    '$ref': {'format': 'uri-reference', 'type': 'string'},
                    '$schema': {'format': 'uri', 'type': 'string'},
                    'additionalItems': {'$ref': '#'},
                    'additionalProperties': {'$ref': '#'},
                    'allOf': {'$ref': '#/definitions/schemaArray'},
                    'anyOf': {'$ref': '#/definitions/schemaArray'},
                    'const': True,
                    'contains': {'$ref': '#'},
                    'contentEncoding': {'type': 'string'},
                    'contentMediaType': {'type': 'string'},
                    'default': True,
                    'definitions': {'additionalProperties': {'$ref': '#'},
                                    'default': {},
                                    'type': 'object'},
                    'dependencies': {'additionalProperties': {'anyOf': [{'$ref': '#'},
                                                                        {'$ref': '#/definitions/stringArray'}]},
                                     'type': 'object'},
                    'description': {'type': 'string'},
                    'else': {'$ref': '#'},
                    'enum': {'items': True, 'type': 'array'},
                    'examples': {'items': True, 'type': 'array'},
                    'exclusiveMaximum': {'type': 'number'},
                    'exclusiveMinimum': {'type': 'number'},
                    'format': {'type': 'string'},
                    'if': {'$ref': '#'},
                    'items': {'anyOf': [{'$ref': '#'},
                                        {'$ref': '#/definitions/schemaArray'}],
                              'default': True},
                    'maxItems': {'$ref': '#/definitions/nonNegativeInteger'},
                    'maxLength': {'$ref': '#/definitions/nonNegativeInteger'},
                    'maxProperties': {'$ref': '#/definitions/nonNegativeInteger'},
                    'maximum': {'type': 'number'},
                    'minItems': {'$ref': '#/definitions/nonNegativeIntegerDefault0'},
                    'minLength': {'$ref': '#/definitions/nonNegativeIntegerDefault0'},
                    'minProperties': {'$ref': '#/definitions/nonNegativeIntegerDefault0'},
                    'minimum': {'type': 'number'},
                    'multipleOf': {'exclusiveMinimum': 0, 'type': 'number'},
                    'not': {'$ref': '#'},
                    'oneOf': {'$ref': '#/definitions/schemaArray'},
                    'pattern': {'format': 'regex', 'type': 'string'},
                    'patternProperties': {'additionalProperties': {'$ref': '#'},
                                          'default': {},
                                          'propertyNames': {'format': 'regex'},
                                          'type': 'object'},
                    'properties': {'additionalProperties': {'$ref': '#'},
                                   'default': {},
                                   'type': 'object'},
                    'propertyNames': {'$ref': '#'},
                    'readOnly': {'default': False, 'type': 'boolean'},
                    'required': {'$ref': '#/definitions/stringArray'},
                    'then': {'$ref': '#'},
                    'title': {'type': 'string'},
                    'type': {'anyOf': [{'$ref': '#/definitions/simpleTypes'},
                                       {'items': {'$ref': '#/definitions/simpleTypes'},
                                        'minItems': 1,
                                        'type': 'array',
                                        'uniqueItems': True}]},
                    'uniqueItems': {'default': False, 'type': 'boolean'}},
     'title': 'Core schema meta-schema',
     'type': ['object', 'boolean']}

On schema['definitions']['territory']['oneOf'][0]['properties']['$ref']:
    '#/definitions/territory_item'


Where is the mistake?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nast2, 2019-07-19
@Nast2

I found what was the problem, I am attaching a valid schema:

schema
{
        'definitions': {
            'territory_item': {
                'type': 'object',
                'properties': {
                    'territory_guid': {
                        'type': 'string', 'format': 'uuid'
                    },
                    'name': {
                        'type': 'string'
                    },
                    'child_territories': {
                        'territory:': {'$ref': '#/definitions/territory'}
                    },
                },
                'required': ['territory_guid', 'name']
            },
            'territory': {
                'oneOf': [
                    {'$ref': '#/definitions/territory_item'},
                    {'type': 'array', 'items': {'$ref': '#/definitions/territory_item'}}
                ],
            }
        },
        
        'type': 'object',
        'properties': {
            'territory': {'$ref': '#/definitions/territory'}
        },
        'required': ['territory']
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question