valgebra-jsonschema 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- valgebra_jsonschema/__init__.py +64 -0
- valgebra_jsonschema/_valgebra.py +177 -0
- valgebra_jsonschema/formats.py +130 -0
- valgebra_jsonschema/mcp.py +72 -0
- valgebra_jsonschema/py.typed +0 -0
- valgebra_jsonschema/sample.py +233 -0
- valgebra_jsonschema/schema.py +2725 -0
- valgebra_jsonschema/study.py +158 -0
- valgebra_jsonschema/suite.py +115 -0
- valgebra_jsonschema-0.0.1.dist-info/METADATA +130 -0
- valgebra_jsonschema-0.0.1.dist-info/RECORD +14 -0
- valgebra_jsonschema-0.0.1.dist-info/WHEEL +4 -0
- valgebra_jsonschema-0.0.1.dist-info/licenses/LICENSE-APACHE +202 -0
- valgebra_jsonschema-0.0.1.dist-info/licenses/LICENSE-MIT +21 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""JSON Schema, read into valgebra's sets over the values `json.loads` returns."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from valgebra_jsonschema.mcp import Tool, read_tool
|
|
6
|
+
from valgebra_jsonschema.schema import (
|
|
7
|
+
ANNOTATIONS,
|
|
8
|
+
ARRAY,
|
|
9
|
+
BOOLEAN,
|
|
10
|
+
INTEGER,
|
|
11
|
+
JSON,
|
|
12
|
+
NULL,
|
|
13
|
+
NUMBER,
|
|
14
|
+
OBJECT,
|
|
15
|
+
REFUSED,
|
|
16
|
+
STRING,
|
|
17
|
+
SUPPORTED,
|
|
18
|
+
Compiler,
|
|
19
|
+
Interval,
|
|
20
|
+
Unsupported,
|
|
21
|
+
UnsupportedDialect,
|
|
22
|
+
branches,
|
|
23
|
+
compile_schema,
|
|
24
|
+
compile_with_fallbacks,
|
|
25
|
+
constants,
|
|
26
|
+
ecma,
|
|
27
|
+
finite,
|
|
28
|
+
interval,
|
|
29
|
+
json_key,
|
|
30
|
+
older,
|
|
31
|
+
spellings,
|
|
32
|
+
upgrade,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
__all__ = [
|
|
36
|
+
"ANNOTATIONS",
|
|
37
|
+
"ARRAY",
|
|
38
|
+
"BOOLEAN",
|
|
39
|
+
"INTEGER",
|
|
40
|
+
"JSON",
|
|
41
|
+
"NULL",
|
|
42
|
+
"NUMBER",
|
|
43
|
+
"OBJECT",
|
|
44
|
+
"REFUSED",
|
|
45
|
+
"STRING",
|
|
46
|
+
"SUPPORTED",
|
|
47
|
+
"Compiler",
|
|
48
|
+
"Interval",
|
|
49
|
+
"Tool",
|
|
50
|
+
"Unsupported",
|
|
51
|
+
"UnsupportedDialect",
|
|
52
|
+
"branches",
|
|
53
|
+
"compile_schema",
|
|
54
|
+
"compile_with_fallbacks",
|
|
55
|
+
"constants",
|
|
56
|
+
"ecma",
|
|
57
|
+
"finite",
|
|
58
|
+
"interval",
|
|
59
|
+
"json_key",
|
|
60
|
+
"older",
|
|
61
|
+
"read_tool",
|
|
62
|
+
"spellings",
|
|
63
|
+
"upgrade",
|
|
64
|
+
]
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"""Every valgebra name the package uses, and the recipes it composes from them.
|
|
2
|
+
|
|
3
|
+
Nothing else in the package imports valgebra: a valgebra release that renames or moves
|
|
4
|
+
a name is one file's edit here, and a test holds the boundary. The names come from the
|
|
5
|
+
``valgebra`` package, whose ``__all__`` is the supported surface, never from the
|
|
6
|
+
extension module underneath it.
|
|
7
|
+
|
|
8
|
+
valgebra ships the minimal Boolean algebra and leaves the derived forms to be composed;
|
|
9
|
+
the ones this package needs are written once, below, as the recipes of valgebra's own
|
|
10
|
+
documentation (`docs/04-algebra.md`, "Composition recipes"). A bound reaches valgebra as
|
|
11
|
+
an ``annotated_types`` marker, the one constraint vocabulary it reads.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from collections.abc import Sequence
|
|
17
|
+
from itertools import combinations
|
|
18
|
+
from typing import Annotated, Any, TypedDict
|
|
19
|
+
|
|
20
|
+
from annotated_types import Len, Predicate
|
|
21
|
+
from valgebra import (
|
|
22
|
+
MAX_SCHEMA_DEPTH,
|
|
23
|
+
Regex,
|
|
24
|
+
ValidationError,
|
|
25
|
+
Validator,
|
|
26
|
+
anything,
|
|
27
|
+
complement,
|
|
28
|
+
intersection,
|
|
29
|
+
nothing,
|
|
30
|
+
recursive,
|
|
31
|
+
union,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
"MAX_SCHEMA_DEPTH",
|
|
36
|
+
"NOT_BOOL",
|
|
37
|
+
"Asked",
|
|
38
|
+
"Regex",
|
|
39
|
+
"ValidationError",
|
|
40
|
+
"Validator",
|
|
41
|
+
"anything",
|
|
42
|
+
"complement",
|
|
43
|
+
"each",
|
|
44
|
+
"exactly_one",
|
|
45
|
+
"fixed",
|
|
46
|
+
"guard",
|
|
47
|
+
"has",
|
|
48
|
+
"implies",
|
|
49
|
+
"intersection",
|
|
50
|
+
"nothing",
|
|
51
|
+
"opaque",
|
|
52
|
+
"recursive",
|
|
53
|
+
"union",
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
#: Every value but a `bool`, which subclasses `int` and so lies inside a bare `int`.
|
|
57
|
+
NOT_BOOL = complement(bool)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def implies(
|
|
61
|
+
condition: object,
|
|
62
|
+
then: object,
|
|
63
|
+
otherwise: object = anything,
|
|
64
|
+
*,
|
|
65
|
+
complemented: object | None = None,
|
|
66
|
+
) -> Validator[Any]:
|
|
67
|
+
"""Return ``(condition & then) | (~condition & otherwise)``: the conditional recipe.
|
|
68
|
+
|
|
69
|
+
``complemented``, where given, is the set ``condition`` is complemented as: one that
|
|
70
|
+
agrees with it on every value ``otherwise`` holds, so the complement can leave out a meet
|
|
71
|
+
``otherwise`` already carries.
|
|
72
|
+
"""
|
|
73
|
+
negated = complement(condition if complemented is None else complemented)
|
|
74
|
+
return union(intersection(condition, then), intersection(negated, otherwise))
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def guard(kind: object, refined: object) -> Validator[Any]:
|
|
78
|
+
"""Return the values outside ``kind``, or the ``refined`` ones: a keyword's scope.
|
|
79
|
+
|
|
80
|
+
It is `implies` with nothing to say outside the kind, written as the union it reduces
|
|
81
|
+
to, which keeps the normal form one level shallower.
|
|
82
|
+
"""
|
|
83
|
+
return union(complement(kind), refined)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def has(key: str) -> Validator[Any]:
|
|
87
|
+
"""Return the mappings that carry ``key``: an open record requiring it.
|
|
88
|
+
|
|
89
|
+
A key ending in ``?`` is named through a ``TypedDict``, which takes a key whole, where
|
|
90
|
+
the dict literal reads that suffix as "optional"; opened, the two records are one set.
|
|
91
|
+
"""
|
|
92
|
+
if key.endswith("?"):
|
|
93
|
+
return Validator(TypedDict("Has", {key: Any})).open() # ty: ignore[invalid-argument-type]
|
|
94
|
+
return Validator({key: anything}).open()
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
#: Whether valgebra leaves the meet of two sets possibly nonempty, by the two objects in the
|
|
98
|
+
#: order asked, each entry holding its two so that neither id is another object's while it
|
|
99
|
+
#: stands.
|
|
100
|
+
Asked = dict[tuple[int, int], tuple[Validator[Any], Validator[Any], bool]]
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def exactly_one(
|
|
104
|
+
*branches: Validator[Any],
|
|
105
|
+
complemented: Sequence[Validator[Any]] | None = None,
|
|
106
|
+
asked: Asked | None = None,
|
|
107
|
+
) -> Validator[Any]:
|
|
108
|
+
"""Return the values in exactly one branch: each branch minus every other it may meet.
|
|
109
|
+
|
|
110
|
+
A branch valgebra proves disjoint from another holds none of its values, so ``b & ~o``
|
|
111
|
+
is ``b`` and the complement is left out. Spelled whole, each of ``n`` branches appears
|
|
112
|
+
``n`` times, and a ``oneOf`` nested in its own branches multiplies a branch by ``n`` at
|
|
113
|
+
every level, past valgebra's node bound. A pair not proved disjoint keeps the
|
|
114
|
+
complement, and a placeholder of a fixpoint still being built is never proved empty,
|
|
115
|
+
so the set is the same either way.
|
|
116
|
+
|
|
117
|
+
``complemented``, where given, holds the set each branch is complemented as: one that
|
|
118
|
+
agrees with it on every value a branch holds. ``asked``, where given, holds the pairs
|
|
119
|
+
asked before and takes the ones asked here: a pair of the same two objects, asked in
|
|
120
|
+
the same order, is the same question, and a document whose ``oneOf`` names the same
|
|
121
|
+
targets at every use asks it at each.
|
|
122
|
+
"""
|
|
123
|
+
negated = branches if complemented is None else complemented
|
|
124
|
+
asked = {} if asked is None else asked
|
|
125
|
+
meet = {
|
|
126
|
+
(i, j): _meets(branches[i], branches[j], asked)
|
|
127
|
+
for i, j in combinations(range(len(branches)), 2)
|
|
128
|
+
}
|
|
129
|
+
return union(
|
|
130
|
+
*(
|
|
131
|
+
intersection(
|
|
132
|
+
b,
|
|
133
|
+
*(
|
|
134
|
+
complement(o)
|
|
135
|
+
for j, o in enumerate(negated)
|
|
136
|
+
if j != i and meet[min(i, j), max(i, j)]
|
|
137
|
+
),
|
|
138
|
+
)
|
|
139
|
+
for i, b in enumerate(branches)
|
|
140
|
+
)
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _meets(a: Validator[Any], b: Validator[Any], asked: Asked) -> bool:
|
|
145
|
+
"""Return whether valgebra leaves ``a & b`` possibly nonempty, asking once a pair."""
|
|
146
|
+
key = (id(a), id(b))
|
|
147
|
+
if (kept := asked.get(key)) is None:
|
|
148
|
+
kept = asked[key] = (a, b, not intersection(a, b).is_empty())
|
|
149
|
+
return kept[2]
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def each(schema: object) -> Validator[Any]:
|
|
153
|
+
"""Return the lists whose every element lies in ``schema``."""
|
|
154
|
+
return Validator(list[schema]) # ty: ignore[invalid-type-form]
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def fixed(parts: list[Validator[Any]]) -> Validator[Any]:
|
|
158
|
+
"""Return the lists of exactly these elements, position by position.
|
|
159
|
+
|
|
160
|
+
A one-element list literal is valgebra's homogeneous list, so a fixed length of one
|
|
161
|
+
is a homogeneous list pinned to length one.
|
|
162
|
+
"""
|
|
163
|
+
if not parts:
|
|
164
|
+
return Validator([])
|
|
165
|
+
if len(parts) == 1:
|
|
166
|
+
return intersection(Annotated[list[Any], Len(1, 1)], each(parts[0]))
|
|
167
|
+
return Validator(parts)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def opaque(base: object, test: Any) -> Validator[Any]:
|
|
171
|
+
"""Return the members of ``base`` that ``test`` admits: valgebra's callback path.
|
|
172
|
+
|
|
173
|
+
Membership is exact; every relation that has to look inside ``test`` answers
|
|
174
|
+
"undecided". It is the last resort, kept as small as the part of a meaning the
|
|
175
|
+
algebra cannot carry.
|
|
176
|
+
"""
|
|
177
|
+
return Validator(Annotated[base, Predicate(test)]) # ty: ignore[invalid-type-form]
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""The ``format`` values read when a caller asks for them to assert.
|
|
2
|
+
|
|
3
|
+
Draft 2020-12 makes ``format`` an annotation by default, so this applies only on request. A
|
|
4
|
+
format with a regular language is a pattern, so a relation through it is decided; the rest are
|
|
5
|
+
predicates, exact on membership and opaque to a relation. Digits are ASCII throughout, since the
|
|
6
|
+
grammars these come from are written over ASCII.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import ipaddress
|
|
12
|
+
import re
|
|
13
|
+
from collections.abc import Callable
|
|
14
|
+
|
|
15
|
+
_DAYS_31 = "(?:0[13578]|1[02])-(?:0[1-9]|[12][0-9]|3[01])"
|
|
16
|
+
_DAYS_30 = "(?:0[469]|11)-(?:0[1-9]|[12][0-9]|30)"
|
|
17
|
+
_DAYS_28 = "02-(?:0[1-9]|1[0-9]|2[0-8])"
|
|
18
|
+
_LEAP_YEAR = "(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:[02468][048]|[13579][26])00)"
|
|
19
|
+
_DATE = f"(?:[0-9]{{4}}-(?:{_DAYS_31}|{_DAYS_30}|{_DAYS_28})|{_LEAP_YEAR}-02-29)"
|
|
20
|
+
_OCTET = "(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])"
|
|
21
|
+
_HEX4 = "[0-9a-fA-F]{4}"
|
|
22
|
+
_DUR_TIME = "T(?:[0-9]+H(?:[0-9]+M(?:[0-9]+S)?)?|[0-9]+M(?:[0-9]+S)?|[0-9]+S)"
|
|
23
|
+
_DUR_DATE = "(?:[0-9]+D|[0-9]+M(?:[0-9]+D)?|[0-9]+Y(?:[0-9]+M(?:[0-9]+D)?)?)"
|
|
24
|
+
|
|
25
|
+
#: The formats that are regular languages, by a pattern over the whole string.
|
|
26
|
+
LANGUAGES: dict[str, str] = {
|
|
27
|
+
"date": _DATE,
|
|
28
|
+
"duration": f"P(?:[0-9]+W|{_DUR_DATE}(?:{_DUR_TIME})?|{_DUR_TIME})",
|
|
29
|
+
"ipv4": rf"{_OCTET}(?:\.{_OCTET}){{3}}",
|
|
30
|
+
"uuid": f"{_HEX4}{_HEX4}-{_HEX4}-{_HEX4}-{_HEX4}-{_HEX4}{_HEX4}{_HEX4}",
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
_CLOCK = re.compile(
|
|
34
|
+
r"([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.[0-9]+)?(?:[zZ]|([+-])([0-9]{2}):([0-9]{2}))",
|
|
35
|
+
re.ASCII,
|
|
36
|
+
)
|
|
37
|
+
_WHOLE_DATE = re.compile(_DATE, re.ASCII)
|
|
38
|
+
_LABEL = re.compile(r"[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?", re.ASCII)
|
|
39
|
+
_ATOM = re.compile(r"[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+", re.ASCII)
|
|
40
|
+
_QUOTED = re.compile(r'"(?:[^"\\\r\n]|\\[ -~])*"')
|
|
41
|
+
_DAY_MINUTES = 24 * 60
|
|
42
|
+
_MAX_HOSTNAME = 253
|
|
43
|
+
_MAX_HOUR = 23
|
|
44
|
+
_MAX_MINUTE = 59
|
|
45
|
+
_LEAP_SECOND = 60
|
|
46
|
+
_LAST_MINUTE = 23 * 60 + 59
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _is_time(text: str) -> bool:
|
|
50
|
+
"""Return whether ``text`` is RFC 3339's ``full-time``.
|
|
51
|
+
|
|
52
|
+
A leap second is the last second of a UTC day, so where one is read depends on the offset.
|
|
53
|
+
"""
|
|
54
|
+
match = _CLOCK.fullmatch(text)
|
|
55
|
+
if match is None:
|
|
56
|
+
return False
|
|
57
|
+
hour, minute, second = int(match[1]), int(match[2]), int(match[3])
|
|
58
|
+
if hour > _MAX_HOUR or minute > _MAX_MINUTE or second > _LEAP_SECOND:
|
|
59
|
+
return False
|
|
60
|
+
offset = 0
|
|
61
|
+
if match[4]:
|
|
62
|
+
off_hour, off_minute = int(match[5]), int(match[6])
|
|
63
|
+
if off_hour > _MAX_HOUR or off_minute > _MAX_MINUTE:
|
|
64
|
+
return False
|
|
65
|
+
offset = (off_hour * 60 + off_minute) * (1 if match[4] == "+" else -1)
|
|
66
|
+
if second < _LEAP_SECOND:
|
|
67
|
+
return True
|
|
68
|
+
return (hour * 60 + minute - offset) % _DAY_MINUTES == _LAST_MINUTE
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _is_date_time(text: str) -> bool:
|
|
72
|
+
"""Return whether ``text`` is RFC 3339's ``date-time``, whose separator may be lowercase."""
|
|
73
|
+
date, separator, time = text[:10], text[10:11], text[11:]
|
|
74
|
+
return separator in {"T", "t"} and _WHOLE_DATE.fullmatch(date) is not None and _is_time(time)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _is_ipv6(text: str) -> bool:
|
|
78
|
+
"""Return whether ``text`` is an RFC 4291 address, which holds no zone."""
|
|
79
|
+
if "%" in text:
|
|
80
|
+
return False
|
|
81
|
+
try:
|
|
82
|
+
ipaddress.IPv6Address(text)
|
|
83
|
+
except ValueError:
|
|
84
|
+
return False
|
|
85
|
+
return True
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _is_hostname(text: str) -> bool:
|
|
89
|
+
"""Return whether ``text`` is an RFC 1123 hostname.
|
|
90
|
+
|
|
91
|
+
An ``xn--`` label is read as the label it spells: its Punycode and the IDNA rules on what it
|
|
92
|
+
decodes to are not read, as `docs/03-conformance.md` records.
|
|
93
|
+
"""
|
|
94
|
+
if not text or len(text) > _MAX_HOSTNAME:
|
|
95
|
+
return False
|
|
96
|
+
return all(_LABEL.fullmatch(label) for label in text.split("."))
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _is_domain_or_literal(domain: str) -> bool:
|
|
100
|
+
"""Return whether ``domain`` is a hostname, or an address literal in square brackets."""
|
|
101
|
+
if domain.startswith("[") and domain.endswith("]"):
|
|
102
|
+
literal = domain[1:-1]
|
|
103
|
+
if literal[:5].lower() == "ipv6:":
|
|
104
|
+
return _is_ipv6(literal[5:])
|
|
105
|
+
try:
|
|
106
|
+
ipaddress.IPv4Address(literal)
|
|
107
|
+
except ValueError:
|
|
108
|
+
return False
|
|
109
|
+
return True
|
|
110
|
+
return _is_hostname(domain)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _is_email(text: str) -> bool:
|
|
114
|
+
"""Return whether ``text`` is an RFC 5321 ``Mailbox``: a local part, ``@``, and a domain."""
|
|
115
|
+
local, at, domain = text.rpartition("@")
|
|
116
|
+
if not at or not _is_domain_or_literal(domain):
|
|
117
|
+
return False
|
|
118
|
+
if local.startswith('"'):
|
|
119
|
+
return _QUOTED.fullmatch(local) is not None
|
|
120
|
+
return all(_ATOM.fullmatch(atom) for atom in local.split("."))
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
#: The formats held by a function: a predicate, exact on membership and opaque to a relation.
|
|
124
|
+
PREDICATES: dict[str, Callable[[str], bool]] = {
|
|
125
|
+
"date-time": _is_date_time,
|
|
126
|
+
"email": _is_email,
|
|
127
|
+
"hostname": _is_hostname,
|
|
128
|
+
"ipv6": _is_ipv6,
|
|
129
|
+
"time": _is_time,
|
|
130
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""An MCP tool's contract: what it takes and what it returns, as sets.
|
|
2
|
+
|
|
3
|
+
A server lists each tool with an ``inputSchema``, the arguments object, and optionally an
|
|
4
|
+
``outputSchema``. Both are JSON Schema in the dialect their ``$schema`` names, 2020-12 where
|
|
5
|
+
it names none, and the protocol has an unsupported dialect fail, which `compile_schema` does.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from collections.abc import Mapping
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
from valgebra_jsonschema._valgebra import Validator
|
|
15
|
+
from valgebra_jsonschema.schema import (
|
|
16
|
+
ANNOTATIONS,
|
|
17
|
+
Compiler,
|
|
18
|
+
Unsupported,
|
|
19
|
+
compile_schema,
|
|
20
|
+
older,
|
|
21
|
+
upgrade,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True, slots=True)
|
|
26
|
+
class Tool:
|
|
27
|
+
"""A tool's two contracts. A tool with no ``outputSchema`` promises no structure."""
|
|
28
|
+
|
|
29
|
+
name: str
|
|
30
|
+
arguments: Validator[Any]
|
|
31
|
+
result: Validator[Any] | None
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _names_an_object(schema: object) -> bool:
|
|
35
|
+
"""Return whether a root schema is an object schema, following bare references.
|
|
36
|
+
|
|
37
|
+
The protocol has ``inputSchema`` state ``type: "object"``. A root that is a bare ``$ref``
|
|
38
|
+
names one, as a generated schema's named root does, so it holds there.
|
|
39
|
+
"""
|
|
40
|
+
draft = older(schema)
|
|
41
|
+
root = upgrade(schema, draft) if draft is not None else schema
|
|
42
|
+
here = root
|
|
43
|
+
for _ in range(8):
|
|
44
|
+
if not (isinstance(here, dict) and "$ref" in here):
|
|
45
|
+
break
|
|
46
|
+
if set(here) - ANNOTATIONS - {"$ref", "$id", "$defs", "definitions"}:
|
|
47
|
+
return False
|
|
48
|
+
here = Compiler(root).resolve(here["$ref"])
|
|
49
|
+
return isinstance(here, dict) and here.get("type") in ("object", ["object"])
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def read_tool(spec: Mapping[str, Any], *, assert_format: bool = False) -> Tool:
|
|
53
|
+
"""Read one entry of a server's tool list into its two contracts.
|
|
54
|
+
|
|
55
|
+
Raises `Unsupported` for an ``inputSchema`` that is missing or names no object, and for
|
|
56
|
+
anything `compile_schema` refuses, and `UnsupportedDialect` for a dialect it does not read.
|
|
57
|
+
``assert_format`` reads as it does there.
|
|
58
|
+
"""
|
|
59
|
+
name = spec.get("name")
|
|
60
|
+
if not isinstance(name, str):
|
|
61
|
+
msg = f"a tool's name is a string, and this one is {name!r:.60}"
|
|
62
|
+
raise Unsupported(msg)
|
|
63
|
+
if "inputSchema" not in spec:
|
|
64
|
+
msg = f"tool {name!r} has no inputSchema"
|
|
65
|
+
raise Unsupported(msg)
|
|
66
|
+
arguments = compile_schema(spec["inputSchema"], assert_format=assert_format)
|
|
67
|
+
if not _names_an_object(spec["inputSchema"]):
|
|
68
|
+
msg = f"tool {name!r} has an inputSchema that names no object"
|
|
69
|
+
raise Unsupported(msg)
|
|
70
|
+
output = spec.get("outputSchema")
|
|
71
|
+
result = None if output is None else compile_schema(output, assert_format=assert_format)
|
|
72
|
+
return Tool(name, arguments, result)
|
|
File without changes
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"""Draw JSON values from a JSON Schema: the witnesses that prove a cell inhabited.
|
|
2
|
+
|
|
3
|
+
valgebra proves emptiness and refutes inclusion, but names no value. A cell it cannot
|
|
4
|
+
prove empty may still hold nothing, so a cell counts as inhabited only when a concrete
|
|
5
|
+
value is found in it. This module proposes candidates; membership in the compiled set is
|
|
6
|
+
the only judge, so a wrong proposal costs a rejection, never a wrong proof.
|
|
7
|
+
|
|
8
|
+
The generator covers the keywords it can construct values for directly and falls back to
|
|
9
|
+
rejection elsewhere: ``not``, ``if`` and an unsatisfiable mix yield nothing.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import math
|
|
15
|
+
import random
|
|
16
|
+
import re
|
|
17
|
+
import string
|
|
18
|
+
from collections.abc import Mapping
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
from valgebra_jsonschema.schema import Compiler, Unsupported, compile_with_fallbacks
|
|
22
|
+
|
|
23
|
+
_ALPHABET = string.ascii_letters + string.digits + "_-@."
|
|
24
|
+
#: The longest string and list a draw builds. Generated schemas bound lengths by 2**31 - 1,
|
|
25
|
+
#: and a draw that long costs gigabytes; deciding a cell needs no such value.
|
|
26
|
+
_LONGEST_STRING = 4096
|
|
27
|
+
_LONGEST_LIST = 16
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class Sampler:
|
|
31
|
+
"""Draw candidate values for the schemas of one document, from one seeded stream."""
|
|
32
|
+
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
root: object,
|
|
36
|
+
rng: random.Random,
|
|
37
|
+
*,
|
|
38
|
+
depth: int = 4,
|
|
39
|
+
documents: Mapping[str, object] | None = None,
|
|
40
|
+
) -> None:
|
|
41
|
+
self.root = root
|
|
42
|
+
self.rng = rng
|
|
43
|
+
self.depth = depth
|
|
44
|
+
#: The documents a reference may name beside the root, as `compile_schema` takes them.
|
|
45
|
+
self.documents = documents
|
|
46
|
+
self.compiler = Compiler(root, documents=documents)
|
|
47
|
+
|
|
48
|
+
def draw(self, schema: object, tries: int = 40) -> list[Any]:
|
|
49
|
+
"""Return up to ``tries`` distinct candidates that the compiled schema admits."""
|
|
50
|
+
# A schema past valgebra's limit reads by predicate, as `compile_schema` has it.
|
|
51
|
+
self.compiler, target = compile_with_fallbacks(self.root, schema, documents=self.documents)
|
|
52
|
+
found: list[Any] = []
|
|
53
|
+
seen: set[str] = set()
|
|
54
|
+
for _ in range(tries):
|
|
55
|
+
value = self._value(schema, self.depth)
|
|
56
|
+
key = repr(value)
|
|
57
|
+
if key not in seen and target.is_valid(value):
|
|
58
|
+
seen.add(key)
|
|
59
|
+
found.append(value)
|
|
60
|
+
return found
|
|
61
|
+
|
|
62
|
+
def _value(self, schema: object, depth: int) -> Any: # noqa: C901, PLR0911, PLR0912
|
|
63
|
+
rng = self.rng
|
|
64
|
+
if schema is True or not isinstance(schema, dict):
|
|
65
|
+
return rng.choice([None, True, 0, 1.5, "a", [], {}])
|
|
66
|
+
if "$ref" in schema and schema["$ref"].startswith("#/"):
|
|
67
|
+
try:
|
|
68
|
+
node = self.compiler.resolve(schema["$ref"])
|
|
69
|
+
except Unsupported:
|
|
70
|
+
return None
|
|
71
|
+
return self._value(node, depth - 1) if depth > 0 else None
|
|
72
|
+
if "const" in schema:
|
|
73
|
+
return schema["const"]
|
|
74
|
+
if "enum" in schema:
|
|
75
|
+
return rng.choice(schema["enum"]) if schema["enum"] else None
|
|
76
|
+
for key in ("anyOf", "oneOf"):
|
|
77
|
+
if key in schema:
|
|
78
|
+
return self._value(rng.choice(schema[key]), depth)
|
|
79
|
+
if "allOf" in schema:
|
|
80
|
+
merged = {k: v for k, v in schema.items() if k != "allOf"}
|
|
81
|
+
for sub in schema["allOf"]:
|
|
82
|
+
if isinstance(sub, dict):
|
|
83
|
+
merged |= sub
|
|
84
|
+
return self._value(merged, depth)
|
|
85
|
+
kinds = schema.get("type", ["null", "boolean", "integer", "number", "string"])
|
|
86
|
+
kind = rng.choice(kinds) if isinstance(kinds, list) else kinds
|
|
87
|
+
if kind == "null":
|
|
88
|
+
return None
|
|
89
|
+
if kind == "boolean":
|
|
90
|
+
return rng.random() < 0.5 # noqa: PLR2004
|
|
91
|
+
if kind in {"integer", "number"}:
|
|
92
|
+
return self._number(schema, integer=kind == "integer")
|
|
93
|
+
if kind == "string":
|
|
94
|
+
return self._string(schema)
|
|
95
|
+
if kind == "array":
|
|
96
|
+
return self._array(schema, depth)
|
|
97
|
+
if kind == "object":
|
|
98
|
+
return self._object(schema, depth)
|
|
99
|
+
return None
|
|
100
|
+
|
|
101
|
+
def _number(self, schema: dict[str, Any], *, integer: bool) -> int | float:
|
|
102
|
+
low = _finite(schema.get("minimum", schema.get("exclusiveMinimum")), -1000)
|
|
103
|
+
high = _finite(schema.get("maximum", schema.get("exclusiveMaximum")), 1000)
|
|
104
|
+
if "exclusiveMinimum" in schema:
|
|
105
|
+
low += 1
|
|
106
|
+
if "exclusiveMaximum" in schema:
|
|
107
|
+
high -= 1
|
|
108
|
+
low, high = int(low), max(int(low), int(high))
|
|
109
|
+
value = self.rng.randint(low, high)
|
|
110
|
+
step = schema.get("multipleOf")
|
|
111
|
+
if isinstance(step, int) and step > 0:
|
|
112
|
+
value -= value % step
|
|
113
|
+
if integer or self.rng.random() < 0.5: # noqa: PLR2004
|
|
114
|
+
return value
|
|
115
|
+
# Past the float range an integer has no non-integral neighbor to draw.
|
|
116
|
+
return value + 0.5 if abs(value) < 2**1023 else value
|
|
117
|
+
|
|
118
|
+
def _string(self, schema: dict[str, Any]) -> str:
|
|
119
|
+
low = int(schema.get("minLength", 0))
|
|
120
|
+
if low > _LONGEST_STRING:
|
|
121
|
+
return ""
|
|
122
|
+
high = min(max(low, int(schema.get("maxLength", low + 8))), low + _LONGEST_STRING)
|
|
123
|
+
n = self.rng.randint(low, high)
|
|
124
|
+
text = "".join(self.rng.choice(_ALPHABET) for _ in range(n))
|
|
125
|
+
if "pattern" in schema:
|
|
126
|
+
text = _from_pattern(schema["pattern"], self.rng) or text
|
|
127
|
+
return text
|
|
128
|
+
|
|
129
|
+
def _array(self, schema: dict[str, Any], depth: int) -> list[Any]:
|
|
130
|
+
prefix = schema.get("prefixItems", [])
|
|
131
|
+
low = int(schema.get("minItems", 0))
|
|
132
|
+
if low > _LONGEST_LIST:
|
|
133
|
+
return []
|
|
134
|
+
high = min(max(low, int(schema.get("maxItems", low + 3))), low + _LONGEST_LIST)
|
|
135
|
+
n = self.rng.randint(low, high) if depth > 0 else low
|
|
136
|
+
items = schema.get("items", True)
|
|
137
|
+
return [self._value(prefix[i] if i < len(prefix) else items, depth - 1) for i in range(n)]
|
|
138
|
+
|
|
139
|
+
def _object(self, schema: dict[str, Any], depth: int) -> dict[str, Any]:
|
|
140
|
+
props: dict[str, Any] = schema.get("properties", {})
|
|
141
|
+
required = set(schema.get("required", []))
|
|
142
|
+
out: dict[str, Any] = {}
|
|
143
|
+
for key, sub in props.items():
|
|
144
|
+
if key in required or (depth > 0 and self.rng.random() < 0.5): # noqa: PLR2004
|
|
145
|
+
out[key] = self._value(sub, depth - 1)
|
|
146
|
+
return out
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _finite(bound: object, default: int) -> float:
|
|
150
|
+
"""Return a numeric bound, or ``default`` for none or one past the float range."""
|
|
151
|
+
if isinstance(bound, bool) or not isinstance(bound, (int, float)):
|
|
152
|
+
return default
|
|
153
|
+
if isinstance(bound, float) and not math.isfinite(bound):
|
|
154
|
+
return default
|
|
155
|
+
return bound
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _from_pattern(pattern: str, rng: random.Random) -> str | None:
|
|
159
|
+
r"""Build a string for the simple patterns contracts use, or None.
|
|
160
|
+
|
|
161
|
+
Covers an anchored sequence of literals, character classes and ``\\d``/``\\w`` with
|
|
162
|
+
``+``, ``*``, ``?`` and ``{m}``/``{m,n}`` counts. Anything else is left to the caller's
|
|
163
|
+
fallback, and membership decides either way.
|
|
164
|
+
"""
|
|
165
|
+
body = pattern.removeprefix("^").removesuffix("$")
|
|
166
|
+
out: list[str] = []
|
|
167
|
+
i = 0
|
|
168
|
+
while i < len(body):
|
|
169
|
+
ch = body[i]
|
|
170
|
+
if ch == "\\" and i + 1 < len(body):
|
|
171
|
+
pool = _ESCAPES.get(body[i + 1], body[i + 1])
|
|
172
|
+
i += 2
|
|
173
|
+
elif ch == "[":
|
|
174
|
+
end = body.find("]", i)
|
|
175
|
+
if end < 0:
|
|
176
|
+
return None
|
|
177
|
+
pool = _expand_class(body[i + 1 : end])
|
|
178
|
+
i = end + 1
|
|
179
|
+
elif ch in "()|.":
|
|
180
|
+
return None
|
|
181
|
+
else:
|
|
182
|
+
pool = ch
|
|
183
|
+
i += 1
|
|
184
|
+
low, high, i = _count(body, i, rng)
|
|
185
|
+
out.extend(rng.choice(pool) for _ in range(rng.randint(low, high)))
|
|
186
|
+
return "".join(out)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
_PRINTABLE = string.ascii_letters + string.digits + "_-.@!~+"
|
|
190
|
+
_ESCAPES = {"d": string.digits, "w": string.ascii_letters + string.digits + "_", "s": " \t"}
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _expand_class(spec: str) -> str:
|
|
194
|
+
"""Return the characters a bracket class admits, from a printable stock for ``[^...]``."""
|
|
195
|
+
negated = spec.startswith("^")
|
|
196
|
+
body = spec[1:] if negated else spec
|
|
197
|
+
chars: list[str] = []
|
|
198
|
+
j = 0
|
|
199
|
+
while j < len(body):
|
|
200
|
+
if body[j] == "\\" and j + 1 < len(body):
|
|
201
|
+
chars.extend(_ESCAPES.get(body[j + 1], body[j + 1]))
|
|
202
|
+
j += 2
|
|
203
|
+
elif j + 2 < len(body) and body[j + 1] == "-":
|
|
204
|
+
chars.extend(chr(c) for c in range(ord(body[j]), ord(body[j + 2]) + 1))
|
|
205
|
+
j += 3
|
|
206
|
+
else:
|
|
207
|
+
chars.append(body[j])
|
|
208
|
+
j += 1
|
|
209
|
+
if negated:
|
|
210
|
+
return "".join(c for c in _PRINTABLE if c not in chars) or "a"
|
|
211
|
+
return "".join(chars) or "a"
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _count(body: str, i: int, rng: random.Random | None = None) -> tuple[int, int, int]:
|
|
215
|
+
"""Return the repeat range at ``body[i]`` and the index after it.
|
|
216
|
+
|
|
217
|
+
An open-ended repeat is usually short and sometimes long, so that cells carved out by a
|
|
218
|
+
length bound elsewhere -- an e-mail address longer than a subject may be -- get drawn.
|
|
219
|
+
"""
|
|
220
|
+
if i < len(body) and body[i] in "+*?":
|
|
221
|
+
if body[i] != "?" and rng is not None and rng.random() < 0.2: # noqa: PLR2004
|
|
222
|
+
return (*rng.choice([(130, 130), (210, 210)]), i + 1)
|
|
223
|
+
return {"+": (1, 4), "*": (0, 4), "?": (0, 1)}[body[i]] + (i + 1,)
|
|
224
|
+
count = _COUNT.match(body, i)
|
|
225
|
+
if count is not None:
|
|
226
|
+
low = int(count[1])
|
|
227
|
+
high = int(count[3]) if count[3] else low + 3 if count[2] else low
|
|
228
|
+
return low, high, count.end()
|
|
229
|
+
return 1, 1, i
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
#: A repeat count, ``{m}``, ``{m,}`` or ``{m,n}``. Any other ``{`` is a literal brace.
|
|
233
|
+
_COUNT = re.compile(r"\{([0-9]+)(,([0-9]*))?\}")
|