-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidation.py
61 lines (46 loc) · 1.62 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
50
51
52
53
54
55
56
57
58
59
60
61
import uuid
import ipaddress
from typing import Dict, Any
import jsonschema
from jsonschema import Draft202012Validator, FormatChecker, ValidationError
def is_json_schema_valid(schema: dict):
"""
Check if a JSON schema is valid.
:param schema: A JSON schema.
:return: True if the schema is valid, False otherwise.
"""
try:
# Check if the schema is valid
jsonschema.Draft202012Validator.check_schema(schema)
return True
except jsonschema.SchemaError as e:
# logger.error(f"SchemaError: The JSON schema is invalid. {e.message}")
return False
# Initialize the FormatChecker
format_checker = FormatChecker()
# Add custom format checkers
@format_checker.checks("ipv4")
def ipv4_check(value):
ipaddress.IPv4Address(value)
@format_checker.checks("ipv6")
def ipv6_check(value):
ipaddress.IPv6Address(value)
@format_checker.checks("uuid")
def uuid_check(value):
uuid.UUID(value)
def validate_enhanaced(instance: Dict[str, Any], schema: Dict[str, Any]) -> bool:
"""
Validate a JSON instance against a schema with enhanced format checking.
:param schema: The JSON schema to validate against.
:param instance: The JSON instance to validate.
:raises ValidationError: If the validation fails.
"""
# first check if the schema is valid
if not is_json_schema_valid(schema):
raise ValidationError("The JSON schema is invalid.")
validator = Draft202012Validator(schema, format_checker=format_checker)
try :
validator.validate(instance)
except ValidationError as e:
raise ValidationError(e.message)
return True