File size: 6,111 Bytes
1a48d6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations
from typing import Any
from jsonschema import Draft202012Validator, SchemaError

COMMON = {"type", "instructions", "nullable"}
NUMERIC = {"integer", "decimals", "unit"}
GRAMMAR_NUMBER = {"minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum", "enum"}
OPTIONS = {
    "text": {"knowledge", "minLength", "maxLength", "pattern", "enum"},
    "number": NUMERIC | GRAMMAR_NUMBER | {"knowledge", "source_only"},
    "compute": NUMERIC | {"null_threshold"},
    "date": {"null_threshold", "source_only"},
    "time": {"null_threshold", "source_only"},
    "datetime": {"null_threshold", "source_only"},
    "duration": NUMERIC | {"null_threshold"},
    "boolean": set(),
    "enum": {"enum"},
    "object": {"properties"},
    "list": {"items", "max_items", "knowledge", "source_only"} | NUMERIC | {"enum"},
    "estimate": {"min", "max", "scale", "step", "unit"},
    "forecast": {"min", "max", "scale", "step", "unit", "horizon", "interval"},
}


def item_spec(spec: dict[str, Any]) -> dict[str, Any]:
    item = spec["items"]
    return dict(item) if isinstance(item, dict) else {"type": item}


def validate_public_fields(fields: dict[str, dict[str, Any]]) -> None:
    _validate_public_fields(fields, set())


def _validate_public_fields(
    fields: dict[str, dict[str, Any]], checked_scalar_schemas: set[tuple[str, ...]]
) -> None:
    for name, spec in fields.items():
        kind = spec.get("type") if isinstance(spec, dict) else None
        if not isinstance(kind, str) or kind not in OPTIONS:
            raise ValueError(f"field {name}: unknown field type")
        unknown = spec.keys() - COMMON - OPTIONS[kind]
        if unknown:
            raise ValueError(f"field {name}: unknown options {sorted(unknown)}")
        if "instructions" in spec and (not isinstance(spec["instructions"], str)):
            raise ValueError(f"field {name}: instructions must be a string")
        if "nullable" in spec and (not isinstance(spec["nullable"], bool)):
            raise ValueError(f"field {name}: nullable must be true or false")
        if "source_only" in spec:
            if not isinstance(spec["source_only"], bool):
                raise ValueError(f"field {name}: source_only must be true or false")
            if kind == "list" and spec.get("items") not in ("date", "number"):
                raise ValueError(
                    f"field {name}: source_only lists require date or number items"
                )
            if spec["source_only"] and "null_threshold" in spec:
                raise ValueError(
                    f"field {name}: source_only does not support typed null_threshold"
                )
            if spec["source_only"] and spec.get("knowledge"):
                raise ValueError(
                    f"field {name}: source_only cannot permit knowledge answers"
                )
        if "enum" in spec and (not isinstance(spec["enum"], list) or not spec["enum"]):
            raise ValueError(f"field {name}: enum must be a nonempty array")
        if kind == "enum" and "enum" not in spec:
            raise ValueError(f"field {name}: enum values are required")
        if kind == "object":
            properties = spec.get("properties")
            if not isinstance(properties, dict) or not properties:
                raise ValueError(f"field {name}: object properties are required")
            if any(
                (
                    not isinstance(child, dict)
                    or child.get("type") not in ("text", "number", "boolean", "enum")
                    for child in properties.values()
                )
            ):
                raise ValueError(
                    f"field {name}: row properties must be text, number, boolean or enum"
                )
            if any((child.get("source_only") for child in properties.values())):
                raise ValueError(
                    f"field {name}: source_only is supported on scalar numbers and number lists, not row properties"
                )
            _validate_public_fields(properties, checked_scalar_schemas)
            from . import schema as zs

            zs.validate_fields(properties)
        if kind == "list":
            if "items" not in spec:
                raise ValueError(f"list {name}: items are required")
            child = item_spec(spec)
            if isinstance(spec["items"], dict) and child.get("type") != "object":
                raise ValueError(f"list {name}: structured items must be objects")
            if child.get("type") == "object":
                _validate_public_fields({name + "[]": child}, checked_scalar_schemas)
                if spec.keys() & (NUMERIC | {"knowledge", "enum"}):
                    raise ValueError(
                        f"list {name}: put numeric or knowledge options on row properties"
                    )
            maximum = spec.get("max_items")
            if "max_items" in spec and (
                isinstance(maximum, bool) or not isinstance(maximum, int) or maximum < 0
            ):
                raise ValueError(
                    f"list {name}: max_items must be a nonnegative integer"
                )
        if kind in ("text", "number", "boolean", "enum", "object"):
            from . import ar

            try:
                schema = ar.schema(spec)
                scalar_key = None
                if schema.keys() == {"type"}:
                    schema_type = schema["type"]
                    scalar_key = (
                        tuple(schema_type)
                        if isinstance(schema_type, list)
                        else (schema_type,)
                    )
                if scalar_key is None or scalar_key not in checked_scalar_schemas:
                    Draft202012Validator.check_schema(schema)
                    if scalar_key is not None:
                        checked_scalar_schemas.add(scalar_key)
            except SchemaError as error:
                raise ValueError(
                    f"field {name}: invalid schema constraint: {error.message}"
                ) from error