@agent-blueprint-protocol/verifier 0.7.0
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.
- package/CHANGELOG.md +891 -0
- package/LICENSE +190 -0
- package/NOTICE +4 -0
- package/README.md +199 -0
- package/dist/corpus/cases/base64url-decode.json +1 -0
- package/dist/corpus/cases/blueprint-decode.json +1 -0
- package/dist/corpus/cases/bounds-new.json +1 -0
- package/dist/corpus/cases/bounds_algebra-intersect.json +1 -0
- package/dist/corpus/cases/canonicalization-encode.json +1 -0
- package/dist/corpus/cases/compatibility-verify.json +1 -0
- package/dist/corpus/cases/deployment-decode.json +1 -0
- package/dist/corpus/cases/digest-tagged.json +1 -0
- package/dist/corpus/cases/extension-resolve.json +1 -0
- package/dist/corpus/cases/federation-decode.json +1 -0
- package/dist/corpus/cases/federation-verify_commitment.json +1 -0
- package/dist/corpus/cases/json-decode.json +1 -0
- package/dist/corpus/cases/negotiation-negotiate.json +1 -0
- package/dist/corpus/cases/portability-scan.json +1 -0
- package/dist/corpus/cases/schema-validate_instance.json +1 -0
- package/dist/corpus/cases/signature-verify.json +1 -0
- package/dist/corpus/index.json +1 -0
- package/dist/corpus/schemas/estate-contract.schema.json +1 -0
- package/dist/corpus/schemas/object.schema.json +1 -0
- package/dist/corpus/vectors/blueprint-golden.json +1 -0
- package/dist/corpus/vectors/deployment-golden.json +1 -0
- package/dist/corpus/vectors/rfc8785-numbers.json +1 -0
- package/dist/kit/cli.mjs +24 -0
- package/dist/verifier/b64url.d.ts +16 -0
- package/dist/verifier/b64url.js +36 -0
- package/dist/verifier/blueprint.d.ts +56 -0
- package/dist/verifier/blueprint.js +795 -0
- package/dist/verifier/bounds.d.ts +19 -0
- package/dist/verifier/bounds.js +45 -0
- package/dist/verifier/bounds_algebra.d.ts +27 -0
- package/dist/verifier/bounds_algebra.js +282 -0
- package/dist/verifier/canonical.d.ts +17 -0
- package/dist/verifier/canonical.js +160 -0
- package/dist/verifier/cli.d.ts +1 -0
- package/dist/verifier/cli.js +149 -0
- package/dist/verifier/compatibility.d.ts +22 -0
- package/dist/verifier/compatibility.js +112 -0
- package/dist/verifier/corpus.d.ts +42 -0
- package/dist/verifier/corpus.js +672 -0
- package/dist/verifier/decode.d.ts +16 -0
- package/dist/verifier/decode.js +457 -0
- package/dist/verifier/deployment.d.ts +49 -0
- package/dist/verifier/deployment.js +830 -0
- package/dist/verifier/digest.d.ts +23 -0
- package/dist/verifier/digest.js +58 -0
- package/dist/verifier/extension.d.ts +11 -0
- package/dist/verifier/extension.js +75 -0
- package/dist/verifier/federation.d.ts +76 -0
- package/dist/verifier/federation.js +817 -0
- package/dist/verifier/negotiation.d.ts +29 -0
- package/dist/verifier/negotiation.js +324 -0
- package/dist/verifier/portability.d.ts +12 -0
- package/dist/verifier/portability.js +184 -0
- package/dist/verifier/predicate.d.ts +10 -0
- package/dist/verifier/predicate.js +172 -0
- package/dist/verifier/registry.d.ts +16 -0
- package/dist/verifier/registry.js +120 -0
- package/dist/verifier/registry_engine.d.ts +42 -0
- package/dist/verifier/registry_engine.js +220 -0
- package/dist/verifier/report.d.ts +13 -0
- package/dist/verifier/report.js +29 -0
- package/dist/verifier/run_corpus.d.ts +1 -0
- package/dist/verifier/run_corpus.js +81 -0
- package/dist/verifier/runner.d.ts +20 -0
- package/dist/verifier/runner.js +778 -0
- package/dist/verifier/schema.d.ts +20 -0
- package/dist/verifier/schema.js +623 -0
- package/dist/verifier/self_checks.d.ts +1 -0
- package/dist/verifier/self_checks.js +316 -0
- package/dist/verifier/signature.d.ts +23 -0
- package/dist/verifier/signature.js +226 -0
- package/dist/verifier/value.d.ts +44 -0
- package/dist/verifier/value.js +77 -0
- package/package.json +42 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Value } from "./value.ts";
|
|
2
|
+
export declare function dialect(): string;
|
|
3
|
+
export interface ParsedSchema {
|
|
4
|
+
root: Value;
|
|
5
|
+
pointers: Map<string, Value>;
|
|
6
|
+
}
|
|
7
|
+
export declare function parse(value: Value, dialectId: string): {
|
|
8
|
+
ok: true;
|
|
9
|
+
v: ParsedSchema;
|
|
10
|
+
} | {
|
|
11
|
+
ok: false;
|
|
12
|
+
e: string;
|
|
13
|
+
};
|
|
14
|
+
export declare function validateInstance(schema: Value, instance: Value, dialectId: string): {
|
|
15
|
+
ok: true;
|
|
16
|
+
} | {
|
|
17
|
+
ok: false;
|
|
18
|
+
e: string;
|
|
19
|
+
};
|
|
20
|
+
export declare function equal(a: Value, b: Value): boolean;
|
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
// Bounded JSON Schema 2020-12 dialect + instance validator — the mirror of
|
|
2
|
+
// AgentBlueprintProtocol.Schema: the closed 16-keyword subset, positional
|
|
3
|
+
// keyword recognition, document-local $ref (RFC 6901 pointers), the 512
|
|
4
|
+
// complexity ceiling, application-edge cycle denial, and evaluation in the
|
|
5
|
+
// pinned canonical keyword order with (schema-node, instance-location)
|
|
6
|
+
// memoization. Numbers compare by mathematical value across the int/float
|
|
7
|
+
// tags; string lengths count codepoints.
|
|
8
|
+
import { err, ok } from "./value.js";
|
|
9
|
+
const DIALECT = "https://json-schema.org/draft/2020-12/schema";
|
|
10
|
+
const CEILING = 512;
|
|
11
|
+
const TYPE_NAMES = new Set(["null", "boolean", "object", "array", "number", "string", "integer"]);
|
|
12
|
+
// Canonical evaluation order — the FIRST failure in this sequence is the
|
|
13
|
+
// reported reason, independent of document member order.
|
|
14
|
+
const KEYWORD_ORDER = [
|
|
15
|
+
"$ref",
|
|
16
|
+
"type",
|
|
17
|
+
"enum",
|
|
18
|
+
"const",
|
|
19
|
+
"minimum",
|
|
20
|
+
"maximum",
|
|
21
|
+
"minLength",
|
|
22
|
+
"maxLength",
|
|
23
|
+
"minItems",
|
|
24
|
+
"maxItems",
|
|
25
|
+
"required",
|
|
26
|
+
"properties",
|
|
27
|
+
"items",
|
|
28
|
+
"additionalProperties",
|
|
29
|
+
"oneOf",
|
|
30
|
+
];
|
|
31
|
+
const SCHEMA_VALUED = ["items", "additionalProperties"];
|
|
32
|
+
const NAME_MAP_VALUED = ["properties", "$defs"];
|
|
33
|
+
export function dialect() {
|
|
34
|
+
return DIALECT;
|
|
35
|
+
}
|
|
36
|
+
export function parse(value, dialectId) {
|
|
37
|
+
if (dialectId !== DIALECT)
|
|
38
|
+
return err("schema_dialect_unknown");
|
|
39
|
+
if (!rootShape(value))
|
|
40
|
+
return err("schema_invalid_shape");
|
|
41
|
+
if (!wellFormed(value))
|
|
42
|
+
return err("schema_keyword_value_invalid");
|
|
43
|
+
const walked = walk(value, [], meter0(), new Map(), [], true);
|
|
44
|
+
if (!walked.ok)
|
|
45
|
+
return walked;
|
|
46
|
+
const { acc, pointers, edges } = walked.v;
|
|
47
|
+
for (const [, tokens] of edges) {
|
|
48
|
+
if (!pointers.has(pathKey(tokens)))
|
|
49
|
+
return err("schema_ref_unresolvable");
|
|
50
|
+
}
|
|
51
|
+
if (!cycleFree(pointers))
|
|
52
|
+
return err("schema_ref_cycle");
|
|
53
|
+
return ok({ root: value, pointers });
|
|
54
|
+
}
|
|
55
|
+
export function validateInstance(schema, instance, dialectId) {
|
|
56
|
+
if (dialectId !== DIALECT)
|
|
57
|
+
return err("schema_dialect_unknown");
|
|
58
|
+
const parsed = parse(schema, DIALECT);
|
|
59
|
+
if (!parsed.ok)
|
|
60
|
+
return parsed;
|
|
61
|
+
if (!wellFormed(instance))
|
|
62
|
+
return err("invalid_type");
|
|
63
|
+
const result = evaluate(parsed.v.root, instance, [], new Map(), parsed.v.pointers);
|
|
64
|
+
if (result.ok)
|
|
65
|
+
return ok(undefined);
|
|
66
|
+
return err(result.e);
|
|
67
|
+
}
|
|
68
|
+
// Structural equality per core §4.2.2 (public on the twin; Predicate consumes
|
|
69
|
+
// the one equality law): numbers by mathematical value across tags, strings
|
|
70
|
+
// codepoint-for-codepoint, arrays order-sensitive, objects order-blind with
|
|
71
|
+
// equal member counts.
|
|
72
|
+
export function equal(a, b) {
|
|
73
|
+
if ((a.t === "int" || a.t === "float") &&
|
|
74
|
+
(b.t === "int" || b.t === "float")) {
|
|
75
|
+
return a.v === b.v;
|
|
76
|
+
}
|
|
77
|
+
if (a.t !== b.t)
|
|
78
|
+
return false;
|
|
79
|
+
switch (a.t) {
|
|
80
|
+
case "null":
|
|
81
|
+
return true;
|
|
82
|
+
case "bool":
|
|
83
|
+
case "str":
|
|
84
|
+
return a.v === b.v;
|
|
85
|
+
case "arr":
|
|
86
|
+
return (b.t === "arr" &&
|
|
87
|
+
a.v.length === b.v.length &&
|
|
88
|
+
a.v.every((x, i) => equal(x, b.v[i])));
|
|
89
|
+
case "obj":
|
|
90
|
+
if (b.t !== "obj")
|
|
91
|
+
return false;
|
|
92
|
+
if (a.v.length !== b.v.length)
|
|
93
|
+
return false;
|
|
94
|
+
return a.v.every(([name, value]) => {
|
|
95
|
+
const other = b.v.find(([n]) => n === name);
|
|
96
|
+
return other !== undefined && equal(value, other[1]);
|
|
97
|
+
});
|
|
98
|
+
// int/float cannot reach the switch (the numeric cross-compare above
|
|
99
|
+
// already returned); the default only completes the switch for the compiler.
|
|
100
|
+
default:
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// ---- gates ----------------------------------------------------------------------
|
|
105
|
+
function rootShape(value) {
|
|
106
|
+
return value.t === "obj" || value.t === "bool";
|
|
107
|
+
}
|
|
108
|
+
// The tagged-value well-formedness gate (decoder output always passes; this
|
|
109
|
+
// is the fail-closed guard for hand-built terms).
|
|
110
|
+
function wellFormed(value) {
|
|
111
|
+
switch (value.t) {
|
|
112
|
+
case "null":
|
|
113
|
+
case "bool":
|
|
114
|
+
case "int":
|
|
115
|
+
case "float":
|
|
116
|
+
return true;
|
|
117
|
+
case "str":
|
|
118
|
+
return validString(value.v);
|
|
119
|
+
case "arr":
|
|
120
|
+
return value.v.every(wellFormed);
|
|
121
|
+
case "obj": {
|
|
122
|
+
const seen = new Set();
|
|
123
|
+
for (const [name, member] of value.v) {
|
|
124
|
+
if (typeof name !== "string" || !validString(name) || seen.has(name))
|
|
125
|
+
return false;
|
|
126
|
+
if (!wellFormed(member))
|
|
127
|
+
return false;
|
|
128
|
+
seen.add(name);
|
|
129
|
+
}
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function validString(s) {
|
|
135
|
+
for (let i = 0; i < s.length; i++) {
|
|
136
|
+
const c = s.charCodeAt(i);
|
|
137
|
+
if (c >= 0xd800 && c <= 0xdbff) {
|
|
138
|
+
const next = s.charCodeAt(i + 1);
|
|
139
|
+
if (!(next >= 0xdc00 && next <= 0xdfff))
|
|
140
|
+
return false;
|
|
141
|
+
i++;
|
|
142
|
+
}
|
|
143
|
+
else if (c >= 0xdc00 && c <= 0xdfff) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
function meter0() {
|
|
150
|
+
return { nodes: 0, keywords: 0, branches: 0, depth: 0 };
|
|
151
|
+
}
|
|
152
|
+
function meterSum(m) {
|
|
153
|
+
return m.nodes + m.keywords + m.branches + 4 * m.depth;
|
|
154
|
+
}
|
|
155
|
+
function pathKey(path) {
|
|
156
|
+
return JSON.stringify(path);
|
|
157
|
+
}
|
|
158
|
+
function walk(term, path, acc, pointers, edges, validate) {
|
|
159
|
+
const stepped = {
|
|
160
|
+
...acc,
|
|
161
|
+
nodes: acc.nodes + 1,
|
|
162
|
+
depth: Math.max(acc.depth, path.length + 1),
|
|
163
|
+
};
|
|
164
|
+
if (term.t === "obj") {
|
|
165
|
+
if (validate && meterSum(stepped) > CEILING)
|
|
166
|
+
return err("schema_complexity_exceeded");
|
|
167
|
+
const next = new Map(pointers);
|
|
168
|
+
next.set(pathKey(path), term);
|
|
169
|
+
return walkMembers(term.v, path, stepped, next, edges, validate);
|
|
170
|
+
}
|
|
171
|
+
if (term.t === "bool") {
|
|
172
|
+
const next = new Map(pointers);
|
|
173
|
+
next.set(pathKey(path), term);
|
|
174
|
+
return ok({ acc: stepped, pointers: next, edges });
|
|
175
|
+
}
|
|
176
|
+
return ok({ acc: stepped, pointers, edges });
|
|
177
|
+
}
|
|
178
|
+
function walkMembers(members, path, acc, pointers, edges, validate) {
|
|
179
|
+
if (members.length === 0)
|
|
180
|
+
return ok({ acc, pointers, edges });
|
|
181
|
+
const [[key, value], ...tail] = members;
|
|
182
|
+
const counted = { ...acc, keywords: acc.keywords + 1 };
|
|
183
|
+
if (validate && !allowedKey(key))
|
|
184
|
+
return err("schema_keyword_not_allowed");
|
|
185
|
+
if (validate) {
|
|
186
|
+
const judged = validateValue(key, value);
|
|
187
|
+
if (!judged.ok)
|
|
188
|
+
return judged;
|
|
189
|
+
}
|
|
190
|
+
const descended = descend(key, value, path, counted, pointers, edges, validate);
|
|
191
|
+
if (!descended.ok)
|
|
192
|
+
return descended;
|
|
193
|
+
if (validate && meterSum(descended.v.acc) > CEILING)
|
|
194
|
+
return err("schema_complexity_exceeded");
|
|
195
|
+
return walkMembers(tail, path, descended.v.acc, descended.v.pointers, descended.v.edges, validate);
|
|
196
|
+
}
|
|
197
|
+
function allowedKey(key) {
|
|
198
|
+
return KEYWORD_ORDER.includes(key) || key === "$defs";
|
|
199
|
+
}
|
|
200
|
+
function schemaShape(value) {
|
|
201
|
+
return value.t === "obj" || value.t === "bool";
|
|
202
|
+
}
|
|
203
|
+
function validateValue(key, value) {
|
|
204
|
+
const invalid = err("schema_keyword_value_invalid");
|
|
205
|
+
switch (key) {
|
|
206
|
+
case "type":
|
|
207
|
+
if (value.t === "str")
|
|
208
|
+
return TYPE_NAMES.has(value.v) ? ok(undefined) : invalid;
|
|
209
|
+
if (value.t === "arr") {
|
|
210
|
+
if (!value.v.every((x) => x.t === "str"))
|
|
211
|
+
return invalid;
|
|
212
|
+
const names = value.v.map((x) => x.v);
|
|
213
|
+
if (names.length === 0)
|
|
214
|
+
return invalid;
|
|
215
|
+
if (new Set(names).size !== names.length)
|
|
216
|
+
return invalid;
|
|
217
|
+
return names.every((n) => TYPE_NAMES.has(n)) ? ok(undefined) : invalid;
|
|
218
|
+
}
|
|
219
|
+
return invalid;
|
|
220
|
+
case "enum":
|
|
221
|
+
return value.t === "arr" ? ok(undefined) : invalid;
|
|
222
|
+
case "const":
|
|
223
|
+
return ok(undefined);
|
|
224
|
+
case "minimum":
|
|
225
|
+
case "maximum":
|
|
226
|
+
return value.t === "int" || value.t === "float" ? ok(undefined) : invalid;
|
|
227
|
+
case "minLength":
|
|
228
|
+
case "maxLength":
|
|
229
|
+
case "minItems":
|
|
230
|
+
case "maxItems":
|
|
231
|
+
if (value.t === "int")
|
|
232
|
+
return value.v >= 0 ? ok(undefined) : invalid;
|
|
233
|
+
if (value.t === "float")
|
|
234
|
+
return value.v >= 0 && value.v === Math.trunc(value.v) ? ok(undefined) : invalid;
|
|
235
|
+
return invalid;
|
|
236
|
+
case "required": {
|
|
237
|
+
if (value.t !== "arr")
|
|
238
|
+
return invalid;
|
|
239
|
+
if (!value.v.every((x) => x.t === "str"))
|
|
240
|
+
return invalid;
|
|
241
|
+
const names = value.v.map((x) => x.v);
|
|
242
|
+
return new Set(names).size === names.length ? ok(undefined) : invalid;
|
|
243
|
+
}
|
|
244
|
+
case "properties":
|
|
245
|
+
case "$defs":
|
|
246
|
+
return value.t === "obj" ? ok(undefined) : invalid;
|
|
247
|
+
case "items":
|
|
248
|
+
case "additionalProperties":
|
|
249
|
+
return schemaShape(value) ? ok(undefined) : invalid;
|
|
250
|
+
case "oneOf":
|
|
251
|
+
if (value.t !== "arr" || value.v.length === 0)
|
|
252
|
+
return invalid;
|
|
253
|
+
return value.v.every(schemaShape) ? ok(undefined) : invalid;
|
|
254
|
+
case "$ref":
|
|
255
|
+
return value.t === "str" ? ok(undefined) : invalid;
|
|
256
|
+
default:
|
|
257
|
+
return invalid;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
function descend(key, value, path, acc, pointers, edges, validate) {
|
|
261
|
+
if (key === "$ref" && value.t === "str") {
|
|
262
|
+
const tokens = refTokens(value.v);
|
|
263
|
+
if (tokens === null)
|
|
264
|
+
return err("schema_keyword_not_allowed");
|
|
265
|
+
return ok({ acc, pointers, edges: [[pathKey(path), tokens], ...edges] });
|
|
266
|
+
}
|
|
267
|
+
if (key === "oneOf" && value.t === "arr") {
|
|
268
|
+
// the branch array itself is a document node — count it
|
|
269
|
+
const bumped = {
|
|
270
|
+
...acc,
|
|
271
|
+
branches: acc.branches + value.v.length,
|
|
272
|
+
nodes: acc.nodes + 1,
|
|
273
|
+
depth: Math.max(acc.depth, path.length + 2),
|
|
274
|
+
};
|
|
275
|
+
return walkIndexed(value.v, 0, key, path, bumped, pointers, edges, validate);
|
|
276
|
+
}
|
|
277
|
+
if (NAME_MAP_VALUED.includes(key) && value.t === "obj") {
|
|
278
|
+
// the name map itself is a document node — count it, then walk its
|
|
279
|
+
// schema-position values
|
|
280
|
+
const bumped = {
|
|
281
|
+
...acc,
|
|
282
|
+
nodes: acc.nodes + 1,
|
|
283
|
+
depth: Math.max(acc.depth, path.length + 2),
|
|
284
|
+
};
|
|
285
|
+
return walkNamed(value.v, key, path, bumped, pointers, edges, validate);
|
|
286
|
+
}
|
|
287
|
+
if (SCHEMA_VALUED.includes(key)) {
|
|
288
|
+
if (schemaShape(value)) {
|
|
289
|
+
return walk(value, [...path, key], acc, pointers, edges, validate);
|
|
290
|
+
}
|
|
291
|
+
return ok({ acc: meterData(value, path.length + 2, acc), pointers, edges });
|
|
292
|
+
}
|
|
293
|
+
// Everything else (type/enum/const/number/required values) is data:
|
|
294
|
+
// nodes only, no keywords inside.
|
|
295
|
+
return ok({ acc: meterData(value, path.length + 2, acc), pointers, edges });
|
|
296
|
+
}
|
|
297
|
+
function walkIndexed(branches, index, key, path, acc, pointers, edges, validate) {
|
|
298
|
+
if (index >= branches.length)
|
|
299
|
+
return ok({ acc, pointers, edges });
|
|
300
|
+
const sub = branches[index];
|
|
301
|
+
if (schemaShape(sub)) {
|
|
302
|
+
const walked = walk(sub, [...path, key, String(index)], acc, pointers, edges, validate);
|
|
303
|
+
if (!walked.ok)
|
|
304
|
+
return walked;
|
|
305
|
+
return walkIndexed(branches, index + 1, key, path, walked.v.acc, walked.v.pointers, walked.v.edges, validate);
|
|
306
|
+
}
|
|
307
|
+
if (validate)
|
|
308
|
+
return err("schema_keyword_value_invalid");
|
|
309
|
+
return walkIndexed(branches, index + 1, key, path, meterData(sub, path.length + 2, acc), pointers, edges, validate);
|
|
310
|
+
}
|
|
311
|
+
function walkNamed(subs, key, path, acc, pointers, edges, validate) {
|
|
312
|
+
if (subs.length === 0)
|
|
313
|
+
return ok({ acc, pointers, edges });
|
|
314
|
+
const [[name, sub], ...tail] = subs;
|
|
315
|
+
if (schemaShape(sub)) {
|
|
316
|
+
const walked = walk(sub, [...path, key, name], acc, pointers, edges, validate);
|
|
317
|
+
if (!walked.ok)
|
|
318
|
+
return walked;
|
|
319
|
+
return walkNamed(tail, key, path, walked.v.acc, walked.v.pointers, walked.v.edges, validate);
|
|
320
|
+
}
|
|
321
|
+
if (validate)
|
|
322
|
+
return err("schema_keyword_value_invalid");
|
|
323
|
+
return walkNamed(tail, key, path, meterData(sub, path.length + 2, acc), pointers, edges, validate);
|
|
324
|
+
}
|
|
325
|
+
function meterData(value, depth, acc) {
|
|
326
|
+
const bumped = { ...acc, nodes: acc.nodes + 1, depth: Math.max(acc.depth, depth) };
|
|
327
|
+
if (value.t === "arr") {
|
|
328
|
+
return value.v.reduce((inner, item) => meterData(item, depth + 1, inner), bumped);
|
|
329
|
+
}
|
|
330
|
+
if (value.t === "obj") {
|
|
331
|
+
return value.v.reduce((inner, [, member]) => meterData(member, depth + 1, inner), bumped);
|
|
332
|
+
}
|
|
333
|
+
return bumped;
|
|
334
|
+
}
|
|
335
|
+
// ---- $ref pointers (RFC 6901) ------------------------------------------------------
|
|
336
|
+
// null = :not_allowed (non-pointer forms); otherwise the token list.
|
|
337
|
+
function refTokens(ref) {
|
|
338
|
+
if (ref === "#")
|
|
339
|
+
return [];
|
|
340
|
+
if (!ref.startsWith("#"))
|
|
341
|
+
return null;
|
|
342
|
+
const rest = ref.slice(1);
|
|
343
|
+
if (!rest.startsWith("/"))
|
|
344
|
+
return null;
|
|
345
|
+
const tokens = rest.split("/").slice(1);
|
|
346
|
+
const decoded = [];
|
|
347
|
+
for (const token of tokens) {
|
|
348
|
+
const unescaped = unescapeToken(token);
|
|
349
|
+
if (unescaped === null)
|
|
350
|
+
return null;
|
|
351
|
+
decoded.push(unescaped);
|
|
352
|
+
}
|
|
353
|
+
return decoded;
|
|
354
|
+
}
|
|
355
|
+
// ~1 → "/" before ~0 → "~" in ONE left-to-right pass; "~" not followed by 0
|
|
356
|
+
// or 1 is not an RFC 6901 escape.
|
|
357
|
+
function unescapeToken(token) {
|
|
358
|
+
let out = "";
|
|
359
|
+
for (let i = 0; i < token.length; i++) {
|
|
360
|
+
const c = token[i];
|
|
361
|
+
if (c === "~") {
|
|
362
|
+
const next = token[i + 1];
|
|
363
|
+
if (next === "0") {
|
|
364
|
+
out += "~";
|
|
365
|
+
i++;
|
|
366
|
+
}
|
|
367
|
+
else if (next === "1") {
|
|
368
|
+
out += "/";
|
|
369
|
+
i++;
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
return null;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
out += c;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return out;
|
|
380
|
+
}
|
|
381
|
+
// ---- application-edge cycle check ---------------------------------------------------
|
|
382
|
+
function cycleFree(pointers) {
|
|
383
|
+
return dfs([], new Set(), new Set(), pointers);
|
|
384
|
+
}
|
|
385
|
+
function dfs(path, visiting, visited, pointers) {
|
|
386
|
+
const key = pathKey(path);
|
|
387
|
+
if (visiting.has(key))
|
|
388
|
+
return false;
|
|
389
|
+
if (visited.has(key))
|
|
390
|
+
return true;
|
|
391
|
+
const nextVisiting = new Set(visiting);
|
|
392
|
+
nextVisiting.add(key);
|
|
393
|
+
for (const child of children(path, pointers)) {
|
|
394
|
+
if (!dfs(child, nextVisiting, visited, pointers))
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
visited.add(key);
|
|
398
|
+
return true;
|
|
399
|
+
}
|
|
400
|
+
function children(path, pointers) {
|
|
401
|
+
const term = pointers.get(pathKey(path));
|
|
402
|
+
if (term === undefined || term.t !== "obj")
|
|
403
|
+
return [];
|
|
404
|
+
const out = [];
|
|
405
|
+
for (const [name, value] of term.v) {
|
|
406
|
+
if (name === "properties" && value.t === "obj") {
|
|
407
|
+
for (const [propertyName] of value.v)
|
|
408
|
+
out.push([...path, "properties", propertyName]);
|
|
409
|
+
}
|
|
410
|
+
else if (name === "items") {
|
|
411
|
+
out.push([...path, "items"]);
|
|
412
|
+
}
|
|
413
|
+
else if (name === "additionalProperties") {
|
|
414
|
+
out.push([...path, "additionalProperties"]);
|
|
415
|
+
}
|
|
416
|
+
else if (name === "oneOf" && value.t === "arr") {
|
|
417
|
+
value.v.forEach((_, i) => out.push([...path, "oneOf", String(i)]));
|
|
418
|
+
}
|
|
419
|
+
else if (name === "$ref" && value.t === "str") {
|
|
420
|
+
const tokens = refTokens(value.v);
|
|
421
|
+
if (tokens !== null)
|
|
422
|
+
out.push(tokens);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return out;
|
|
426
|
+
}
|
|
427
|
+
function evaluate(schema, instance, loc, memo, pointers) {
|
|
428
|
+
if (schema.t === "bool") {
|
|
429
|
+
return schema.v ? ok(undefined) : err("invalid_type");
|
|
430
|
+
}
|
|
431
|
+
const locKey = JSON.stringify(loc);
|
|
432
|
+
const byLoc = memo.get(schema);
|
|
433
|
+
if (byLoc !== undefined) {
|
|
434
|
+
const cached = byLoc.get(locKey);
|
|
435
|
+
if (cached !== undefined)
|
|
436
|
+
return cached;
|
|
437
|
+
}
|
|
438
|
+
// parse()'s shape gate admits only obj/bool schemas; bool returned above.
|
|
439
|
+
const result = evalKeywords(schema, instance, loc, memo, pointers);
|
|
440
|
+
if (!byLoc)
|
|
441
|
+
memo.set(schema, new Map());
|
|
442
|
+
memo.get(schema).set(locKey, result);
|
|
443
|
+
return result;
|
|
444
|
+
}
|
|
445
|
+
function evalKeywords(schema, instance, loc, memo, pointers) {
|
|
446
|
+
for (const keyword of KEYWORD_ORDER) {
|
|
447
|
+
const value = fetch(schema, keyword);
|
|
448
|
+
if (value === null)
|
|
449
|
+
continue;
|
|
450
|
+
const result = evalKeyword(keyword, schema, value, instance, loc, memo, pointers);
|
|
451
|
+
if (!result.ok)
|
|
452
|
+
return result;
|
|
453
|
+
}
|
|
454
|
+
return ok(undefined);
|
|
455
|
+
}
|
|
456
|
+
function fetch(schema, key) {
|
|
457
|
+
const found = schema.v.find(([name]) => name === key);
|
|
458
|
+
return found ? found[1] : null;
|
|
459
|
+
}
|
|
460
|
+
function evalKeyword(keyword, schema, value, instance, loc, memo, pointers) {
|
|
461
|
+
switch (keyword) {
|
|
462
|
+
case "$ref": {
|
|
463
|
+
const tokens = refTokens(value.v);
|
|
464
|
+
const target = pointers.get(pathKey(tokens));
|
|
465
|
+
return evaluate(target, instance, loc, memo, pointers);
|
|
466
|
+
}
|
|
467
|
+
case "type": {
|
|
468
|
+
const names = typeNames(value);
|
|
469
|
+
return names.some((n) => typeMatches(n, instance)) ? ok(undefined) : err("invalid_type");
|
|
470
|
+
}
|
|
471
|
+
case "enum": {
|
|
472
|
+
const elements = value.v;
|
|
473
|
+
return elements.some((e) => equal(e, instance)) ? ok(undefined) : err("invalid_constraint");
|
|
474
|
+
}
|
|
475
|
+
case "const":
|
|
476
|
+
return equal(value, instance) ? ok(undefined) : err("invalid_constraint");
|
|
477
|
+
case "minimum":
|
|
478
|
+
case "maximum": {
|
|
479
|
+
if (instance.t !== "int" && instance.t !== "float")
|
|
480
|
+
return ok(undefined);
|
|
481
|
+
// A numeric limit per keyword validation; junk compares false either way.
|
|
482
|
+
const limit = value.v;
|
|
483
|
+
const pass = keyword === "minimum" ? instance.v >= limit : instance.v <= limit;
|
|
484
|
+
return pass ? ok(undefined) : err("invalid_constraint");
|
|
485
|
+
}
|
|
486
|
+
case "minLength":
|
|
487
|
+
case "maxLength": {
|
|
488
|
+
if (instance.t !== "str")
|
|
489
|
+
return ok(undefined);
|
|
490
|
+
const limit = zeroFraction(value);
|
|
491
|
+
const length = countCodepoints(instance.v);
|
|
492
|
+
const pass = keyword === "minLength" ? length >= limit : length <= limit;
|
|
493
|
+
return pass ? ok(undefined) : err("invalid_constraint");
|
|
494
|
+
}
|
|
495
|
+
case "minItems":
|
|
496
|
+
case "maxItems": {
|
|
497
|
+
if (instance.t !== "arr")
|
|
498
|
+
return ok(undefined);
|
|
499
|
+
const limit = zeroFraction(value);
|
|
500
|
+
const pass = keyword === "minItems" ? instance.v.length >= limit : instance.v.length <= limit;
|
|
501
|
+
return pass ? ok(undefined) : err("invalid_cardinality");
|
|
502
|
+
}
|
|
503
|
+
case "required": {
|
|
504
|
+
if (instance.t !== "obj")
|
|
505
|
+
return ok(undefined);
|
|
506
|
+
const names = value.v.map((x) => x.v);
|
|
507
|
+
const all = names.every((n) => instance.v.some(([name]) => name === n));
|
|
508
|
+
return all ? ok(undefined) : err("invalid_cardinality");
|
|
509
|
+
}
|
|
510
|
+
case "properties": {
|
|
511
|
+
if (instance.t !== "obj")
|
|
512
|
+
return ok(undefined);
|
|
513
|
+
return evalPresentProperties(value.v, instance.v, loc, memo, pointers);
|
|
514
|
+
}
|
|
515
|
+
case "items": {
|
|
516
|
+
if (instance.t !== "arr")
|
|
517
|
+
return ok(undefined);
|
|
518
|
+
return evalItems(instance.v, 0, value, loc, memo, pointers);
|
|
519
|
+
}
|
|
520
|
+
case "additionalProperties": {
|
|
521
|
+
if (instance.t !== "obj")
|
|
522
|
+
return ok(undefined);
|
|
523
|
+
return evalClosure(schema, value, instance.v, loc, memo, pointers);
|
|
524
|
+
}
|
|
525
|
+
case "oneOf": {
|
|
526
|
+
const branches = value.v;
|
|
527
|
+
const [passes] = countPasses(branches, instance, loc, memo, pointers, 0);
|
|
528
|
+
return passes === 1 ? ok(undefined) : err("invalid_cardinality");
|
|
529
|
+
}
|
|
530
|
+
default:
|
|
531
|
+
return ok(undefined);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
// Adjacency (core §10.3.2.3): additionalProperties sees only the sibling
|
|
535
|
+
// `properties` member of the SAME schema object; false is the closure itself
|
|
536
|
+
// and reports the closure class.
|
|
537
|
+
function evalClosure(schema, sub, members, loc, memo, pointers) {
|
|
538
|
+
const adjacentMember = fetch(schema, "properties");
|
|
539
|
+
const adjacent = adjacentMember !== null && adjacentMember.t === "obj"
|
|
540
|
+
? adjacentMember.v.map(([name]) => name)
|
|
541
|
+
: [];
|
|
542
|
+
const additional = members.filter(([name]) => !adjacent.includes(name));
|
|
543
|
+
if (sub.t === "bool") {
|
|
544
|
+
if (sub.v)
|
|
545
|
+
return ok(undefined);
|
|
546
|
+
return additional.length === 0 ? ok(undefined) : err("invalid_cardinality");
|
|
547
|
+
}
|
|
548
|
+
return evalAdditional(additional, sub, loc, memo, pointers);
|
|
549
|
+
}
|
|
550
|
+
function evalPresentProperties(subs, members, loc, memo, pointers) {
|
|
551
|
+
for (const [name, sub] of subs) {
|
|
552
|
+
const found = members.find(([memberName]) => memberName === name);
|
|
553
|
+
if (found === undefined)
|
|
554
|
+
continue;
|
|
555
|
+
const result = evaluate(sub, found[1], [name, ...loc], memo, pointers);
|
|
556
|
+
if (!result.ok)
|
|
557
|
+
return result;
|
|
558
|
+
}
|
|
559
|
+
return ok(undefined);
|
|
560
|
+
}
|
|
561
|
+
function evalItems(items, index, sub, loc, memo, pointers) {
|
|
562
|
+
if (index >= items.length)
|
|
563
|
+
return ok(undefined);
|
|
564
|
+
// Schema first, instance second — the historical swap here evaluated each
|
|
565
|
+
// item AS the schema (object-shaped items passed vacuously, scalar items
|
|
566
|
+
// crashed); the tier schemas are the first corpus-bearing items user.
|
|
567
|
+
const result = evaluate(sub, items[index], [String(index), ...loc], memo, pointers);
|
|
568
|
+
if (!result.ok)
|
|
569
|
+
return result;
|
|
570
|
+
return evalItems(items, index + 1, sub, loc, memo, pointers);
|
|
571
|
+
}
|
|
572
|
+
function evalAdditional(additional, sub, loc, memo, pointers) {
|
|
573
|
+
for (const [name, value] of additional) {
|
|
574
|
+
const result = evaluate(sub, value, [name, ...loc], memo, pointers);
|
|
575
|
+
if (!result.ok)
|
|
576
|
+
return result;
|
|
577
|
+
}
|
|
578
|
+
return ok(undefined);
|
|
579
|
+
}
|
|
580
|
+
function countPasses(branches, instance, loc, memo, pointers, count) {
|
|
581
|
+
if (branches.length === 0 || count >= 2)
|
|
582
|
+
return [count, memo];
|
|
583
|
+
const result = evaluate(branches[0], instance, loc, memo, pointers);
|
|
584
|
+
const nextCount = result.ok ? count + 1 : count;
|
|
585
|
+
return countPasses(branches.slice(1), instance, loc, memo, pointers, nextCount);
|
|
586
|
+
}
|
|
587
|
+
function typeNames(value) {
|
|
588
|
+
if (value.t === "str")
|
|
589
|
+
return [value.v];
|
|
590
|
+
// A "type" keyword value is str or arr of str per keyword validation.
|
|
591
|
+
return value.v.map((x) => x.v);
|
|
592
|
+
}
|
|
593
|
+
function typeMatches(name, instance) {
|
|
594
|
+
switch (name) {
|
|
595
|
+
case "null":
|
|
596
|
+
return instance.t === "null";
|
|
597
|
+
case "boolean":
|
|
598
|
+
return instance.t === "bool";
|
|
599
|
+
case "object":
|
|
600
|
+
return instance.t === "obj";
|
|
601
|
+
case "array":
|
|
602
|
+
return instance.t === "arr";
|
|
603
|
+
case "string":
|
|
604
|
+
return instance.t === "str";
|
|
605
|
+
case "number":
|
|
606
|
+
return instance.t === "int" || instance.t === "float";
|
|
607
|
+
case "integer":
|
|
608
|
+
return instance.t === "int" || (instance.t === "float" && instance.v === Math.trunc(instance.v));
|
|
609
|
+
default:
|
|
610
|
+
return false;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
// RFC 8259 characters = codepoints (never graphemes, never UTF-16 units).
|
|
614
|
+
function countCodepoints(s) {
|
|
615
|
+
let count = 0;
|
|
616
|
+
for (const _ of s)
|
|
617
|
+
count++;
|
|
618
|
+
return count;
|
|
619
|
+
}
|
|
620
|
+
function zeroFraction(value) {
|
|
621
|
+
// Length/limit keyword values are numeric per keyword validation.
|
|
622
|
+
return value.t === "int" ? value.v : Math.trunc(value.v);
|
|
623
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|