-
Notifications
You must be signed in to change notification settings - Fork 4
/
validation.py
49 lines (36 loc) 路 1.37 KB
/
validation.py
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
from schemadiff import Change
from typing import List, Dict, Any
from dataclasses import dataclass
from schemadiff.validation_rules import ValidationRule
@dataclass
class ValidationResult:
ok: bool
errors: List['ValidationError']
@dataclass
class ValidationError:
rule: str
reason: str
change: Change
def validate_changes(diff: List[Change], rules: List[str], allowed_changes: Dict[str, Any] = None) -> ValidationResult:
"""Given a list of changes between schemas and a list of rules,
it runs all rules against the changes, to detect invalid changes.
It also admits an allowlist of accepted invalid changes to document exceptions to the rules
Returns:
bool: True if there is at least one restricted change,
False otherwise.
"""
allowed_changes = allowed_changes or {}
is_valid = True
errors = []
rules = ValidationRule.get_subclasses_by_names(rules)
for change in diff:
for rule in rules:
if not rule(change).is_valid():
if change.checksum() in allowed_changes:
continue
change.restricted = rule(change).message
is_valid = False
errors.append(ValidationError(rule.name, change.restricted, change))
return ValidationResult(is_valid, errors)
def rules_list():
return ValidationRule.get_rules_list()