safeshield 1.3.2__py3-none-any.whl → 1.4.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,165 +0,0 @@
1
- from .base import ValidationRule
2
- from typing import Any, Dict, List, Optional, Set, Union, Tuple, Type
3
- from enum import Enum
4
- from .basic import RequiredRule, ProhibitedRule, PresentRule, MissingRule, ExcludeRule
5
- import re
6
- import inspect
7
- from collections.abc import Iterable
8
-
9
- # =============================================
10
- # CONDITIONAL VALIDATION RULES
11
- # =============================================
12
-
13
- class RequiredIfRule(RequiredRule):
14
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
15
- if len(params) < 2 or len(params) % 2 != 0:
16
- return True
17
-
18
- conditions = list(zip(params[::2], params[1::2]))
19
-
20
- condition_met = False
21
- for other_field, expected_value in conditions:
22
- if not other_field or expected_value is None:
23
- continue
24
-
25
- actual_value = self.get_field_value(other_field, '')
26
-
27
- if actual_value == expected_value:
28
- condition_met = True
29
- break
30
-
31
- if not condition_met:
32
- return True
33
-
34
- return super().validate(field, value, params)
35
-
36
- class RequiredUnlessRule(RequiredRule):
37
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
38
- if len(params) < 2:
39
- return False
40
-
41
- other_field, other_value = params[0], params[1]
42
- actual_value = self.get_field_value(other_field, '')
43
-
44
- if actual_value == other_value:
45
- return True
46
-
47
- return super().validate(field, value, params)
48
-
49
- class RequiredAllIfRule(RequiredRule):
50
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
51
- if len(params) < 2:
52
- return False
53
-
54
- conditions = [(f.strip(), v.strip()) for f, v in zip(params[::2], params[1::2])]
55
-
56
- all_conditions_met = all(
57
- self.get_field_value(f) == v
58
- for f, v in conditions
59
- )
60
-
61
- if not all_conditions_met:
62
- return True
63
-
64
- return super().validate(field, value, params)
65
-
66
- class ProhibitedIfRule(ProhibitedRule):
67
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
68
- if len(params) < 2:
69
- return False
70
-
71
- other_field, other_value = params[0], params[1]
72
- actual_value = self.get_field_value(other_field, '')
73
-
74
- if actual_value != other_value:
75
- return True
76
-
77
- return super().validate(field, value, params)
78
-
79
- class ProhibitedUnlessRule(ProhibitedRule):
80
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
81
- if len(params) < 2:
82
- return False
83
-
84
- other_field, other_value = params[0], params[1]
85
- actual_value = self.get_field_value(other_field, '')
86
-
87
- if actual_value == other_value:
88
- return True
89
-
90
- return super().validate(field, value, params)
91
-
92
- class PresentIfRule(PresentRule):
93
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
94
- if len(params) < 2:
95
- return False
96
- other_field, other_value = params[0], params[1]
97
-
98
- if self.get_field_value(other_field, None) == other_value:
99
- return super().validate(field, value, params)
100
- return True
101
-
102
- class PresentUnlessRule(PresentRule):
103
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
104
- if len(params) < 2:
105
- return False
106
- other_field, other_value = params[0], params[1]
107
-
108
- if self.get_field_value(other_field, None) != other_value:
109
- return super().validate(field, value, params)
110
-
111
- return True
112
-
113
- class MissingIfRule(MissingRule):
114
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
115
- if len(params) < 2:
116
- return False
117
- other_field, other_value = params[0], params[1]
118
-
119
- if self.get_field_value(other_field, None) == other_value:
120
- return super().validate(field, value, params)
121
-
122
- return True
123
-
124
- class MissingUnlessRule(MissingRule):
125
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
126
- if len(params) < 2:
127
- return False
128
- other_field, other_value = params[0], params[1]
129
- if self.get_field_value(other_field, None) != other_value:
130
- return super().validate(field, value, params)
131
-
132
- return True
133
-
134
- class ExcludeIfRule(ExcludeRule):
135
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
136
- conditions = [(f.strip(), v.strip()) for f, v in zip(params[::2], params[1::2])]
137
-
138
- all_conditions_met = all(
139
- self.get_field_value(f) == v
140
- for f, v in conditions
141
- )
142
-
143
- if all_conditions_met:
144
- return super().validate(field, value, params)
145
- return True
146
-
147
- def message(self, field: str, params: List[str]) -> str:
148
- return f"The :attribute field is excluded when {params[0]} is {params[1]}."
149
-
150
- class ExcludeUnlessRule(ExcludeRule):
151
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
152
- conditions = [(f.strip(), v.strip()) for f, v in zip(params[::2], params[1::2])]
153
-
154
- all_conditions_met = all(
155
- self.get_field_value(f) == v
156
- for f, v in conditions
157
- )
158
-
159
- if not all_conditions_met:
160
- return super().validate(field, value, params)
161
-
162
- return True
163
-
164
- def message(self, field: str, params: List[str]) -> str:
165
- return f"The :attribute field is excluded unless {params[0]} is {params[1]}."
validator/rules/type.py DELETED
@@ -1,49 +0,0 @@
1
- from .base import ValidationRule
2
- from typing import Any, Dict, List, Optional, Set, Union, Tuple, Type
3
- from datetime import datetime
4
-
5
- # =============================================
6
- # TYPE DATA VALIDATION RULES
7
- # =============================================
8
-
9
- class StringRule(ValidationRule):
10
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
11
- return isinstance(value, str)
12
-
13
- def message(self, field: str, params: List[str]) -> str:
14
- return f"The :attribute must be a string."
15
-
16
- class NumericRule(ValidationRule):
17
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
18
- if isinstance(value, (int, float)):
19
- return True
20
- if not isinstance(value, str):
21
- return False
22
- return value.replace('.', '', 1).isdigit()
23
-
24
- def message(self, field: str, params: List[str]) -> str:
25
- return f"The :attribute must be a number."
26
-
27
- class IntegerRule(ValidationRule):
28
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
29
- if isinstance(value, int):
30
- return True
31
- if not isinstance(value, str):
32
- return False
33
- return value.isdigit()
34
-
35
- def message(self, field: str, params: List[str]) -> str:
36
- return f"The :attribute must be an integer."
37
-
38
- class BooleanRule(ValidationRule):
39
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
40
- if isinstance(value, bool):
41
- return True
42
- if isinstance(value, str):
43
- return value.lower() in ['true', 'false', '1', '0', 'yes', 'no', 'on', 'off']
44
- if isinstance(value, int):
45
- return value in [0, 1]
46
- return False
47
-
48
- def message(self, field: str, params: List[str]) -> str:
49
- return f"The :attribute field must be true or false."