-
Notifications
You must be signed in to change notification settings - Fork 70
/
anyofvalidator.py
53 lines (44 loc) · 2.05 KB
/
anyofvalidator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from __future__ import absolute_import
from cerberus import Validator
from cerberus import errors
class AnyofValidator(Validator):
def __init__(self, *args, **kwargs):
super(AnyofValidator, self).__init__(*args, **kwargs)
def _validate_anyof(self, definitions, field, value):
""" {'type': 'list', 'logical': 'anyof'} """
if 'role' not in list(definitions[0]['schema'].keys()):
return super(AnyofValidator, self)._validate_anyof(definitions,
field, value)
valids = 0
found_valid_role = False
_errors = errors.ErrorList()
valid_roles = []
for i, definition in enumerate(definitions):
valid_roles.extend(definition['schema']['role']['allowed'])
if value['role'] not in definition['schema']['role']['allowed']:
continue
found_valid_role = True
schema = {field: definition.copy()}
for rule in ('allow_unknown', 'type'):
if rule not in schema[field] and rule in self.schema[field]:
schema[field][rule] = self.schema[field][rule]
if 'allow_unknown' not in schema[field]:
schema[field]['allow_unknown'] = self.allow_unknown
validator = self._get_child_validator(
schema_crumb=(field, 'role', value['role']), schema=schema,
allow_unknown=True
)
if validator(self.document, update=self.update, normalize=False):
valids += 1
else:
_errors.extend(validator._errors)
if valids == 0:
if not found_valid_role:
role_error = errors.ValidationError(
("resource_definitions", 0, "role"),
("res_defs", "schema", "anyof", -1, "schema", "role"),
0x44, "rule", valid_roles, value['role'], ""
)
self._error([role_error])
else:
self._error(validator._errors)