5

I have the following schema:

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

When I run validation:

v = Validator(schema)

I get an error:

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

Also i tried to specify type:

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}
        }
    },
dreftymac
  • 31,404
  • 26
  • 119
  • 182

1 Answers1

11

You need to specify type as dict.

The following should work:

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))

Output:

{'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
Kyrylo
  • 651
  • 1
  • 6
  • 15
  • Excellent, will try your solve, are you sure it works? –  Feb 22 '18 at 00:16
  • For me it works. I used `validate` method, so for different inputs it gave me different results. Try to change the type of any value in document variable ( let gender be **M** instead of **1** ) and the output of the validate function will be `False`, because gender should be integer. – Kyrylo Feb 22 '18 at 10:04
  • Bu where object ` 'parameters': {}`? –  Feb 22 '18 at 17:07
  • @Amely I have solved it for the lower case. You can just add parameters values to the dictionary and it should work. I will try to do this as well in hour. – Kyrylo Feb 22 '18 at 17:15
  • Man I get an error: `is not a document, must be a dict"` –  Feb 22 '18 at 17:39
  • I will update the solution with added parameters values. – Kyrylo Feb 22 '18 at 17:45
  • @Amely Check if you have the same output. – Kyrylo Feb 22 '18 at 17:47