safeshield 1.5.1__py3-none-any.whl → 1.5.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.

Potentially problematic release.


This version of safeshield might be problematic. Click here for more details.

@@ -4,247 +4,393 @@ from .basic import *
4
4
  from .boolean import AcceptedRule, DeclinedRule
5
5
 
6
6
  class RequiredIfRule(RequiredRule):
7
+ _count_parameter = 2
8
+ _accept_closure = True
9
+
7
10
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
8
- if len(params) == 1 and callable(params[0]):
11
+ if callable(params[0]):
9
12
  condition_met = params[0](self.validator.data)
10
- elif len(params) == 1 and isinstance(params[0], bool):
13
+ elif isinstance(params[0], bool):
11
14
  condition_met = params[0]
12
15
  else:
13
- conditions = list(zip(params[::2], params[1::2]))
16
+ expected_value = self.get_field_value(params[0], False)
17
+ conditions = params[1:]
14
18
 
15
- condition_met = any(
16
- str(self.get_field_value(field, '')) == str(expected_value)
17
- for field, expected_value in conditions
18
- if field and expected_value is not None
19
- )
19
+ condition_met = expected_value in conditions
20
20
 
21
21
  if condition_met:
22
22
  return super().validate(field, value, params)
23
23
 
24
24
  return True
25
25
 
26
- class RequiredUnlessRule(RequiredRule):
27
- def validate(self, field: str, value: Any, params: List[str]) -> bool:
28
- if len(params) < 2:
29
- return False
30
-
31
- other_field, other_value = params[0], params[1]
32
- actual_value = self.get_field_value(other_field, '')
26
+ def replacements(self, field, value):
27
+ replacements = {
28
+ ':attribute': self._get_display_name(field),
29
+ ':input': value,
30
+ ':other': self._get_display_name(self._params[0]),
31
+ ':values': ', '.join(self._params[1:]),
32
+ }
33
33
 
34
- if actual_value == other_value:
35
- return True
36
-
37
- return super().validate(field, value, params)
34
+ return replacements
35
+
36
+ class RequiredUnlessRule(RequiredRule):
37
+ _count_parameter = 2
38
+ _accept_closure = True
38
39
 
39
- class RequiredAllIfRule(RequiredRule):
40
40
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
41
- if len(params) < 2:
42
- return False
41
+ if callable(params[0]):
42
+ condition_met = params[0](self.validator.data)
43
+ elif isinstance(params[0], bool):
44
+ condition_met = params[0]
45
+ else:
46
+ expected_value = self.get_field_value(params[0], False)
47
+ conditions = params[1:]
43
48
 
44
- conditions = [(f.strip(), v.strip()) for f, v in zip(params[::2], params[1::2])]
49
+ condition_met = expected_value in conditions
50
+
51
+ if not condition_met:
52
+ return super().validate(field, value, params)
45
53
 
46
- all_conditions_met = all(
47
- self.get_field_value(f) == v
48
- for f, v in conditions
49
- )
54
+ return True
55
+
56
+ def replacements(self, field, value):
57
+ replacements = {
58
+ ':attribute': self._get_display_name(field),
59
+ ':input': value,
60
+ ':other': self._get_display_name(self._params[0]),
61
+ ':values': ', '.join(self._params[1:]),
62
+ }
50
63
 
51
- if not all_conditions_met:
52
- return True
53
-
54
- return super().validate(field, value, params)
64
+ return replacements
55
65
 
56
66
  class RequiredWithRule(RequiredRule):
67
+ _count_parameter = 1
68
+
57
69
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
58
- if not params:
59
- return False
60
-
61
70
  if any(f in self.validator.data for f in params):
62
71
  return super().validate(field, value, params)
63
72
  return True
73
+
74
+ def replacements(self, field, value):
75
+ replacements = {
76
+ ':attribute': self._get_display_name(field),
77
+ ':input': value,
78
+ ':others': ', '.join(self._get_display_name(self._params)),
79
+ }
80
+
81
+ return replacements
64
82
 
65
83
  class RequiredWithAllRule(RequiredRule):
84
+ _count_parameter = 1
85
+
66
86
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
67
- if not params:
68
- return False
69
-
70
87
  if all(f in self.validator.data for f in params):
71
- return not self.is_empty(value)
88
+ return super().validate(field, value, params)
72
89
  return True
73
90
 
91
+ def replacements(self, field, value):
92
+ replacements = {
93
+ ':attribute': self._get_display_name(field),
94
+ ':input': value,
95
+ ':others': ', '.join(self._get_display_name(self._params)),
96
+ }
97
+
98
+ return replacements
99
+
74
100
  class RequiredWithoutRule(RequiredRule):
101
+ _count_parameter = 1
102
+
75
103
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
76
- if not params:
77
- return False
78
-
79
104
  if any(f not in self.validator.data for f in params):
80
105
  return super().validate(field, value, params)
81
106
  return True
82
107
 
108
+ def replacements(self, field, value):
109
+ replacements = {
110
+ ':attribute': self._get_display_name(field),
111
+ ':input': value,
112
+ ':others': ', '.join(self._get_display_name(self._params)),
113
+ }
114
+
115
+ return replacements
116
+
83
117
  class RequiredWithoutAllRule(RequiredRule):
118
+ _count_parameter = 1
119
+
84
120
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
85
- if not params:
86
- return False
87
-
88
121
  if all(f not in self.validator.data for f in params):
89
122
  return super().validate(field, value, params)
90
123
  return True
91
124
 
125
+ def replacements(self, field, value):
126
+ replacements = {
127
+ ':attribute': self._get_display_name(field),
128
+ ':input': value,
129
+ ':others': ', '.join(self._get_display_name(self._params)),
130
+ }
131
+
132
+ return replacements
133
+
92
134
  class RequiredIfAcceptedRule(RequiredRule, AcceptedRule):
135
+ _count_parameter = 1
136
+
93
137
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
94
- if not params:
95
- return False
96
-
97
138
  if AcceptedRule.validate(self, field, params[0], params):
98
139
  return super().validate(field, value, params)
99
140
 
100
141
  return True
101
142
 
102
143
  class RequiredIfDeclinedRule(RequiredRule, DeclinedRule):
144
+ _count_parameter = 1
145
+
103
146
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
104
- if not params:
105
- return False
106
-
107
147
  if DeclinedRule.validate(self, field, params[0], params):
108
148
  return super().validate(field, value, params)
109
149
 
110
150
  return True
111
151
 
112
152
  class RequiredArrayKeysRule(Rule):
153
+ _count_parameter = 1
154
+
113
155
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
114
- if not isinstance(value, dict) or not params:
156
+ if not isinstance(value, dict):
115
157
  return False
116
158
  return all(key in value for key in params)
117
159
 
118
160
  def message(self, field: str, params: List[str]) -> str:
119
- return f"The :attribute must contain all required keys: {', '.join(params)}"
161
+ return f"The :attribute must contain all required keys: :values"
162
+
163
+ def replacements(self, field, value):
164
+ replacements = {
165
+ ':attribute': self._get_display_name(field),
166
+ ':input': value,
167
+ 'values': ', '.join(self._params),
168
+ }
169
+
170
+ return replacements
120
171
 
121
172
  class ProhibitedIfRule(ProhibitedRule):
173
+ _count_parameter = 2
174
+ _accept_closure = True
175
+
122
176
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
123
- if len(params) == 1 and callable(params[0]):
177
+ if callable(params[0]):
124
178
  condition_met = params[0](self.validator.data)
125
- elif len(params) == 1 and isinstance(params[0], bool):
179
+ elif isinstance(params[0], bool):
126
180
  condition_met = params[0]
127
181
  else:
128
- other_field, other_value = params[0], params[1]
129
- actual_value = self.get_field_value(other_field, '')
130
-
131
- condition_met = actual_value == other_value
182
+ expected_value, conditions = self.get_field_value(params[0], None), params[1:]
183
+ condition_met = expected_value in conditions
132
184
 
133
185
  if condition_met:
134
186
  return super().validate(field, value, params)
135
- return True
187
+ return True
188
+
189
+ def replacements(self, field, value):
190
+ replacements = {
191
+ ':attribute': self._get_display_name(field),
192
+ ':input': value,
193
+ ':other': self._get_display_name(self._params[0]),
194
+ ':values': ', '.join(self._params[1:]),
195
+ }
196
+
197
+ return replacements
136
198
 
137
199
  class ProhibitedUnlessRule(ProhibitedRule):
200
+ _count_parameter = 2
201
+ _accept_closure = True
202
+
138
203
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
139
- if len(params) < 2:
140
- return False
141
-
142
- other_field, other_value = params[0], params[1]
143
- actual_value = self.get_field_value(other_field, '')
204
+ if callable(params[0]):
205
+ condition_met = params[0](self.validator.data)
206
+ elif isinstance(params[0], bool):
207
+ condition_met = params[0]
208
+ else:
209
+ expected_value, conditions = self.get_field_value(params[0], None), params[1:]
210
+ condition_met = expected_value in conditions
144
211
 
145
- if actual_value == other_value:
146
- return True
147
-
148
- return super().validate(field, value, params)
212
+ if not condition_met:
213
+ return super().validate(field, value, params)
214
+ return True
215
+
216
+ def replacements(self, field, value):
217
+ replacements = {
218
+ ':attribute': self._get_display_name(field),
219
+ ':input': value,
220
+ ':other': self._get_display_name(self._params[0]),
221
+ ':values': ', '.join(self._params[1:]),
222
+ }
223
+
224
+ return replacements
149
225
 
150
226
  class ProhibitedIfAcceptedRule(ProhibitedRule, AcceptedRule):
227
+ _count_parameter = 1
228
+
151
229
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
152
- if not params:
153
- return False
154
-
155
230
  if AcceptedRule.validate(self, field, params[0], params):
156
231
  return super().validate(field, value, params)
157
232
  return True
158
233
 
159
234
  class ProhibitedIfDeclinedRule(ProhibitedRule, DeclinedRule):
235
+ _count_parameter = 1
236
+
160
237
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
161
- if not params:
162
- return False
163
-
164
238
  if DeclinedRule.validate(self, field, params[0], params):
165
239
  return super().validate(field, value, params)
166
240
 
167
241
  return True
168
242
 
169
243
  class PresentIfRule(PresentRule):
244
+ _count_parameter = 2
245
+ _accept_closure = True
246
+
170
247
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
171
- condition_met = False
172
-
173
- if len(params) == 1 and callable(params[0]):
248
+ if callable(params[0]):
174
249
  condition_met = params[0](self.validator.data)
175
- elif len(params) == 1 and isinstance(params[0], bool):
250
+ elif isinstance(params[0], bool):
176
251
  condition_met = params[0]
177
252
  else:
178
- conditions = list(zip(params[::2], params[1::2]))
179
- for other_field, expected_value in conditions:
180
- if self.get_field_value(other_field, None) == expected_value:
181
- condition_met = True
182
- break
253
+ expected_value, conditions = self.get_field_value(params[0], None), params[1:]
254
+
255
+ condition_met = expected_value in conditions
183
256
 
184
257
  if condition_met:
185
258
  return super().validate(field, value, params)
186
259
 
187
260
  return True
188
261
 
262
+ def replacements(self, field, value):
263
+ replacements = {
264
+ ':attribute': self._get_display_name(field),
265
+ ':input': value,
266
+ ':other': self._get_display_name(self._params[0]),
267
+ ':values': ', '.join(self._params[1:]),
268
+ }
269
+
270
+ return replacements
271
+
189
272
  class PresentUnlessRule(PresentRule):
273
+ _count_parameter = 2
274
+ _accept_closure = True
275
+
190
276
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
191
- if len(params) < 2:
192
- return False
193
- other_field, other_value = params[0], params[1]
194
-
195
- if self.get_field_value(other_field, None) != other_value:
277
+ if callable(params[0]):
278
+ condition_met = params[0](self.validator.data)
279
+ elif isinstance(params[0], bool):
280
+ condition_met = params[0]
281
+ else:
282
+ expected_value, conditions = self.get_field_value(params[0], None), params[1:]
283
+
284
+ condition_met = expected_value in conditions
285
+
286
+ if not condition_met:
196
287
  return super().validate(field, value, params)
197
288
 
198
289
  return True
199
290
 
291
+ def replacements(self, field, value):
292
+ replacements = {
293
+ ':attribute': self._get_display_name(field),
294
+ ':input': value,
295
+ ':other': self._get_display_name(self._params[0]),
296
+ ':values': ', '.join(self._params[1:]),
297
+ }
298
+
299
+ return replacements
300
+
200
301
  class PresentWithRule(PresentRule):
302
+ _count_parameter = 1
303
+
201
304
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
202
- if not params:
203
- return False
204
305
  if any(self.get_field_value(param, None) is not None for param in params):
205
306
  return super().validate(field, value, params)
206
307
 
207
308
  return True
208
309
 
310
+ def replacements(self, field, value):
311
+ replacements = {
312
+ ':attribute': self._get_display_name(field),
313
+ ':input': value,
314
+ ':others': ', '.join(self._get_display_name(self._params)),
315
+ }
316
+
317
+ return replacements
318
+
209
319
  class PresentWithAllRule(PresentRule):
320
+ _count_parameter = 1
321
+
210
322
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
211
- if not params:
212
- return False
213
-
214
323
  if all(self.get_field_value(param, None) is not None for param in params):
215
324
  return super().validate(field, value, params)
325
+
326
+ def replacements(self, field, value):
327
+ replacements = {
328
+ ':attribute': self._get_display_name(field),
329
+ ':input': value,
330
+ ':others': ', '.join(self._get_display_name(self._params)),
331
+ }
332
+
333
+ return replacements
216
334
 
217
335
  class MissingIfRule(MissingRule):
336
+ _count_parameter = 2
337
+ _accept_closure = True
338
+
218
339
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
219
- condition_met = False
220
-
221
- if len(params) == 1 and callable(params[0]):
340
+ if callable(params[0]):
222
341
  condition_met = params[0](self.validator.data)
223
- elif len(params) == 1 and isinstance(params[0], bool):
342
+ elif isinstance(params[0], bool):
224
343
  condition_met = params[0]
225
344
  else:
226
- conditions = list(zip(params[::2], params[1::2]))
227
- for other_field, expected_value in conditions:
228
- if self.get_field_value(other_field, None) == expected_value:
229
- condition_met = True
230
- break
345
+ expected_value, conditions = self.get_field_value(params[0], None), params[1:]
346
+ condition_met = expected_value in conditions
231
347
 
232
348
  if condition_met:
233
349
  return super().validate(field, value, params)
234
350
 
235
351
  return True
236
352
 
353
+ def replacements(self, field, value):
354
+ replacements = {
355
+ ':attribute': self._get_display_name(field),
356
+ ':input': value,
357
+ ':other': self._get_display_name(self._params[0]),
358
+ ':values': ', '.join(self._params[1:]),
359
+ }
360
+
361
+ return replacements
362
+
237
363
  class MissingUnlessRule(MissingRule):
364
+ _count_parameter = 2
365
+ _accept_closure = True
366
+
238
367
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
239
- if len(params) < 2:
240
- return False
241
- other_field, other_value = params[0], params[1]
242
- if self.get_field_value(other_field, None) != other_value:
368
+ if callable(params[0]):
369
+ condition_met = params[0](self.validator.data)
370
+ elif isinstance(params[0], bool):
371
+ condition_met = params[0]
372
+ else:
373
+ expected_value, conditions = self.get_field_value(params[0], None), params[1:]
374
+ condition_met = expected_value in conditions
375
+
376
+ if not condition_met:
243
377
  return super().validate(field, value, params)
244
-
378
+
245
379
  return True
380
+
381
+ def replacements(self, field, value):
382
+ replacements = {
383
+ ':attribute': self._get_display_name(field),
384
+ ':input': value,
385
+ ':other': self._get_display_name(self._params[0]),
386
+ ':values': ', '.join(self._params[1:]),
387
+ }
388
+
389
+ return replacements
246
390
 
247
391
  class MissingWithRule(MissingRule):
392
+ _count_parameter = 1
393
+
248
394
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
249
395
  if not params:
250
396
  return False
@@ -254,7 +400,18 @@ class MissingWithRule(MissingRule):
254
400
 
255
401
  return True
256
402
 
403
+ def replacements(self, field, value):
404
+ replacements = {
405
+ ':attribute': self._get_display_name(field),
406
+ ':input': value,
407
+ ':others': ', '.join(self._get_display_name(self._params)),
408
+ }
409
+
410
+ return replacements
411
+
257
412
  class MissingWithAllRule(MissingRule):
413
+ _count_parameter = 1
414
+
258
415
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
259
416
  if not params:
260
417
  return False
@@ -264,48 +421,52 @@ class MissingWithAllRule(MissingRule):
264
421
 
265
422
  return True
266
423
 
424
+ def replacements(self, field, value):
425
+ replacements = {
426
+ ':attribute': self._get_display_name(field),
427
+ ':input': value,
428
+ ':others': ', '.join(self._get_display_name(self._params)),
429
+ }
430
+
431
+ return replacements
432
+
267
433
  class ExcludeIfRule(ExcludeRule):
434
+ _count_parameter = 2
435
+ _accept_closure = True
436
+
268
437
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
269
- if len(params) == 1 and callable(params[0]):
438
+ if callable(params[0]):
270
439
  condition_met = params[0](self.validator.data)
271
- elif len(params) == 1 and isinstance(params[0], bool):
440
+ elif isinstance(params[0], bool):
272
441
  condition_met = params[0]
273
442
  else:
274
- conditions = list(zip(params[::2], params[1::2]))
275
- condition_met = all(
276
- str(self.get_field_value(f)) == str(v)
277
- for f, v in conditions
278
- )
443
+ expected_value, conditions = self.get_field_value(params[0], None), params[1:]
444
+ condition_met = expected_value in conditions
279
445
 
280
446
  if condition_met:
281
447
  return super().validate(field, value, params)
282
448
  return True
283
449
 
284
450
  class ExcludeUnlessRule(ExcludeRule):
451
+ _count_parameter = 2
452
+ _accept_closure = True
453
+
285
454
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
286
- all_conditions_met = False
287
-
288
- if len(params) == 1 and callable(params[0]):
289
- all_conditions_met = params[0](self.validator.data)
290
- elif len(params) == 1 and isinstance(params[0], bool):
291
- all_conditions_met = params[0]
455
+ if callable(params[0]):
456
+ condition_met = params[0](self.validator.data)
457
+ elif isinstance(params[0], bool):
458
+ condition_met = params[0]
292
459
  else:
293
- conditions = [(f.strip(), v.strip()) for f, v in zip(params[::2], params[1::2])]
294
-
295
- all_conditions_met = all(
296
- self.get_field_value(f) == v
297
- for f, v in conditions
298
- )
460
+ expected_value, conditions = self.get_field_value(params[0], None), params[1:]
461
+ condition_met = expected_value in conditions
299
462
 
300
- if not all_conditions_met:
463
+ if not condition_met:
301
464
  return super().validate(field, value, params)
302
-
303
465
  return True
304
-
305
- def message(self, field: str, params: List[str]) -> str:
306
- return f"The :attribute field is excluded unless {params[0]} is {params[1]}."
307
466
 
308
467
  class ExcludeWithRule(ExcludeRule):
468
+ _count_parameter = 1
469
+
309
470
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
310
471
  if any(not self.is_empty(self.get_field_value(param, None)) for param in params):
311
472
  return super().validate(field, value, params)
@@ -313,6 +474,8 @@ class ExcludeWithRule(ExcludeRule):
313
474
  return True
314
475
 
315
476
  class ExcludeWithoutRule(ExcludeRule):
477
+ _count_parameter = 1
478
+
316
479
  def validate(self, field: str, value: Any, params: List[str]) -> bool:
317
480
  if any(self.is_empty(self.get_field_value(param, None)) for param in params):
318
481
  return super().validate(field, value, params)