-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidator.py
More file actions
717 lines (623 loc) · 24.7 KB
/
validator.py
File metadata and controls
717 lines (623 loc) · 24.7 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
from __future__ import annotations
from collections import defaultdict
from collections.abc import Sequence
from contextlib import suppress
from copy import deepcopy
from itertools import chain
from types import MappingProxyType
from typing import Any
from typing import Callable
from typing import get_args
from typing import TYPE_CHECKING
from dynaconf import validator_conditions
from dynaconf.utils import ensure_a_list
from dynaconf.utils.functional import empty
from dynaconf.utils.parse_conf import Lazy
if TYPE_CHECKING:
from dynaconf.base import LazySettings # noqa: F401
from dynaconf.base import Settings
DEFAULT_CAST = lambda value: value # noqa
EQUALITY_ATTRS = (
"names",
"must_exist",
"when",
"condition",
"operations",
"envs",
"cast",
)
class ValidationError(Exception):
"""Raised when a validation fails"""
def __init__(self, message: str, *args, **kwargs):
self.details = kwargs.pop("details", [])
super().__init__(message, *args, **kwargs)
self.message = message
class Validator:
"""Validators are conditions attached to settings variables names
or patterns::
Validator('MESSAGE', must_exist=True, eq='Hello World')
The above ensure MESSAGE is available in default env and
is equal to 'Hello World'
`names` are a one (or more) names or patterns::
Validator('NAME')
Validator('NAME', 'OTHER_NAME', 'EVEN_OTHER')
Validator(r'^NAME', r'OTHER./*')
The `operations` are::
eq: value == other
ne: value != other
gt: value > other
lt: value < other
gte: value >= other
lte: value <= other
is_type_of: isinstance(value, type)
is_in: value in sequence
is_not_in: value not in sequence
identity: value is other
cont: contain value in
len_eq: len(value) == other
len_ne: len(value) != other
len_min: len(value) > other
len_max: len(value) < other
startswith: value.startswith(term)
endswith: value.endswith(term)
`env` is which env to be checked, can be a list or
default is used.
`when` holds a validator and its return decides if validator runs or not::
Validator('NAME', must_exist=True, when=Validator('OTHER', eq=2))
# NAME is required only if OTHER eq to 2
# When the very first thing to be performed when passed.
# if no env is passed to `when` it is inherited
`must_exist` is alias to `required` requirement. (executed after when)::
settings.get(value, empty) returns non empty
condition is a callable to be executed and return boolean::
Validator('NAME', condition=lambda x: x == 1)
# it is executed before operations.
"""
default_messages = MappingProxyType(
{
"must_exist_true": "{name} is required in env {env}",
"must_exist_false": "{name} cannot exists in env {env}",
"condition": "{name} invalid for {function}({value}) in env {env}",
"operations": (
"{name} must {operation} {op_value} "
"but it is {value} in env {env}"
),
"combined": "combined validators failed {errors}",
}
)
def __init__(
self,
*names: str,
must_exist: bool | None = None,
required: bool | None = None, # alias for `must_exist`
condition: Callable[[Any], bool] | None = None,
when: Validator | None = None,
env: str | Sequence[str] | None = None,
messages: dict[str, str] | None = None,
cast: Callable[[Any], Any] | None = None,
default: Any | Callable[[Any, Validator], Any] | None = empty,
description: str | None = None,
apply_default_on_none: bool | None = False,
items_validators: list[Validator] | None = None,
**operations: Any,
) -> None:
# Copy immutable MappingProxyType as a mutable dict
self.messages = dict(self.default_messages)
if messages:
self.messages.update(messages)
if when is not None and not isinstance(when, Validator):
raise TypeError("when must be Validator instance")
if condition is not None and not callable(condition):
raise TypeError("condition must be callable")
# in the case that:
# * default is a Lazy object AND
# * there isnt any validate operation to perform (that would require knowing the lazy value)
# Then we shouldnt trigger the Lazy evaluation
self.should_call_lazy = not all(
(
default,
isinstance(default, Lazy),
not must_exist,
not required,
not cast,
not items_validators,
not operations,
)
)
self.names = names
self.must_exist = must_exist if must_exist is not None else required
self.condition = condition
self.when = when
self.cast = cast or DEFAULT_CAST
self.operations = operations
self.default = default
self.description = description
self.envs: Sequence[str] | None = None
self.apply_default_on_none = apply_default_on_none
self.items_validators = items_validators or []
if isinstance(env, str):
self.envs = [env]
elif isinstance(env, (list, tuple)):
self.envs = env
def __repr__(self):
_repr = f"{self.__class__.__name__}("
_elements = []
if len(self.names) > 1:
_elements.append(f"{list(self.names)}")
elif self.names:
_elements.append(f"'{self.names[0]}'")
if self.is_type_of:
type_name = str(self.is_type_of)
if not get_args(self.is_type_of):
with suppress(Exception):
type_name = (
str(self.is_type_of)
.replace("<class '", "")
.replace("'>", "")
.strip("<>'")
.split(".")[-1]
)
_elements.append(f"type={type_name}")
if self.must_exist is not None:
_elements.append(f"required={self.must_exist}")
operations = {
k: v for k, v in self.operations.items() if k != "is_type_of"
}
if operations:
_elements.append(f"operations={self.operations}")
if self.cast is not DEFAULT_CAST:
_elements.append(f"cast={self.cast}")
if self.condition:
_elements.append(f"condition={self.condition}")
if self.envs:
_elements.append(f"env={self.envs}")
if self.when:
_elements.append(f"when={self.when}")
if self.items_validators:
_elements.append(f"items_validators={self.items_validators}")
_repr += ", ".join(_elements)
_repr += ")"
return _repr
@property
def required(self) -> bool:
return bool(self.must_exist)
@required.setter
def required(self, value: bool):
self.must_exist = value
@property
def is_type_of(self):
# See #585
return self.operations.get("is_type_of")
@is_type_of.setter
def is_type_of(self, value):
self.operations["is_type_of"] = value
def __or__(self, other: Validator) -> CombinedValidator:
return OrValidator(self, other, description=self.description)
def __and__(self, other: Validator) -> CombinedValidator:
return AndValidator(self, other, description=self.description)
def __eq__(self, other: object) -> bool:
if self is other:
return True
if type(self).__name__ != type(other).__name__:
return False
identical_attrs = (
getattr(self, attr) == getattr(other, attr)
for attr in EQUALITY_ATTRS
)
if all(identical_attrs):
return True
return False
def validate(
self,
settings: Settings | dict,
only: str | Sequence | None = None,
exclude: str | Sequence | None = None,
only_current_env: bool = False,
variable_path: tuple | None = None,
) -> None:
"""Raise ValidationError if invalid"""
# If only or exclude are not set, this value always passes startswith
only = ensure_a_list(only or [""])
if only and not isinstance(only[0], str):
raise ValueError("'only' must be a string or list of strings.")
exclude = ensure_a_list(exclude)
if exclude and not isinstance(exclude[0], str):
raise ValueError("'exclude' must be a string or list of strings.")
current_env = getattr(settings, "current_env", "main")
envs = self.envs or [current_env]
# NOTE: Smells bad, must not mutate a validator
if self.when is not None:
try:
# inherit env if not defined
if self.when.envs is None:
self.when.envs = envs
self.when.validate(settings, only=only, exclude=exclude)
except ValidationError:
# if when is invalid, return canceling validation flow
return
if only_current_env:
if current_env.upper() in [s.upper() for s in envs]:
self._validate_names(
settings,
current_env,
only=only,
exclude=exclude,
variable_path=variable_path,
)
return
# If only using current_env, skip using_env decoration (reload)
if len(envs) == 1 and envs[0].upper() == current_env.upper():
self._validate_names(
settings,
current_env,
only=only,
exclude=exclude,
variable_path=variable_path,
)
return
for env in envs:
if from_env := getattr(settings, "from_env", None):
env_settings: Settings | dict = from_env(env)
else:
env_settings = settings
self._validate_names(
env_settings,
only=only,
exclude=exclude,
variable_path=variable_path,
)
# merge source metadata into original settings for history inspect
# use getattr to cheat mypy
if (d1 := getattr(settings, "_loaded_by_loaders", None)) and (
d2 := getattr(env_settings, "_loaded_by_loaders")
):
d1.update(d2)
def _validate_names(
self,
settings: Settings | dict,
env: str | None = None,
only: str | Sequence | None = None,
exclude: str | Sequence | None = None,
variable_path: tuple | None = None,
) -> None:
if env is None and (
current_env := getattr(settings, "current_env", None)
):
env = current_env
for name in self.names:
# Skip if only is set and name isn't in the only list
if only and not any(name.startswith(sub) for sub in only):
continue
# Skip if exclude is set and name is in the exclude list
if exclude and any(name.startswith(sub) for sub in exclude):
continue
if self.default is not empty:
if callable(self.default) and self.should_call_lazy:
default_value = self.default(settings, self)
else:
default_value = self.default
else:
default_value = empty
# THIS IS A FIX FOR #585 in contrast with #799
# toml considers signed strings "+-1" as integers
# however existing users are passing strings
# to default on validator (see #585)
# The solution we added on #667 introduced a new problem
# This fix here makes it to work for both cases.
# This guard also fixes #1064 assuming that any validator
# having is_type_of=str wants to bypass toml inference.
if isinstance(default_value, str) and self.is_type_of is str:
# avoid TOML from parsing "+-1" started strings as integer
default_value = f"'{default_value}'"
# NOTE: must stop mutating settings here
if getattr(settings, "_store", None):
try:
# settings is a Dynaconf instance
value = getattr(settings, "setdefault")( # cheat mypy
name,
default_value,
apply_default_on_none=self.apply_default_on_none,
env=env,
)
except AttributeError: # dotted get/set on a non-dict type
raise ValidationError(f"Mismatched type for {name}")
else:
value = settings.get(name, default_value)
# is name required but not exists?
if self.must_exist is True and value is empty:
_message = self.messages["must_exist_true"].format(
name=name, env=env
)
raise ValidationError(_message, details=[(self, _message)])
if self.must_exist is False and value is not empty:
_message = self.messages["must_exist_false"].format(
name=name, env=env
)
raise ValidationError(_message, details=[(self, _message)])
if self.must_exist in (False, None) and value is empty:
continue
# value or default value already set
# by settings.setdefault above
# however we need to cast it
# so we call .set again
# NOTE: we must stop mutating settings here
if self.should_call_lazy:
value = self.cast(settings.get(name))
if _set := getattr(settings, "set", None):
# Settings is Dynaconf
_set(
name, value, validate=False, loader_identifier="validator"
)
else:
# settings is a dict
settings[name] = value
# reorder passed operations so type check is made first
sorted_operations = sorted(
self.operations.items(),
key=lambda item: item[0] != "is_type_of",
)
for op_name, op_value in sorted_operations:
op_function = getattr(validator_conditions, op_name)
op_succeeded = False
# 'is_type_of' special error handling - related to #879
if op_name == "is_type_of":
# auto transform quoted types
if isinstance(op_value, str):
op_value = __builtins__.get( # type: ignore
op_value, op_value
)
# invalid type (not in __builtins__) may raise TypeError
try:
op_succeeded = op_function(value, op_value)
except TypeError as e:
raise ValidationError(
f"Invalid type '{op_value}' for condition "
f"'is_type_of'. Should provide a valid type. {e}"
)
else:
try:
op_succeeded = op_function(value, op_value)
except TypeError as e:
raise ValidationError(
f"Invalid Operation '{op_name}' "
f"for type {type(value)!r} "
f"on '{name}': {e}"
)
if not op_succeeded:
_message = self.messages["operations"].format(
name=name,
operation=op_function.__name__,
op_value=op_value,
value=value,
env=env,
)
raise ValidationError(_message, details=[(self, _message)])
# is there a callable condition?
if self.condition is not None:
if not self.condition(value):
_message = self.messages["condition"].format(
name=name,
function=self.condition.__name__,
value=value,
env=env,
)
raise ValidationError(_message, details=[(self, _message)])
# Type is list or dict and has internal validators
self._validate_internal_items(value, name, variable_path)
def _validate_internal_items(
self,
value: Any,
name: str,
variable_path: tuple | None = None,
):
"""Validate internal items of a data structure."""
variable_path = variable_path or tuple()
variable_path += (name,)
if isinstance(value, list):
for i, item in enumerate(value):
_item_path = tuple(variable_path)
if isinstance(item, dict):
_item_path += (f"{i}", "{name}")
_validator_location = ".".join(_item_path)
data = item
else:
_item_path += (f"{i}",)
_validator_location = ".".join(_item_path)
data = {"<item>": item}
for validator in self.items_validators:
# avoid mutating a reusable validator
if not validator.names:
_validator = deepcopy(validator)
_validator.names = ("<item>",)
else:
_validator = validator
for k, v in _validator.default_messages.items():
_validator.messages[k] = v.replace(
"{name}", _validator_location
)
_validator.validate(data, variable_path=_item_path)
elif isinstance(value, dict):
_validator_location = ".".join(variable_path)
for validator in self.items_validators:
for k, v in validator.default_messages.items():
validator.messages[k] = v.replace(
"{name}", _validator_location + ".{name}"
)
validator.validate(
value, only=validator.names, variable_path=variable_path
)
class CombinedValidator(Validator):
def __init__(
self,
validator_a: Validator,
validator_b: Validator,
*args: Any,
**kwargs: Any,
) -> None:
"""Takes 2 validators and combines the validation"""
self.validators = (validator_a, validator_b)
super().__init__(*args, **kwargs)
for attr in EQUALITY_ATTRS:
if not getattr(self, attr, None):
value = tuple(
getattr(validator, attr) for validator in self.validators
)
setattr(self, attr, value)
def validate(
self,
settings: Any,
only: str | Sequence | None = None,
exclude: str | Sequence | None = None,
only_current_env: bool = False,
variable_path: tuple | None = None,
) -> None: # pragma: no cover
raise NotImplementedError(
"subclasses OrValidator or AndValidator implements this method"
)
def __repr__(self):
result = f"{self.__class__.__name__}("
result += ", ".join(repr(v) for v in self.validators)
result += ")"
return result
class OrValidator(CombinedValidator):
"""Evaluates on Validator() | Validator()"""
def validate(
self,
settings: Any,
only: str | Sequence | None = None,
exclude: str | Sequence | None = None,
only_current_env: bool = False,
variable_path: tuple | None = None,
) -> None:
"""Ensure at least one of the validators are valid"""
errors = []
for validator in self.validators:
try:
validator.validate(
settings,
only=only,
exclude=exclude,
only_current_env=only_current_env,
variable_path=variable_path,
)
except ValidationError as e:
errors.append(e)
continue
else:
return
_message = self.messages["combined"].format(
errors=" or ".join(
str(e).replace("combined validators failed ", "")
for e in errors
)
)
raise ValidationError(_message, details=[(self, _message)])
class AndValidator(CombinedValidator):
"""Evaluates on Validator() & Validator()"""
def validate(
self,
settings: Any,
only: str | Sequence | None = None,
exclude: str | Sequence | None = None,
only_current_env: bool = False,
variable_path: tuple | None = None,
) -> None:
"""Ensure both the validators are valid"""
errors = []
for validator in self.validators:
try:
validator.validate(
settings,
only=only,
exclude=exclude,
only_current_env=only_current_env,
variable_path=variable_path,
)
except ValidationError as e:
errors.append(e)
continue
if errors:
_message = self.messages["combined"].format(
errors=" and ".join(
str(e).replace("combined validators failed ", "")
for e in errors
)
)
raise ValidationError(_message, details=[(self, _message)])
class ValidatorList(list):
def __init__(
self,
settings: Settings,
validators: Sequence[Validator] | None = None,
*args: Validator,
**kwargs: Any,
) -> None:
if isinstance(validators, (list, tuple)):
args = list(args) + list(validators) # type: ignore
self._only = kwargs.pop("validate_only", None)
self._exclude = kwargs.pop("validate_exclude", None)
super().__init__(args, **kwargs) # type: ignore
self.settings = settings
def register(self, *args: Validator, **kwargs: Validator):
validators: list[Validator] = list(
chain.from_iterable(kwargs.values()) # type: ignore
)
validators.extend(args)
for validator in validators:
if validator and validator not in self:
self.append(validator)
def descriptions(self, flat: bool = False) -> dict[str, str | list[str]]:
if flat:
descriptions: dict[str, str | list[str]] = {}
else:
descriptions = defaultdict(list)
for validator in self:
for name in validator.names:
if isinstance(name, tuple) and len(name) > 0:
name = name[0]
if flat:
descriptions.setdefault(name, validator.description)
else:
descriptions[name].append( # type: ignore
validator.description
)
return descriptions
def validate(
self,
only: str | Sequence | None = None,
exclude: str | Sequence | None = None,
only_current_env: bool = False,
) -> None:
for validator in self:
validator.validate(
self.settings,
only=only,
exclude=exclude,
only_current_env=only_current_env,
)
def validate_all(
self,
only: str | Sequence | None = None,
exclude: str | Sequence | None = None,
only_current_env: bool = False,
raise_error=True,
) -> list[ValidationError]:
errors = []
details = []
for validator in self:
try:
validator.validate(
self.settings,
only=only,
exclude=exclude,
only_current_env=only_current_env,
)
except ValidationError as e:
errors.append(e)
details.append((validator, str(e)))
continue
if errors and raise_error:
raise ValidationError(
"; ".join(str(e) for e in errors), details=details
)
return errors