@Nast2

Python jsonschema, почему возникает ошибка при валидации схемы?

Имеется:
python: 3.7
jsonschema: 3.0.1

Определяю следующую схему:

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'}
}



Но при попытке использовать ее для валидации получаю следующую ошибку:

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'



В чем ошибка?
  • Вопрос задан
  • 296 просмотров
Решения вопроса 1
@Nast2 Автор вопроса
Я нашел в чем была проблема, прикладываю валидную схему:

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']
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы