Debian Codegraph @

strictyaml/validators.py

raw
1 from strictyaml.exceptions import YAMLValidationError, YAMLSerializationError
2 from strictyaml.exceptions import InvalidValidatorError
3 from strictyaml.representation import YAML
4 from strictyaml import utils
5 import sys
6
7
8 if sys.version_info[0] == 3:
9 unicode = str
10
11
12 class Validator(object):
13 def __or__(self, other):
14 return OrValidator(self, other)
15
16 def __call__(self, chunk):
17 self.validate(chunk)
18 return YAML(chunk, validator=self)
19
20 def __repr__(self):
21 return "{0}()".format(self.__class__.__name__)
22
23
24 class MapValidator(Validator):
25 def _should_be_mapping(self, data):
26 if not isinstance(data, dict):
27 raise YAMLSerializationError("Expected a dict, found '{}'".format(data))
28 if len(data) == 0:
29 raise YAMLSerializationError(
30 (
31 "Expected a non-empty dict, found an empty dict.\n"
32 "Use EmptyDict validator to serialize empty dicts."
33 )
34 )
35
36
37 class SeqValidator(Validator):
38 def _should_be_list(self, data):
39 if not isinstance(data, list):
40 raise YAMLSerializationError("Expected a list, found '{}'".format(data))
41 if len(data) == 0:
42 raise YAMLSerializationError(
43 (
44 "Expected a non-empty list, found an empty list.\n"
45 "Use EmptyList validator to serialize empty lists."
46 )
47 )
48
49
50 class OrValidator(Validator):
51 def __init__(self, validator_a, validator_b):
52 assert isinstance(validator_a, Validator), "validator_a must be a Validator"
53 assert isinstance(validator_b, Validator), "validator_b must be a Validator"
54
55 self._validator_a = validator_a
56 self._validator_b = validator_b
57
58 def unpacked(validator):
59 if isinstance(validator, OrValidator):
60 return [
61 unpacked(validator._validator_a),
62 unpacked(validator._validator_b),
63 ]
64 else:
65 return [validator]
66
67 map_validator_count = len(
68 [
69 validator
70 for validator in list(utils.flatten(unpacked(self)))
71 if isinstance(validator, MapValidator)
72 ]
73 )
74
75 if map_validator_count > 1:
76 raise InvalidValidatorError(
77 (
78 "You tried to Or ('|') together {} Map validators. "
79 "Try using revalidation instead."
80 ).format(map_validator_count)
81 )
82
83 seq_validator_count = len(
84 [
85 validator
86 for validator in list(utils.flatten(unpacked(self)))
87 if isinstance(validator, SeqValidator)
88 ]
89 )
90
91 if seq_validator_count > 1:
92 raise InvalidValidatorError(
93 (
94 "You tried to Or ('|') together {} Seq validators. "
95 "Try using revalidation instead."
96 ).format(seq_validator_count)
97 )
98
99 def to_yaml(self, value):
100 try:
101 return self._validator_a.to_yaml(value)
102 except YAMLSerializationError:
103 return self._validator_b.to_yaml(value)
104
105 def __call__(self, chunk):
106 try:
107 result = self._validator_a(chunk)
108 result._selected_validator = result._validator
109 result._validator = self
110 return result
111 except YAMLValidationError:
112 result = self._validator_b(chunk)
113 result._selected_validator = result._validator
114 result._validator = self
115 return result
116
117 def __repr__(self):
118 return "{0} | {1}".format(repr(self._validator_a), repr(self._validator_b))