cerberus.cerberus.SchemaError: unknown rule 'anyof_schema' for field 'anyof_schema'

392 views
Skip to first unread message

kumy.c...@gmail.com

unread,
Dec 21, 2016, 10:22:32 AM12/21/16
to Eve REST Framework
Hi guys,

I'm trying a extend an already established schema.
I need a branch of the conf to accept two syntax, using *of-rules.

Cerberus (0.9.2)
http://docs.python-cerberus.org/en/stable/validation-rules.html#of-rules

Here is my test case:

```
ip_dhcp = {
'configuration_mode': { 'type': 'string', 'required': True, 'allowed': ['DHCP'] },
}

doc_dhcp_ok = { 'configuration_mode': 'DHCP' }
doc_dhcp_fail = { 'configuration_mode': 'DHCPVV' }

ip_manual = {
'configuration_mode': { 'type': 'string', 'required': True },
'address' : { 'type': 'string', 'required': True },
'netmask' : { 'type': 'string', 'required': True },
'gateway' : { 'type': 'string', 'required': False },
}

doc_manual_ok_1 = { 'configuration_mode': 'MANUAL', 'address': '1.2.3.4', 'netmask': '255.255.255.0', 'gateway': '1.2.3.1' }
doc_manual_ok_2 = { 'configuration_mode': 'MANUAL', 'address': '1.2.3.4', 'netmask': '255.255.255.0' }
doc_manual_fail = { 'configuration_mode': 'MANUAL', 'address': '1.2.3.4' }

ips_mode = [ ip_dhcp, ip_manual ]

ips = {
'ips' : {
'type': 'list',
'required': True,
'minlength': 1,
'anyof_schema': ips_mode,
}
}

doc_ips_ok_1 = [ doc_dhcp_ok ]
doc_ips_ok_2 = [ doc_dhcp_ok, doc_dhcp_ok ]
doc_ips_ok_3 = [ doc_dhcp_ok, doc_manual_ok_1 ]
doc_ips_ok_4 = [ doc_manual_ok_1, doc_manual_ok_2 ]
doc_ips_fail_1 = [ doc_dhcp_fail ]
doc_ips_fail_2 = [ doc_dhcp_fail, doc_dhcp_ok ]
doc_ips_fail_3 = [ doc_manual_ok_1, doc_manual_fail ]

from cerberus import Validator

v1 = Validator(ip_dhcp)
print v1.validate(doc_dhcp_ok) # expect True
print v1.validate(doc_dhcp_fail) # expect False

v2 = Validator(ip_manual)
print v2.validate(doc_dhcp_ok) # expect False
print v2.validate(doc_manual_ok_1) # expect True
print v2.validate(doc_manual_ok_2) # expect True
print v2.validate(doc_manual_fail) # expect False

v3 = Validator(ips)
print v3.validate([doc_dhcp_ok]) # expect True
print v3.validate([doc_manual_ok_1]) # expect True
print v3.validate([doc_manual_ok_2]) # expect True
print v3.validate([doc_dhcp_fail]) # expect False
print v3.validate([doc_manual_fail]) # expect False
print v3.validate(doc_ips_ok_1) # expect True
print v3.validate(doc_ips_ok_2) # expect True
print v3.validate(doc_ips_ok_3) # expect True
print v3.validate(doc_ips_ok_4) # expect True
print v3.validate(doc_ips_fail_1) # expect False
print v3.validate(doc_ips_fail_2) # expect False
print v3.validate(doc_ips_fail_3) # expect False
```

But I'm getting:

$ python test.py
True
False
False
True
True
False
Traceback (most recent call last):
File "test.py", line 52, in <module>
v3 = Validator(ips)
File "/usr/local/lib/python2.7/dist-packages/cerberus/cerberus.py", line 175, in __init__
self.validate_schema(self.schema)
File "/usr/local/lib/python2.7/dist-packages/cerberus/cerberus.py", line 451, in validate_schema
constraint, field))
cerberus.cerberus.SchemaError: unknown rule 'anyof_schema' for field 'anyof_schema'


What I'm doing wrong?
Thanks for your help.

Bests
Mathieu

kumy.c...@gmail.com

unread,
Dec 21, 2016, 11:19:00 AM12/21/16
to Eve REST Framework, kumy.c...@gmail.com
Hum strange, I cannot get the example from http://docs.python-cerberus.org/en/stable/validation-rules.html#of-rules-typesaver to work

from cerberus import Validator

schemas = [{'department': {'required': True, 'regex': '^IT$'}, 'phone': {'nullable': True}},
{'department': {'required': True}, 'phone': {'required': True}}]
employee_vldtr = Validator({'employee': {'oneof_schema': schemas, 'type': 'dict'}}, allow_unknown=True)
invalid_employees_phones = []
for employee in employees:
if not employee_vldtr.validate(employee):
invalid_employees_phones.append(employee)


$ python test.py

Traceback (most recent call last):

File "test.py", line 5, in <module>
employee_vldtr = Validator({'employee': {'oneof_schema': schemas, 'type': 'dict'}}, allow_unknown=True)


File "/usr/local/lib/python2.7/dist-packages/cerberus/cerberus.py", line 175, in __init__
self.validate_schema(self.schema)
File "/usr/local/lib/python2.7/dist-packages/cerberus/cerberus.py", line 451, in validate_schema
constraint, field))

cerberus.cerberus.SchemaError: unknown rule 'oneof_schema' for field 'oneof_schema'


(BTW the is a typo in the example ; 'emloyee_vldtr' vs 'employee_vldtr'

Reply all
Reply to author
Forward
0 new messages