-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathvalidators.py
More file actions
145 lines (109 loc) · 4.21 KB
/
validators.py
File metadata and controls
145 lines (109 loc) · 4.21 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Copyright 2022 The FeatHub Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from abc import ABC, abstractmethod
from typing import TypeVar, Generic, Iterable
from feathub.common.exceptions import FeathubConfigurationException
T = TypeVar("T")
class Validator(ABC, Generic[T]):
"""
Validator is used to perform single configuration validation.
"""
@abstractmethod
def ensure_valid(self, name: str, value: T) -> None:
"""
Perform single configuration validation.
:param name: The name of the configuration
:param value: The value of the configuration
:raise FeathubConfigurationException if the value is invalid.
"""
pass
class NotNoneValidator(Validator[T]):
def ensure_valid(self, name: str, value: T) -> None:
if value is None:
raise FeathubConfigurationException(f"Value of {name} cannot be None.")
def not_none() -> Validator[T]:
return NotNoneValidator()
class InListValidator(Validator[T]):
def __init__(self, *allowed: T):
self.allowed = allowed
def ensure_valid(self, name: str, value: T) -> None:
if value not in self.allowed:
raise FeathubConfigurationException(
f"Invalid value {value} of {name}: Value must be one of "
f"{', '.join([str(v) for v in self.allowed])}."
)
def in_list(*allowed: T) -> Validator[T]:
"""
Returns a validator check if the value is in the given list of allowed values.
:param allowed: Allowed values.
"""
return InListValidator(*allowed)
ITER_T = TypeVar("ITER_T", bound=Iterable)
class IsSubSetValidator(Validator[ITER_T]):
def __init__(self, *allowed: T):
self.in_set_validator = InListValidator(*allowed)
def ensure_valid(self, name: str, value: Iterable[T]) -> None:
for v in value:
self.in_set_validator.ensure_valid(name, v)
def is_subset(*allowed: T) -> Validator[ITER_T]:
"""
Returns a validator check if the elements in a collection typed value are in the
set of allowed values.
:param allowed: Allowed values.
"""
return IsSubSetValidator(*allowed)
class ComparableValidator(Validator[T]):
def __init__(self, bound: T, is_greater_than: bool, inclusive: bool):
self.is_greater_than = is_greater_than
self.bound = bound
self.inclusive = inclusive
def ensure_valid(self, name: str, value: T) -> None:
if value is None:
raise FeathubConfigurationException(
f"Value for configuration {name} is not specified."
)
if (
(self.is_greater_than and self.bound > value) # type: ignore
or (not self.is_greater_than and self.bound < value) # type: ignore
or (not self.inclusive and self.bound == value)
):
raise FeathubConfigurationException(
f"Invalid value {value} of {name}: Value must be "
f"{'greater' if self.is_greater_than else 'less'} than "
f"{'or equal to' if self.inclusive else ''} {self.bound}."
)
def lt(upper_bound: T) -> Validator[T]:
return ComparableValidator(
bound=upper_bound,
is_greater_than=False,
inclusive=False,
)
def lt_eq(upper_bound: T) -> Validator[T]:
return ComparableValidator(
bound=upper_bound,
is_greater_than=False,
inclusive=True,
)
def gt(lower_bound: T) -> Validator[T]:
return ComparableValidator(
bound=lower_bound,
is_greater_than=True,
inclusive=False,
)
def gt_eq(lower_bound: T) -> Validator[T]:
return ComparableValidator(
bound=lower_bound,
is_greater_than=True,
inclusive=True,
)