5

我有以下架构:

schema = {
    'person': {
        'name': {'type': 'string', 'required': True, 'minlength': 2},
        'gender': {'type': 'integer', 'required': True},
        'weight': {'type': 'integer', 'required': True},
        'age': {'type': 'integer', 'required': True, 'min': 5}
    },
    'parameters': {
        'ps': {'type': 'float', 'required': True},
        'pp': {'type': 'float', 'required': True},
        'ls': {'type': 'float', 'required': True},
        'lp': {'type': 'float', 'required': True},
        'ab': {'type': 'float', 'required': True}
    },
    'indicators': {
        'atmospheric_pressure': {'type': 'integer', 'required': True},
        'puls': {'type': 'integer', 'required': True},
        'respiration_rate': {'type': 'integer', 'required': True}
    }
}

当我运行验证时:

v = Validator(schema)

我收到一个错误:

Traceback (most recent call last):
  File "index.py", line 53, in <module>
    v = Validator(schema)
  File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36\lib\site-packages\cerberus\validator.py", line 143, in __init__
    self.schema = kwargs.get('schema', None)
  File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36\lib\site-packages\cerberus\validator.py", line 472, in schema
    self._schema = DefinitionSchema(self, schema)
  File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36\lib\site-packages\cerberus\schema.py", line 56, in __init__
    self.validate(schema)
  File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36\lib\site-packages\cerberus\schema.py", line 181, in validate
    self._validate(schema)
  File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36\lib\site-packages\cerberus\schema.py", line 203, in _validate
    raise SchemaError(self.schema_validator.errors)
cerberus.schema.SchemaError: {'indicators': [{'atmospheric_pressure': ['unknown rule'], 'puls': ['unknown rule'], 'respiration_rate': ['unknown rule']}], 'parameters': [{'ab': ['unknown rule'], 'lp': ['unknown rule'], 'ls': ['unknown rule'], 'pp': ['unknown rule'], 'ps': ['unknown rule']}], 'person': [{'age': ['unknown rule'], 'gender': ['unknown rule'], 'name': ['unknown rule'], 'weight': ['unknown rule']}]}

我也尝试指定类型:

schema = {
    'person': {
        'type': 'object',
        'schema': {
            'name': {'type': 'string', 'required': True, 'minlength': 2},
            'gender': {'type': 'integer', 'required': True},
            'weight': {'type': 'integer', 'required': True},
            'age': {'type': 'integer', 'required': True, 'min': 5}
        }
    },
4

1 回答 1

9

您需要指定typedict.

以下应该有效:

from cerberus import Validator

schema = {
    'person': {
        'type': 'dict',
        'schema': {
            'name': {'type': 'string', 'required': True, 'minlength': 2},
            'gender': {'type': 'integer', 'required': True},
            'weight': {'type': 'integer', 'required': True},
            'age': {'type': 'integer', 'required': True, 'min': 5}
        }
    },
    'parameters': {
        'type': 'dict',
        'schema':{
            'ps': {'type': 'float', 'required': True},
            'pp': {'type': 'float', 'required': True},
            'ls': {'type': 'float', 'required': True},
            'lp': {'type': 'float', 'required': True},
            'ab': {'type': 'float', 'required': True}
        },
    }
}
document = {
    'person':{
        'name':'John',
        'gender': 1,
        'weight': 180,
        'age': 27
    }
}

v = Validator()
v.schema = schema
print(v.schema)
print(v.validate(document, schema))

输出:

{'person': {'type': 'dict', 'schema': {'name': {'type': 'string', 'required': True, 'minlength': 2}, 'gender': {'type': 'integer', 'required': True}, 'weight': {'type': 'integer', 'required': True}, 'age': {'type': 'integer', 'required': True, 'min': 5}}}, 'parameters': {'type': 'dict', 'schema': {'ps': {'type': 'float', 'required': True}, 'pp': {'type': 'float', 'required': True}, 'ls': {'type': 'float', 'required': True}, 'lp': {'type': 'float', 'required': True}, 'ab': {'type': 'float', 'required': True}}}}
True
于 2018-02-22T00:07:12.090 回答