safeshield 1.4.11__py3-none-any.whl → 1.5.2__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.
@@ -25,10 +25,9 @@ class IntegerRule(Rule):
25
25
  return f"The :attribute must be an integer."
26
26
 
27
27
  class DigitsRule(Rule):
28
+ _count_parameter = 1
29
+
28
30
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
29
- if not params or not isinstance(value, str):
30
- return False
31
-
32
31
  try:
33
32
  digits = int(params[0])
34
33
  except ValueError:
@@ -37,13 +36,21 @@ class DigitsRule(Rule):
37
36
  return value.isdigit() and len(value) == digits
38
37
 
39
38
  def message(self, field: str, params: List[str]) -> str:
40
- return f"The :attribute must be {params[0]} digits."
39
+ return f"The :attribute must be :value digits."
40
+
41
+ def replacements(self, field, value):
42
+ replacements = {
43
+ ':attribute': self._get_display_name(field),
44
+ ':input': value,
45
+ ':value': self._params[0],
46
+ }
47
+
48
+ return replacements
41
49
 
42
50
  class DigitsBetweenRule(Rule):
51
+ _count_parameter = 2
52
+
43
53
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
44
- if len(params) < 2 or not isinstance(value, str):
45
- return False
46
-
47
54
  try:
48
55
  min_digits = int(params[0])
49
56
  max_digits = int(params[1])
@@ -53,7 +60,17 @@ class DigitsBetweenRule(Rule):
53
60
  return value.isdigit() and min_digits <= len(value) <= max_digits
54
61
 
55
62
  def message(self, field: str, params: List[str]) -> str:
56
- return f"The :attribute must be between {params[0]} and {params[1]} digits."
63
+ return f"The :attribute must be between :min and :max digits."
64
+
65
+ def replacements(self, field, value):
66
+ replacements = {
67
+ ':attribute': self._get_display_name(field),
68
+ ':input': value,
69
+ ':min': self._params[0],
70
+ ':max': self._params[1],
71
+ }
72
+
73
+ return replacements
57
74
 
58
75
  class DecimalRule(Rule):
59
76
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
@@ -68,11 +85,9 @@ class DecimalRule(Rule):
68
85
 
69
86
  class GreaterThanRule(Rule):
70
87
  _name = 'gt'
88
+ _count_parameter = 1
71
89
 
72
90
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
73
- if len(params) < 1:
74
- return False
75
-
76
91
  try:
77
92
  threshold = decimal.Decimal(params[0])
78
93
  numeric_value = decimal.Decimal(str(value))
@@ -81,15 +96,22 @@ class GreaterThanRule(Rule):
81
96
  return False
82
97
 
83
98
  def message(self, field: str, params: List[str]) -> str:
84
- return f"The :attribute must be greater than {params[0]}."
99
+ return f"The :attribute must be greater than :value."
100
+
101
+ def replacements(self, field, value):
102
+ replacements = {
103
+ ':attribute': self._get_display_name(field),
104
+ ':input': value,
105
+ ':value': self._params[0],
106
+ }
107
+
108
+ return replacements
85
109
 
86
110
  class GreaterThanOrEqualRule(Rule):
87
111
  _name = 'gte'
112
+ _count_parameter = 1
88
113
 
89
114
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
90
- if len(params) < 1:
91
- return False
92
-
93
115
  try:
94
116
  threshold = decimal.Decimal(params[0])
95
117
  numeric_value = decimal.Decimal(str(value))
@@ -98,15 +120,22 @@ class GreaterThanOrEqualRule(Rule):
98
120
  return False
99
121
 
100
122
  def message(self, field: str, params: List[str]) -> str:
101
- return f"The :attribute must be greater than or equal to {params[0]}."
123
+ return f"The :attribute must be greater than or equal to :value."
124
+
125
+ def replacements(self, field, value):
126
+ replacements = {
127
+ ':attribute': self._get_display_name(field),
128
+ ':input': value,
129
+ ':value': self._params[0],
130
+ }
131
+
132
+ return replacements
102
133
 
103
134
  class LessThanRule(Rule):
104
135
  _name = 'lt'
136
+ _count_parameter = 1
105
137
 
106
138
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
107
- if len(params) < 1:
108
- return False
109
-
110
139
  try:
111
140
  threshold = decimal.Decimal(params[0])
112
141
  numeric_value = decimal.Decimal(str(value))
@@ -115,15 +144,22 @@ class LessThanRule(Rule):
115
144
  return False
116
145
 
117
146
  def message(self, field: str, params: List[str]) -> str:
118
- return f"The :attribute must be less than {params[0]}."
147
+ return f"The :attribute must be less than :value."
148
+
149
+ def replacements(self, field, value):
150
+ replacements = {
151
+ ':attribute': self._get_display_name(field),
152
+ ':input': value,
153
+ ':value': self._params[0],
154
+ }
155
+
156
+ return replacements
119
157
 
120
158
  class LessThanOrEqualRule(Rule):
121
159
  _name = 'lte'
160
+ _count_parameter = 1
122
161
 
123
162
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
124
- if len(params) < 1:
125
- return False
126
-
127
163
  try:
128
164
  threshold = decimal.Decimal(params[0])
129
165
  numeric_value = decimal.Decimal(str(value))
@@ -132,13 +168,21 @@ class LessThanOrEqualRule(Rule):
132
168
  return False
133
169
 
134
170
  def message(self, field: str, params: List[str]) -> str:
135
- return f"The :attribute must be less than or equal to {params[0]}."
171
+ return f"The :attribute must be less than or equal to :value."
172
+
173
+ def replacements(self, field, value):
174
+ replacements = {
175
+ ':attribute': self._get_display_name(field),
176
+ ':input': value,
177
+ ':value': self._params[0],
178
+ }
179
+
180
+ return replacements
136
181
 
137
182
  class MaxDigitsRule(Rule):
183
+ _count_parameter = 1
184
+
138
185
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
139
- if len(params) < 1:
140
- return False
141
-
142
186
  try:
143
187
  max_digits = int(params[0])
144
188
  numeric_value = decimal.Decimal(str(value))
@@ -150,13 +194,21 @@ class MaxDigitsRule(Rule):
150
194
  return False
151
195
 
152
196
  def message(self, field: str, params: List[str]) -> str:
153
- return f"The :attribute must not exceed {params[0]} digits."
197
+ return f"The :attribute must not exceed :value digits."
198
+
199
+ def replacements(self, field, value):
200
+ replacements = {
201
+ ':attribute': self._get_display_name(field),
202
+ ':input': value,
203
+ ':value': self._params[0],
204
+ }
205
+
206
+ return replacements
154
207
 
155
208
  class MinDigitsRule(Rule):
209
+ _count_parameter = 1
210
+
156
211
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
157
- if len(params) < 1:
158
- return False
159
-
160
212
  try:
161
213
  min_digits = int(params[0])
162
214
  numeric_value = decimal.Decimal(str(value))
@@ -168,13 +220,21 @@ class MinDigitsRule(Rule):
168
220
  return False
169
221
 
170
222
  def message(self, field: str, params: List[str]) -> str:
171
- return f"The :attribute must have at least {params[0]} digits."
223
+ return f"The :attribute must have at least :value digits."
224
+
225
+ def replacements(self, field, value):
226
+ replacements = {
227
+ ':attribute': self._get_display_name(field),
228
+ ':input': value,
229
+ ':value': self._params[0],
230
+ }
231
+
232
+ return replacements
172
233
 
173
234
  class MultipleOfRule(Rule):
235
+ _count_parameter = 1
236
+
174
237
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
175
- if not params:
176
- return False
177
-
178
238
  try:
179
239
  divisor = float(params[0])
180
240
  if divisor == 0:
@@ -185,4 +245,13 @@ class MultipleOfRule(Rule):
185
245
  return False
186
246
 
187
247
  def message(self, field: str, params: List[str]) -> str:
188
- return f"The :attribute must be a multiple of {params[0]}."
248
+ return f"The :attribute must be a multiple of :value."
249
+
250
+ def replacements(self, field, value):
251
+ replacements = {
252
+ ':attribute': self._get_display_name(field),
253
+ ':input': value,
254
+ ':value': self._params[0]
255
+ }
256
+
257
+ return replacements
validator/rules/string.py CHANGED
@@ -56,40 +56,84 @@ class AsciiRule(Rule):
56
56
  return f"The :attribute must only contain ASCII characters."
57
57
 
58
58
  class StartsWithRule(Rule):
59
+ _count_parameter = 1
60
+
59
61
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
60
62
  if not isinstance(value, str):
61
63
  return False
62
64
  return any(value.startswith(p) for p in params)
63
65
 
64
66
  def message(self, field: str, params: List[str]) -> str:
65
- return f"The :attribute must start with one of the following: {', '.join(params)}."
67
+ return f"The :attribute must start with one of the following: :values."
68
+
69
+ def replacements(self, field, value):
70
+ replacements = {
71
+ ':attribute': self._get_display_name(field),
72
+ ':input': value,
73
+ ':values': ', '.join(self._params),
74
+ }
75
+
76
+ return replacements
66
77
 
67
78
  class DoesntStartWithRule(Rule):
79
+ _count_parameter = 1
80
+
68
81
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
69
- if not isinstance(value, str) or not params:
82
+ if not isinstance(value, str):
70
83
  return False
71
84
  return all(not value.startswith(prefix) for prefix in params)
72
85
 
73
86
  def message(self, field: str, params: List[str]) -> str:
74
- return f"The :attribute must not start with any of: {', '.join(params)}"
87
+ return f"The :attribute must not start with any of: :values"
88
+
89
+ def replacements(self, field, value):
90
+ replacements = {
91
+ ':attribute': self._get_display_name(field),
92
+ ':input': value,
93
+ ':values': ', '.join(self._params),
94
+ }
95
+
96
+ return replacements
75
97
 
76
98
  class EndsWithRule(Rule):
99
+ _count_parameter = 1
100
+
77
101
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
78
102
  if not isinstance(value, str):
79
103
  return False
80
104
  return any(value.endswith(p) for p in params)
81
105
 
82
106
  def message(self, field: str, params: List[str]) -> str:
83
- return f"The :attribute must end with one of the following: {', '.join(params)}."
107
+ return f"The :attribute must end with one of the following: :values."
108
+
109
+ def replacements(self, field, value):
110
+ replacements = {
111
+ ':attribute': self._get_display_name(field),
112
+ ':input': value,
113
+ ':values': ', '.join(self._params),
114
+ }
115
+
116
+ return replacements
84
117
 
85
118
  class DoesntEndWithRule(Rule):
119
+ _count_parameter = 1
120
+
86
121
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
87
122
  if not isinstance(value, str) or not params:
88
123
  return False
89
124
  return all(not value.endswith(suffix) for suffix in params)
90
125
 
91
126
  def message(self, field: str, params: List[str]) -> str:
92
- return f"The :attribute must not end with any of: {', '.join(params)}"
127
+ return f"The :attribute must not end with any of: :values"
128
+
129
+ def replacements(self, field, value):
130
+ replacements = {
131
+ ':attribute': self._get_display_name(field),
132
+ ':input': value,
133
+ ':values': ', '.join(self._params),
134
+ }
135
+
136
+ return replacements
93
137
 
94
138
  class ConfirmedRule(Rule):
95
139
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
@@ -101,8 +145,10 @@ class ConfirmedRule(Rule):
101
145
  return f"The :attribute confirmation does not match."
102
146
 
103
147
  class RegexRule(Rule):
148
+ _count_parameter = 1
149
+
104
150
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
105
- if not params or not isinstance(value, str):
151
+ if not isinstance(value, str):
106
152
  return False
107
153
  try:
108
154
  return bool(re.fullmatch(params[0], value))
@@ -113,10 +159,9 @@ class RegexRule(Rule):
113
159
  return f"The :attribute format is invalid."
114
160
 
115
161
  class NotRegexRule(Rule):
162
+ _count_parameter = 1
163
+
116
164
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
117
- if not params or not isinstance(value, str):
118
- return True
119
- print(not bool(re.search(params[0], value)))
120
165
  try:
121
166
  return not bool(re.search(params[0], value))
122
167
  except re.error: