-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathschema_validator.py
More file actions
47 lines (38 loc) · 1.25 KB
/
schema_validator.py
File metadata and controls
47 lines (38 loc) · 1.25 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
import requests
import jsonschema
_schema_cache = {}
def _load_schema(schema_type: str):
"""Fetch JSON Schema for a given schema.org type."""
if schema_type in _schema_cache:
return _schema_cache[schema_type]
url = f"https://schema.org/{schema_type}.schema.json"
try:
response = requests.get(url, timeout=10)
if response.status_code == 200:
schema = response.json()
_schema_cache[schema_type] = schema
return schema
except Exception:
pass
return None
def validate(data: dict):
"""Validate structured data dict using schema.org definitions.
Parameters
----------
data: dict
Structured data item in JSON-LD format.
Returns
-------
list[str]
List of validation error messages. Empty if valid or schema missing.
"""
schema_type = data.get("@type")
if isinstance(schema_type, list):
schema_type = schema_type[0]
if not schema_type:
return ["Missing @type"]
schema = _load_schema(schema_type)
if not schema:
return [f"Schema for {schema_type} not found at schema.org"]
validator = jsonschema.Draft2020Validator(schema)
return [error.message for error in validator.iter_errors(data)]