-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.py
55 lines (43 loc) · 1.57 KB
/
validator.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
54
55
"""
Custom Cerberus validators
"""
from cerberus import TypeDefinition, Validator
from .tags import RefTag
class ArchitectureValidator(Validator):
"""
Validates architecture definitions
"""
__ref_type = TypeDefinition("reference", (RefTag,), ())
types_mapping = Validator.types_mapping.copy()
types_mapping["reference"] = __ref_type
class PropertyValidator(ArchitectureValidator):
"""
Validates component properties
"""
def __init__(self, *args, **kwargs) -> None:
if "components" in kwargs:
self.components = kwargs["components"]
super(PropertyValidator, self).__init__(*args, **kwargs)
def _validate_ref_type(self, required_type, field, referenced_component):
"""
Makes sure that the reference is of the correct type
The rule's arguments are validated against this schema:
{'type': 'string'}
"""
if not isinstance(referenced_component, RefTag):
return
found: bool = False
for component in self.components:
if component["name"] == referenced_component.value:
found = True
typee = component["type"]
if typee != required_type:
self._error(
field,
f"Invalid type in component {referenced_component} in field {field}: {typee}, expected {required_type}",
)
if not found:
self._error(
field,
f"No component {referenced_component} found",
)