@sorbetai/schema 0.1.0-alpha.17 → 0.1.0-alpha.19
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/dist/compiler.js +862 -693
- package/dist/compiler.js.map +1 -1
- package/package.json +2 -2
package/dist/compiler.js
CHANGED
|
@@ -1,13 +1,475 @@
|
|
|
1
|
-
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.
|
|
1
|
+
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.6.0/node_modules/@earendil-works/pi-schema/dist/checks.js
|
|
2
|
+
const ALWAYS = () => true;
|
|
3
|
+
const NEVER = () => false;
|
|
4
|
+
const IS_STRING = (v) => typeof v === "string";
|
|
5
|
+
const IS_NUMBER = (v) => Number.isFinite(v);
|
|
6
|
+
const IS_INTEGER = (v) => Number.isInteger(v);
|
|
7
|
+
const IS_BOOLEAN = (v) => typeof v === "boolean";
|
|
8
|
+
const IS_NULL = (v) => v === null;
|
|
9
|
+
const IS_ARRAY = (v) => Array.isArray(v);
|
|
10
|
+
const IS_BYTES = (v) => v instanceof Uint8Array;
|
|
11
|
+
const IS_BIGINT = (v) => typeof v === "bigint";
|
|
12
|
+
const IS_UNDEFINED = (v) => v === void 0;
|
|
13
|
+
const IS_OBJECT = (v) => isObject(v);
|
|
14
|
+
/** Whether `v` is a JSON object: not null, an array or bytes. */
|
|
15
|
+
function isObject(v) {
|
|
16
|
+
return typeof v === "object" && v !== null && !Array.isArray(v) && !(v instanceof Uint8Array);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The check of a `type` name. `bytes` (a Uint8Array), `bigint` (TypeBox's `Type.BigInt`) and `undefined` (TypeBox's
|
|
20
|
+
* `Type.Undefined`) extend JSON Schema for values that never pass through JSON. A `number` is finite, as in TypeBox:
|
|
21
|
+
* JSON has no NaN or Infinity, so only a value built in code can be one.
|
|
22
|
+
*/
|
|
23
|
+
const TYPE_CHECKS = new Map([
|
|
24
|
+
["string", IS_STRING],
|
|
25
|
+
["number", IS_NUMBER],
|
|
26
|
+
["integer", IS_INTEGER],
|
|
27
|
+
["boolean", IS_BOOLEAN],
|
|
28
|
+
["null", IS_NULL],
|
|
29
|
+
["array", IS_ARRAY],
|
|
30
|
+
["object", IS_OBJECT],
|
|
31
|
+
["bytes", IS_BYTES],
|
|
32
|
+
["bigint", IS_BIGINT],
|
|
33
|
+
["undefined", IS_UNDEFINED]
|
|
34
|
+
]);
|
|
35
|
+
const leaves = new WeakMap([
|
|
36
|
+
[IS_STRING, [1, void 0]],
|
|
37
|
+
[IS_NUMBER, [2, void 0]],
|
|
38
|
+
[IS_INTEGER, [3, void 0]],
|
|
39
|
+
[IS_BOOLEAN, [4, void 0]],
|
|
40
|
+
[IS_NULL, [5, void 0]],
|
|
41
|
+
[ALWAYS, [6, void 0]]
|
|
42
|
+
]);
|
|
43
|
+
/** The leaf form of `check`, undefined when it is not a leaf. */
|
|
44
|
+
function leafOf(check) {
|
|
45
|
+
return leaves.get(check);
|
|
46
|
+
}
|
|
47
|
+
function leaf(code, arg, check) {
|
|
48
|
+
leaves.set(check, [code, arg]);
|
|
49
|
+
return check;
|
|
50
|
+
}
|
|
51
|
+
/** Bounds of a number or a string length leaf. */
|
|
52
|
+
var Bounds = class {
|
|
53
|
+
min;
|
|
54
|
+
max;
|
|
55
|
+
xmin;
|
|
56
|
+
xmax;
|
|
57
|
+
constructor(min, max, xmin, xmax) {
|
|
58
|
+
this.min = min;
|
|
59
|
+
this.max = max;
|
|
60
|
+
this.xmin = xmin;
|
|
61
|
+
this.xmax = xmax;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
function leafOk(code, x, arg) {
|
|
65
|
+
switch (code) {
|
|
66
|
+
case 1: return typeof x === "string";
|
|
67
|
+
case 2: return Number.isFinite(x);
|
|
68
|
+
case 3: return Number.isInteger(x);
|
|
69
|
+
case 4: return typeof x === "boolean";
|
|
70
|
+
case 5: return x === null;
|
|
71
|
+
case 7: return x === arg;
|
|
72
|
+
case 8: {
|
|
73
|
+
const list = arg;
|
|
74
|
+
for (let i = 0; i < list.length; i++) if (list[i] === x) return true;
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
case 9: return (typeBits(x) & arg) !== 0;
|
|
78
|
+
case 10: return Number.isInteger(x) && inBounds(x, arg);
|
|
79
|
+
case 11: return Number.isFinite(x) && inBounds(x, arg);
|
|
80
|
+
case 12: return typeof x === "string" && lengthOk(x, arg.min, arg.max);
|
|
81
|
+
case 13: return typeof x === "string" && arg.test(x);
|
|
82
|
+
default: return true;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function inBounds(x, b) {
|
|
86
|
+
return x >= b.min && x <= b.max && x > b.xmin && x < b.xmax;
|
|
87
|
+
}
|
|
88
|
+
/** Whether a combined object or array check also checks the type: not at all, strictly, or passing other types. */
|
|
89
|
+
const GUARD_NONE = 0;
|
|
90
|
+
const GUARD_STRICT = 1;
|
|
91
|
+
const GUARD_LOOSE = 2;
|
|
92
|
+
/** Flags of a declared property in an object check's entries: its leaf code and two bits. */
|
|
93
|
+
const CODE = 15;
|
|
94
|
+
const REQUIRED = 16;
|
|
95
|
+
const INHERITED = 32;
|
|
96
|
+
/** All of `parts`; two and three parts get closures without a loop. */
|
|
97
|
+
function and(parts) {
|
|
98
|
+
switch (parts.length) {
|
|
99
|
+
case 0: return ALWAYS;
|
|
100
|
+
case 1: return parts[0];
|
|
101
|
+
case 2: {
|
|
102
|
+
const [a, b] = parts;
|
|
103
|
+
return (v) => a(v) && b(v);
|
|
104
|
+
}
|
|
105
|
+
case 3: {
|
|
106
|
+
const [a, b, c] = parts;
|
|
107
|
+
return (v) => a(v) && b(v) && c(v);
|
|
108
|
+
}
|
|
109
|
+
default: return (v) => {
|
|
110
|
+
for (let i = 0; i < parts.length; i++) if (!parts[i](v)) return false;
|
|
111
|
+
return true;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/** A check whose implementation is set later: the indirection a recursive reference goes through. */
|
|
116
|
+
function indirect(slot) {
|
|
117
|
+
return (v) => slot.check(v);
|
|
118
|
+
}
|
|
119
|
+
/** `check` with `uri` pushed onto the dynamic scope while it runs (only for schemas with `$dynamicRef`). */
|
|
120
|
+
function scopedCheck(scope, uri, check) {
|
|
121
|
+
return (v) => {
|
|
122
|
+
scope.push(uri);
|
|
123
|
+
const ok = check(v);
|
|
124
|
+
scope.pop();
|
|
125
|
+
return ok;
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/** Any of the named types. */
|
|
129
|
+
function typeCheck(types) {
|
|
130
|
+
if (types.length === 1) return (typeof types[0] === "string" ? TYPE_CHECKS.get(types[0]) : void 0) ?? NEVER;
|
|
131
|
+
return maskCheck(typeMask(types));
|
|
132
|
+
}
|
|
133
|
+
const TYPE_BITS = new Map([
|
|
134
|
+
["string", 1],
|
|
135
|
+
["number", 2],
|
|
136
|
+
["integer", 4],
|
|
137
|
+
["boolean", 8],
|
|
138
|
+
["null", 16],
|
|
139
|
+
["array", 32],
|
|
140
|
+
["object", 64],
|
|
141
|
+
["bytes", 128],
|
|
142
|
+
["bigint", 256],
|
|
143
|
+
["undefined", 512]
|
|
144
|
+
]);
|
|
145
|
+
/** The bits of the named types; unknown names add none. */
|
|
146
|
+
function typeMask(types) {
|
|
147
|
+
let mask = 0;
|
|
148
|
+
for (const t of types) if (typeof t === "string") mask |= TYPE_BITS.get(t) ?? 0;
|
|
149
|
+
return mask;
|
|
150
|
+
}
|
|
151
|
+
/** The type bits of `v`: an integer has the bits of `number` and `integer`; NaN and Infinity have neither. */
|
|
152
|
+
function typeBits(v) {
|
|
153
|
+
switch (typeof v) {
|
|
154
|
+
case "string": return 1;
|
|
155
|
+
case "number": return Number.isInteger(v) ? 6 : Number.isFinite(v) ? 2 : 0;
|
|
156
|
+
case "boolean": return 8;
|
|
157
|
+
case "object": return v === null ? 16 : Array.isArray(v) ? 32 : v instanceof Uint8Array ? 128 : 64;
|
|
158
|
+
case "bigint": return 256;
|
|
159
|
+
case "undefined": return 512;
|
|
160
|
+
default: return 0;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function maskCheck(mask) {
|
|
164
|
+
return leaf(9, mask, (v) => (typeBits(v) & mask) !== 0);
|
|
165
|
+
}
|
|
166
|
+
/** `anyOf` whose bare-type branches have the types of `mask`: those by one type test, the others by their checks. */
|
|
167
|
+
function maskedAnyCheck(mask, checks) {
|
|
168
|
+
if (checks.length === 0) return maskCheck(mask);
|
|
169
|
+
const rest = checks.length === 1 ? checks[0] : anyCheck(checks);
|
|
170
|
+
return (v) => (typeBits(v) & mask) !== 0 || rest(v);
|
|
171
|
+
}
|
|
172
|
+
/** A Uint8Array within the length bounds. */
|
|
173
|
+
function bytesCheck(min, max) {
|
|
174
|
+
return (v) => v instanceof Uint8Array && v.length >= min && v.length <= max;
|
|
175
|
+
}
|
|
176
|
+
/** The number of Unicode code points of `s`: JSON Schema string lengths count code points, not UTF-16 units. */
|
|
177
|
+
function codePoints(s) {
|
|
178
|
+
let n = 0;
|
|
179
|
+
for (let i = 0; i < s.length; i++, n++) {
|
|
180
|
+
const c = s.charCodeAt(i);
|
|
181
|
+
if (c >= 55296 && c <= 56319 && i + 1 < s.length) {
|
|
182
|
+
const next = s.charCodeAt(i + 1);
|
|
183
|
+
if (next >= 56320 && next <= 57343) i++;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return n;
|
|
187
|
+
}
|
|
188
|
+
function lengthOk(s, min, max) {
|
|
189
|
+
const units = s.length;
|
|
190
|
+
if (units < min) return false;
|
|
191
|
+
if (units < 2 * min && codePoints(s) < min) return false;
|
|
192
|
+
return units <= max || codePoints(s) <= max;
|
|
193
|
+
}
|
|
194
|
+
/** The string keywords: length bounds, pattern and format. */
|
|
195
|
+
function stringCheck(strict, min, max, re, format) {
|
|
196
|
+
if (re !== void 0 && min === 0 && max === Number.POSITIVE_INFINITY && format === void 0) return strict ? leaf(13, re, (v) => typeof v === "string" && re.test(v)) : (v) => typeof v !== "string" || re.test(v);
|
|
197
|
+
if (re === void 0 && format === void 0) return strict ? leaf(12, new Bounds(min, max, 0, 0), (v) => typeof v === "string" && lengthOk(v, min, max)) : (v) => typeof v !== "string" || lengthOk(v, min, max);
|
|
198
|
+
return (v) => {
|
|
199
|
+
if (typeof v !== "string") return !strict;
|
|
200
|
+
return lengthOk(v, min, max) && (re === void 0 || re.test(v)) && (format === void 0 || format(v));
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* The number keywords, for numbers and bigints alike (as TypeBox applies them): bounds of either kind compare with
|
|
205
|
+
* values of either kind. `mode`: 0 passes other types, 1 requires a number, 2 an integer, 3 a bigint.
|
|
206
|
+
*/
|
|
207
|
+
function numberCheck(mode, min, max, xmin, xmax, multipleOf) {
|
|
208
|
+
const check = (v) => {
|
|
209
|
+
if (typeof v !== "number" && typeof v !== "bigint") return mode === 0;
|
|
210
|
+
if (mode === 3 ? typeof v !== "bigint" : mode !== 0 && !Number.isFinite(v)) return false;
|
|
211
|
+
if (mode === 2 && !Number.isInteger(v)) return false;
|
|
212
|
+
return v >= min && v <= max && v > xmin && v < xmax && (multipleOf === void 0 || isMultiple(v, multipleOf));
|
|
213
|
+
};
|
|
214
|
+
if (mode === 0 || mode === 3 || multipleOf !== void 0 || !(typeof min === "number" && typeof max === "number" && typeof xmin === "number" && typeof xmax === "number")) return check;
|
|
215
|
+
return leaf(mode === 2 ? 10 : 11, new Bounds(min, max, xmin, xmax), check);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Whether `v` is a multiple of `d`, exactly for decimals: 0.0075 / 0.0001 is 74.99999999999999 in floating point. With
|
|
219
|
+
* a bigint on either side, exact integer arithmetic; a bigint is no multiple of 0 or of a fraction.
|
|
220
|
+
*
|
|
221
|
+
* Deliberately unlike TypeBox, which throws there (`BigInt(0.5)`, division by zero): a check answers for every value
|
|
222
|
+
* and never throws, and no real schema pairs a bigint with a fractional or zero `multipleOf`.
|
|
223
|
+
*/
|
|
224
|
+
function isMultiple(v, d) {
|
|
225
|
+
if (typeof v === "bigint" || typeof d === "bigint") {
|
|
226
|
+
if (typeof v === "number" && !Number.isInteger(v) || typeof d === "number" && !Number.isInteger(d)) return false;
|
|
227
|
+
const divisor = BigInt(d);
|
|
228
|
+
return divisor !== 0n && BigInt(v) % divisor === 0n;
|
|
229
|
+
}
|
|
230
|
+
const q = v / d;
|
|
231
|
+
if (Number.isInteger(q)) return true;
|
|
232
|
+
if (!Number.isFinite(q)) return v % d === 0;
|
|
233
|
+
const [vc, ve] = decimal(v);
|
|
234
|
+
const [dc, de] = decimal(d);
|
|
235
|
+
return ve >= de ? vc * 10n ** BigInt(ve - de) % dc === 0n : vc % (dc * 10n ** BigInt(de - ve)) === 0n;
|
|
236
|
+
}
|
|
237
|
+
function decimal(x) {
|
|
238
|
+
const [mantissa, exponent = "0"] = Math.abs(x).toString().split("e");
|
|
239
|
+
const point = mantissa.indexOf(".");
|
|
240
|
+
const fraction = point < 0 ? 0 : mantissa.length - point - 1;
|
|
241
|
+
return [BigInt(mantissa.replace(".", "")), Number(exponent) - fraction];
|
|
242
|
+
}
|
|
243
|
+
/** `inner` for objects; other values pass unless `strict`. */
|
|
244
|
+
function guardObject(inner, strict) {
|
|
245
|
+
return strict ? (v) => isObject(v) && inner(v) : (v) => !isObject(v) || inner(v);
|
|
246
|
+
}
|
|
247
|
+
/** `inner` for arrays; other values pass unless `strict`. */
|
|
248
|
+
function guardArray(inner, strict) {
|
|
249
|
+
return strict ? (v) => Array.isArray(v) && inner(v) : (v) => !Array.isArray(v) || inner(v);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* An object with declared properties: `entries` is `[key, check, flags]` per property. The value's own keys are
|
|
253
|
+
* walked with for...in (whose `o[k]` V8 reads from the enum cache, unlike a load by a name from a list), each found
|
|
254
|
+
* among the declared names; `closed` rejects any other key. A property whose value is `undefined` counts as absent.
|
|
255
|
+
*/
|
|
256
|
+
function declaredObject(entries$1, requiredCount, closed, guard) {
|
|
257
|
+
const n = entries$1.length;
|
|
258
|
+
return (v) => {
|
|
259
|
+
if (guard !== GUARD_NONE && !isObject(v)) return guard === GUARD_LOOSE;
|
|
260
|
+
const o = v;
|
|
261
|
+
let required = 0;
|
|
262
|
+
let next = 0;
|
|
263
|
+
for (const k in o) {
|
|
264
|
+
let i = next;
|
|
265
|
+
if (entries$1[i] !== k) {
|
|
266
|
+
i = 0;
|
|
267
|
+
while (i < n && entries$1[i] !== k) i += 3;
|
|
268
|
+
if (i === n) {
|
|
269
|
+
if (closed) return false;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
next = i + 3;
|
|
274
|
+
const x = o[k];
|
|
275
|
+
const f = entries$1[i + 2];
|
|
276
|
+
if (x === void 0 && !(f & REQUIRED)) continue;
|
|
277
|
+
const c = f & CODE;
|
|
278
|
+
if (c !== 0 ? !leafOk(c, x, entries$1[i + 1]) : !entries$1[i + 1](x)) return false;
|
|
279
|
+
if (f & REQUIRED) required++;
|
|
280
|
+
}
|
|
281
|
+
return required === requiredCount;
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
/** An object with declared properties, required keys not declared, pattern properties and additional properties. */
|
|
285
|
+
function openObject(entries$1, keys, extraRequired, patterns, additional, guard) {
|
|
286
|
+
const known = patterns.length > 0 || additional !== void 0 ? new Set(keys) : void 0;
|
|
287
|
+
return (v) => {
|
|
288
|
+
if (guard !== GUARD_NONE && !isObject(v)) return guard === GUARD_LOOSE;
|
|
289
|
+
const o = v;
|
|
290
|
+
for (let i = 0; i < entries$1.length; i += 3) {
|
|
291
|
+
const k = entries$1[i];
|
|
292
|
+
const x = o[k];
|
|
293
|
+
const f = entries$1[i + 2];
|
|
294
|
+
if (x === void 0 && !(f & REQUIRED && Object.hasOwn(o, k)) || f & INHERITED && !Object.hasOwn(o, k)) {
|
|
295
|
+
if (f & REQUIRED) return false;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
const c = f & CODE;
|
|
299
|
+
if (c !== 0 ? !leafOk(c, x, entries$1[i + 1]) : !entries$1[i + 1](x)) return false;
|
|
300
|
+
}
|
|
301
|
+
for (let i = 0; i < extraRequired.length; i++) if (!Object.hasOwn(o, extraRequired[i])) return false;
|
|
302
|
+
if (known === void 0) return true;
|
|
303
|
+
for (const k of Object.keys(o)) {
|
|
304
|
+
let matched = known.has(k);
|
|
305
|
+
for (let i = 0; i < patterns.length; i++) if (patterns[i][0].test(k)) {
|
|
306
|
+
matched = true;
|
|
307
|
+
if (!patterns[i][1](o[k])) return false;
|
|
308
|
+
}
|
|
309
|
+
if (!matched && additional !== void 0 && !additional(o[k])) return false;
|
|
310
|
+
}
|
|
311
|
+
return true;
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
/** Every property name passes `check` (`v` is an object). */
|
|
315
|
+
function propertyNamesCheck(check) {
|
|
316
|
+
return (v) => {
|
|
317
|
+
for (const k in v) if (!check(k)) return false;
|
|
318
|
+
return true;
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
/** `minProperties` and `maxProperties` (`v` is an object). */
|
|
322
|
+
function propertyCountCheck(min, max) {
|
|
323
|
+
return (v) => {
|
|
324
|
+
let n = 0;
|
|
325
|
+
for (const _ in v) n++;
|
|
326
|
+
return n >= min && n <= max;
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
/** `dependentRequired` (`v` is an object). */
|
|
330
|
+
function dependentRequiredCheck(entries$1) {
|
|
331
|
+
return (v) => {
|
|
332
|
+
const o = v;
|
|
333
|
+
for (const [k, list] of entries$1) if (Object.hasOwn(o, k)) {
|
|
334
|
+
for (const r of list) if (!Object.hasOwn(o, r)) return false;
|
|
335
|
+
}
|
|
336
|
+
return true;
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
/** `dependentSchemas` (`v` is an object). */
|
|
340
|
+
function dependentSchemasCheck(entries$1) {
|
|
341
|
+
return (v) => {
|
|
342
|
+
for (const [k, check] of entries$1) if (Object.hasOwn(v, k) && !check(v)) return false;
|
|
343
|
+
return true;
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
const NO_LEAF = [0, void 0];
|
|
347
|
+
/** The common array: guard, length bounds, prefix items and the items after them, in one closure. */
|
|
348
|
+
function listCheck(guard, min, max, prefix, rest) {
|
|
349
|
+
const n = prefix.length;
|
|
350
|
+
const [code, arg] = (rest === void 0 ? void 0 : leafOf(rest)) ?? NO_LEAF;
|
|
351
|
+
return (v) => {
|
|
352
|
+
if (!Array.isArray(v)) return guard === GUARD_LOOSE;
|
|
353
|
+
const length = v.length;
|
|
354
|
+
if (length < min || length > max) return false;
|
|
355
|
+
const m = length < n ? length : n;
|
|
356
|
+
for (let i = 0; i < m; i++) if (!prefix[i](v[i])) return false;
|
|
357
|
+
if (rest === void 0) return true;
|
|
358
|
+
if (code !== 0) {
|
|
359
|
+
for (let i = n; i < length; i++) if (!leafOk(code, v[i], arg)) return false;
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
362
|
+
for (let i = n; i < length; i++) if (!rest(v[i])) return false;
|
|
363
|
+
return true;
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
/** Prefix items and the items after them (`v` is an array). */
|
|
367
|
+
function itemsCheck(prefix, rest) {
|
|
368
|
+
return listCheck(GUARD_NONE, 0, Number.POSITIVE_INFINITY, prefix, rest);
|
|
369
|
+
}
|
|
370
|
+
/** `minItems` and `maxItems` (`v` is an array). */
|
|
371
|
+
function lengthCheck(min, max) {
|
|
372
|
+
return (v) => v.length >= min && v.length <= max;
|
|
373
|
+
}
|
|
374
|
+
/** `contains` with its bounds (`v` is an array). */
|
|
375
|
+
function containsCheck(check, min, max) {
|
|
376
|
+
return (v) => {
|
|
377
|
+
const list = v;
|
|
378
|
+
let n = 0;
|
|
379
|
+
for (let i = 0; i < list.length; i++) if (check(list[i]) && ++n > max) return false;
|
|
380
|
+
return n >= min;
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
/** `uniqueItems` (`v` is an array). */
|
|
384
|
+
function uniqueCheck(v) {
|
|
385
|
+
const list = v;
|
|
386
|
+
for (let i = 1; i < list.length; i++) for (let j = 0; j < i; j++) if (deepEqual(list[i], list[j])) return false;
|
|
387
|
+
return true;
|
|
388
|
+
}
|
|
389
|
+
/** `enum`: up to 8 primitives by a scan, more by a Set, objects and arrays by deep equality. */
|
|
390
|
+
function enumCheck(values) {
|
|
391
|
+
const primitives = values.filter((x) => typeof x !== "object" || x === null);
|
|
392
|
+
if (primitives.length === values.length && primitives.length <= 8) return leaf(8, primitives, (v) => {
|
|
393
|
+
for (let i = 0; i < primitives.length; i++) if (primitives[i] === v) return true;
|
|
394
|
+
return false;
|
|
395
|
+
});
|
|
396
|
+
const set = new Set(primitives);
|
|
397
|
+
const objects = values.filter((x) => typeof x === "object" && x !== null);
|
|
398
|
+
return (v) => set.has(v) || typeof v === "object" && objects.some((o) => deepEqual(v, o));
|
|
399
|
+
}
|
|
400
|
+
/** `const`. */
|
|
401
|
+
function constCheck(value) {
|
|
402
|
+
if (typeof value !== "object" || value === null) return leaf(7, value, (v) => v === value);
|
|
403
|
+
return (v) => deepEqual(v, value);
|
|
404
|
+
}
|
|
405
|
+
/** `anyOf`. */
|
|
406
|
+
function anyCheck(checks) {
|
|
407
|
+
return (v) => {
|
|
408
|
+
for (let i = 0; i < checks.length; i++) if (checks[i](v)) return true;
|
|
409
|
+
return false;
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
/** `oneOf`. */
|
|
413
|
+
function oneCheck(checks) {
|
|
414
|
+
return (v) => {
|
|
415
|
+
let n = 0;
|
|
416
|
+
for (let i = 0; i < checks.length; i++) if (checks[i](v) && ++n > 1) return false;
|
|
417
|
+
return n === 1;
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
/** `not`. */
|
|
421
|
+
function notCheck(check) {
|
|
422
|
+
return (v) => !check(v);
|
|
423
|
+
}
|
|
424
|
+
/** `if`, `then` and `else`. */
|
|
425
|
+
function ifCheck(cond, then, otherwise) {
|
|
426
|
+
return (v) => cond(v) ? then(v) : otherwise(v);
|
|
427
|
+
}
|
|
428
|
+
/** A union of objects tagged by a const on `key`: the tag selects the one branch that can match. */
|
|
429
|
+
function taggedCheck(key, tags, checks) {
|
|
430
|
+
return (v) => {
|
|
431
|
+
if (!isObject(v)) return false;
|
|
432
|
+
const tag = v[key];
|
|
433
|
+
for (let i = 0; i < tags.length; i++) if (tags[i] === tag) return checks[i](v);
|
|
434
|
+
return false;
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* JSON equality, as `const`, `enum` and `uniqueItems` compare: the same primitives, arrays with equal items in order,
|
|
439
|
+
* objects with the same keys and equal values (key order does not matter).
|
|
440
|
+
*/
|
|
441
|
+
function deepEqual(a, b) {
|
|
442
|
+
if (a === b) return true;
|
|
443
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
|
|
444
|
+
const array = Array.isArray(a);
|
|
445
|
+
if (array !== Array.isArray(b)) return false;
|
|
446
|
+
if (array) {
|
|
447
|
+
const x$1 = a;
|
|
448
|
+
const y$1 = b;
|
|
449
|
+
if (x$1.length !== y$1.length) return false;
|
|
450
|
+
for (let i = 0; i < x$1.length; i++) if (!deepEqual(x$1[i], y$1[i])) return false;
|
|
451
|
+
return true;
|
|
452
|
+
}
|
|
453
|
+
const x = a;
|
|
454
|
+
const y = b;
|
|
455
|
+
const keys = Object.keys(x);
|
|
456
|
+
if (keys.length !== Object.keys(y).length) return false;
|
|
457
|
+
for (const k of keys) if (!Object.hasOwn(y, k) || !deepEqual(x[k], y[k])) return false;
|
|
458
|
+
return true;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
//#endregion
|
|
462
|
+
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.6.0/node_modules/@earendil-works/pi-schema/dist/resolver.js
|
|
2
463
|
/** The base URI of a root schema without `$id`; relative references resolve against it. */
|
|
3
464
|
const DEFAULT_BASE = "https://json-schema.invalid/root.json";
|
|
4
465
|
/**
|
|
5
466
|
* Resolves references in the root schema and the registered documents. The schema is indexed on the first
|
|
6
|
-
* reference, so a schema
|
|
467
|
+
* reference or the first `$id` a walker meets, so a schema with neither is never walked for identifiers.
|
|
7
468
|
*/
|
|
8
469
|
var Resolver = class {
|
|
9
470
|
draft;
|
|
10
|
-
|
|
471
|
+
/** The base URI the root schema is walked from; walkers apply the root's own `$id` through `baseFor`. */
|
|
472
|
+
rootBase = DEFAULT_BASE;
|
|
11
473
|
/** Schema object to the base URI it was indexed under. */
|
|
12
474
|
baseOf = /* @__PURE__ */ new Map();
|
|
13
475
|
/** URI (with a `#name` fragment) of every `$dynamicAnchor`, to its schema. */
|
|
@@ -26,17 +488,21 @@ var Resolver = class {
|
|
|
26
488
|
this.root = root;
|
|
27
489
|
this.options = options;
|
|
28
490
|
this.rejectUnresolved = options.unresolvedRefs === "reject";
|
|
29
|
-
this.rootBase = isSchemaObject(root) ? this.baseFor(root, DEFAULT_BASE) : DEFAULT_BASE;
|
|
30
491
|
}
|
|
31
492
|
/**
|
|
32
493
|
* The base URI of `node`, whose parent has `base`: its `$id` changes it, unless draft 7 makes the `$id` inert. Once
|
|
33
494
|
* the schema is indexed, the indexed base (a resolved reference target already has its own).
|
|
34
495
|
*/
|
|
35
496
|
baseFor(node, base) {
|
|
36
|
-
|
|
497
|
+
let indexed = this.baseOf.get(node);
|
|
37
498
|
if (indexed !== void 0) return indexed;
|
|
38
499
|
const id = node.$id;
|
|
39
500
|
if (typeof id !== "string") return base;
|
|
501
|
+
if (!this.indexed) {
|
|
502
|
+
this.ensureIndexed();
|
|
503
|
+
indexed = this.baseOf.get(node);
|
|
504
|
+
if (indexed !== void 0) return indexed;
|
|
505
|
+
}
|
|
40
506
|
if (this.draft === "7" && (typeof node.$ref === "string" || id.startsWith("#"))) return base;
|
|
41
507
|
return documentOf(resolveUri(id, base));
|
|
42
508
|
}
|
|
@@ -192,7 +658,7 @@ function documentOf(uri) {
|
|
|
192
658
|
}
|
|
193
659
|
|
|
194
660
|
//#endregion
|
|
195
|
-
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.
|
|
661
|
+
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.6.0/node_modules/@earendil-works/pi-schema/dist/shared.js
|
|
196
662
|
/**
|
|
197
663
|
* The regular expression of a `pattern` source, shared by source. Patterns are ECMAScript regular expressions with
|
|
198
664
|
* the `u` flag; a source that is invalid with it (some older schemas escape characters `u` rejects) compiles
|
|
@@ -217,7 +683,7 @@ const regexCleanup = new FinalizationRegistry((source) => {
|
|
|
217
683
|
});
|
|
218
684
|
|
|
219
685
|
//#endregion
|
|
220
|
-
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.
|
|
686
|
+
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.6.0/node_modules/@earendil-works/pi-schema/dist/coerce.js
|
|
221
687
|
/** Coerces values for one compiled schema. */
|
|
222
688
|
var Coercer = class {
|
|
223
689
|
c;
|
|
@@ -274,716 +740,381 @@ var Coercer = class {
|
|
|
274
740
|
entered.length -= 2;
|
|
275
741
|
}
|
|
276
742
|
if (memoize && this.cuts === cuts) {
|
|
277
|
-
this.memo ??= /* @__PURE__ */ new Map();
|
|
278
|
-
let values = this.memo.get(node);
|
|
279
|
-
if (values === void 0) {
|
|
280
|
-
values = /* @__PURE__ */ new Map();
|
|
281
|
-
this.memo.set(node, values);
|
|
282
|
-
}
|
|
283
|
-
values.set(value, result);
|
|
284
|
-
}
|
|
285
|
-
return result;
|
|
286
|
-
}
|
|
287
|
-
/** Whether `value` passes the schema `node`; undefined when `node` cannot be compiled. */
|
|
288
|
-
passes(node, base, value) {
|
|
289
|
-
try {
|
|
290
|
-
return this.c.check(node, this.baseOf(node, base))(value);
|
|
291
|
-
} catch {
|
|
292
|
-
return;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
baseOf(node, base) {
|
|
296
|
-
return isSchemaObject(node) ? this.c.r.baseOf.get(node) ?? base : base;
|
|
297
|
-
}
|
|
298
|
-
dropOptionalNulls(value, schema, base) {
|
|
299
|
-
if (!isSchemaObject(schema)) return value;
|
|
300
|
-
const b = this.baseOf(schema, base);
|
|
301
|
-
if (Array.isArray(value)) return mapItems(value, (item, i) => {
|
|
302
|
-
const itemSchema = itemSchemaAt(schema, i);
|
|
303
|
-
return itemSchema === void 0 ? item : this.dropOptionalNulls(item, itemSchema, b);
|
|
304
|
-
});
|
|
305
|
-
if (!isPlainObject(value)) return value;
|
|
306
|
-
const properties = isSchemaObject(schema.properties) ? schema.properties : {};
|
|
307
|
-
const patterns = isSchemaObject(schema.patternProperties) ? Object.entries(schema.patternProperties).map(([pattern, item]) => [sharedRegex(pattern), item]) : [];
|
|
308
|
-
const additional = isSchemaObject(schema.additionalProperties) ? schema.additionalProperties : void 0;
|
|
309
|
-
if (patterns.length === 0 && additional === void 0 && !isSchemaObject(schema.properties)) return value;
|
|
310
|
-
const required = new Set(Array.isArray(schema.required) ? schema.required : []);
|
|
311
|
-
let out;
|
|
312
|
-
const set = (key, next) => {
|
|
313
|
-
if (next === (out ?? value)[key]) return;
|
|
314
|
-
out ??= { ...value };
|
|
315
|
-
out[key] = next;
|
|
316
|
-
};
|
|
317
|
-
for (const [key, property] of Object.entries(properties)) {
|
|
318
|
-
if (!Object.hasOwn(value, key)) continue;
|
|
319
|
-
const isRef = isSchemaObject(property) && typeof property.$ref === "string";
|
|
320
|
-
if (value[key] === null && !required.has(key) && !isRef && this.passes(property, b, null) === false) {
|
|
321
|
-
out ??= { ...value };
|
|
322
|
-
delete out[key];
|
|
323
|
-
continue;
|
|
324
|
-
}
|
|
325
|
-
set(key, this.dropOptionalNulls(value[key], property, b));
|
|
326
|
-
}
|
|
327
|
-
if (patterns.length > 0 || additional !== void 0) for (const key of Object.keys(value)) {
|
|
328
|
-
let matched = Object.hasOwn(properties, key);
|
|
329
|
-
for (const [pattern, item] of patterns) {
|
|
330
|
-
if (!pattern.test(key)) continue;
|
|
331
|
-
matched = true;
|
|
332
|
-
set(key, this.dropOptionalNulls((out ?? value)[key], item, b));
|
|
333
|
-
}
|
|
334
|
-
if (!matched && additional !== void 0) set(key, this.dropOptionalNulls(value[key], additional, b));
|
|
335
|
-
}
|
|
336
|
-
return out ?? value;
|
|
337
|
-
}
|
|
338
|
-
convert(node, base, value) {
|
|
339
|
-
if (!isSchemaObject(node)) return value;
|
|
340
|
-
const b = this.baseOf(node, base);
|
|
341
|
-
if (typeof node.$ref === "string") {
|
|
342
|
-
let target;
|
|
343
|
-
try {
|
|
344
|
-
target = this.c.r.resolve(node.$ref, b);
|
|
345
|
-
} catch {
|
|
346
|
-
return value;
|
|
347
|
-
}
|
|
348
|
-
return isSchemaObject(target.node) ? this.reenter(target.node, target.base, value, true) : value;
|
|
349
|
-
}
|
|
350
|
-
return this.convertNode(node, b, value);
|
|
351
|
-
}
|
|
352
|
-
/** One schema node's conversion: its first keyword among `const`, `enum`, the unions, `allOf` and `type`. */
|
|
353
|
-
convertNode(node, b, value) {
|
|
354
|
-
if (Object.hasOwn(node, "const")) return convertLiteral(node.const, value);
|
|
355
|
-
if (Array.isArray(node.enum)) return this.convertEnum(node, node.enum, b, value);
|
|
356
|
-
if (Array.isArray(node.anyOf)) return this.convertUnion(node, node.anyOf, b, value);
|
|
357
|
-
if (Array.isArray(node.oneOf)) return this.convertUnion(node, node.oneOf, b, value);
|
|
358
|
-
if (Array.isArray(node.allOf)) {
|
|
359
|
-
let next = value;
|
|
360
|
-
for (const member of node.allOf) next = this.convert(member, b, next);
|
|
361
|
-
return this.convertMembers(node, b, next);
|
|
362
|
-
}
|
|
363
|
-
switch (node.type) {
|
|
364
|
-
case "string": return orValue(tryString(value), value);
|
|
365
|
-
case "number": return orValue(tryNumber(value), value);
|
|
366
|
-
case "integer": {
|
|
367
|
-
const result = tryNumber(value);
|
|
368
|
-
return result === void 0 ? value : Math.trunc(result.value);
|
|
369
|
-
}
|
|
370
|
-
case "boolean": return orValue(tryBoolean(value), value);
|
|
371
|
-
case "null": return orValue(tryNull(value), value);
|
|
372
|
-
case "array": return this.convertArray(node, b, value);
|
|
373
|
-
case "object": return this.convertObject(node, b, value);
|
|
374
|
-
case void 0:
|
|
375
|
-
if (node.properties !== void 0 || node.patternProperties !== void 0) return this.convertObject(node, b, value);
|
|
376
|
-
if (node.items !== void 0 || node.prefixItems !== void 0) return this.convertArray(node, b, value);
|
|
377
|
-
return value;
|
|
378
|
-
default: return value;
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
/** An enum is a union of its values: a member stays, else the first value the input converts to. */
|
|
382
|
-
convertEnum(node, values, base, value) {
|
|
383
|
-
if (values.some((member) => member === value)) return value;
|
|
384
|
-
for (const member of values) {
|
|
385
|
-
const candidate = convertLiteral(member, value);
|
|
386
|
-
if (candidate !== value && this.passes(node, base, candidate) === true) return candidate;
|
|
387
|
-
}
|
|
388
|
-
return value;
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* A value one branch accepts stays; else the first branch's conversion that the whole union accepts. Branches are
|
|
392
|
-
* converted in order and only until one is accepted, so a later recursive branch is not entered needlessly. A node
|
|
393
|
-
* that also declares members (plain JSON Schema; TypeBox's never do) converts them into each candidate before it is
|
|
394
|
-
* checked, so a branch is not discarded for a sibling property that is converted after it.
|
|
395
|
-
*/
|
|
396
|
-
convertUnion(node, branches, base, value) {
|
|
397
|
-
if (branches.some((branch) => this.passes(branch, base, value) === true)) return this.convertMembers(node, base, value);
|
|
398
|
-
for (const branch of branches) {
|
|
399
|
-
const candidate = this.convertMembers(node, base, this.convert(branch, base, value));
|
|
400
|
-
if (this.passes(node, base, candidate) === true) return candidate;
|
|
401
|
-
}
|
|
402
|
-
return this.convertMembers(node, base, value);
|
|
403
|
-
}
|
|
404
|
-
/** The object members of a union or intersection node, when it declares any. */
|
|
405
|
-
convertMembers(node, base, value) {
|
|
406
|
-
return node.properties !== void 0 || node.patternProperties !== void 0 || node.additionalProperties !== void 0 ? this.convertObject(node, base, value) : value;
|
|
407
|
-
}
|
|
408
|
-
convertArray(node, base, value) {
|
|
409
|
-
const prefix = Array.isArray(node.items) ? node.items : Array.isArray(node.prefixItems) ? node.prefixItems : void 0;
|
|
410
|
-
if (prefix !== void 0) {
|
|
411
|
-
if (!Array.isArray(value)) return value;
|
|
412
|
-
const rest = Array.isArray(node.items) ? void 0 : node.items;
|
|
413
|
-
return mapItems(value, (item$1, i) => {
|
|
414
|
-
const schema = i < prefix.length ? prefix[i] : rest;
|
|
415
|
-
return schema === void 0 ? item$1 : this.convert(schema, base, item$1);
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
if (Array.isArray(value)) return node.items === void 0 ? value : mapItems(value, (item$1) => this.convert(node.items, base, item$1));
|
|
419
|
-
const item = node.items;
|
|
420
|
-
return [isSchemaObject(item) ? this.reenter(item, base, value, false) : value];
|
|
421
|
-
}
|
|
422
|
-
convertObject(node, base, value) {
|
|
423
|
-
if (!isPlainObject(value)) return value;
|
|
424
|
-
const properties = isSchemaObject(node.properties) ? node.properties : {};
|
|
425
|
-
const patterns = isSchemaObject(node.patternProperties) ? Object.entries(node.patternProperties).map(([pattern, schema]) => [sharedRegex(pattern), schema]) : [];
|
|
426
|
-
const additional = isSchemaObject(node.additionalProperties) ? node.additionalProperties : void 0;
|
|
427
|
-
let out;
|
|
428
|
-
for (const key of Object.keys(value)) {
|
|
429
|
-
if (value[key] === void 0) continue;
|
|
430
|
-
let next = value[key];
|
|
431
|
-
let matched = Object.hasOwn(properties, key);
|
|
432
|
-
if (matched) next = this.convert(properties[key], base, next);
|
|
433
|
-
for (const [pattern, schema] of patterns) {
|
|
434
|
-
if (!pattern.test(key)) continue;
|
|
435
|
-
matched = true;
|
|
436
|
-
next = this.convert(schema, base, next);
|
|
437
|
-
}
|
|
438
|
-
if (!matched && additional !== void 0) next = this.convert(additional, base, next);
|
|
439
|
-
if (next !== value[key]) {
|
|
440
|
-
out ??= { ...value };
|
|
441
|
-
out[key] = next;
|
|
743
|
+
this.memo ??= /* @__PURE__ */ new Map();
|
|
744
|
+
let values = this.memo.get(node);
|
|
745
|
+
if (values === void 0) {
|
|
746
|
+
values = /* @__PURE__ */ new Map();
|
|
747
|
+
this.memo.set(node, values);
|
|
442
748
|
}
|
|
749
|
+
values.set(value, result);
|
|
443
750
|
}
|
|
444
|
-
return
|
|
751
|
+
return result;
|
|
445
752
|
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
if (Array.isArray(schema.oneOf)) next = this.coerceUnion(next, schema.oneOf, b);
|
|
453
|
-
const types = typesOf(schema);
|
|
454
|
-
const matchesUnionMember = types.length > 1 && types.some((type) => matchesType(next, type));
|
|
455
|
-
if (types.length > 0 && !matchesUnionMember) for (const type of types) {
|
|
456
|
-
const candidate = coercePrimitive(next, type);
|
|
457
|
-
if (candidate !== next) {
|
|
458
|
-
next = candidate;
|
|
459
|
-
break;
|
|
460
|
-
}
|
|
753
|
+
/** Whether `value` passes the schema `node`; undefined when `node` cannot be compiled. */
|
|
754
|
+
passes(node, base, value) {
|
|
755
|
+
try {
|
|
756
|
+
return this.c.check(node, this.baseOf(node, base))(value);
|
|
757
|
+
} catch {
|
|
758
|
+
return;
|
|
461
759
|
}
|
|
462
|
-
if (types.includes("object") && isPlainObject(next)) next = this.coerceProperties(next, schema, b);
|
|
463
|
-
if (types.includes("array") && Array.isArray(next)) next = this.coerceItems(next, schema, b);
|
|
464
|
-
return next;
|
|
465
760
|
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
for (const branch of branches) {
|
|
469
|
-
const coerced = this.coerceTypes(value, branch, base);
|
|
470
|
-
if (this.passes(branch, base, coerced) === true) return coerced;
|
|
471
|
-
}
|
|
472
|
-
return value;
|
|
761
|
+
baseOf(node, base) {
|
|
762
|
+
return isSchemaObject(node) ? this.c.r.baseFor(node, base) : base;
|
|
473
763
|
}
|
|
474
|
-
|
|
764
|
+
dropOptionalNulls(value, schema, base) {
|
|
765
|
+
if (!isSchemaObject(schema)) return value;
|
|
766
|
+
const b = this.baseOf(schema, base);
|
|
767
|
+
if (Array.isArray(value)) return mapItems(value, (item, i) => {
|
|
768
|
+
const itemSchema = itemSchemaAt(schema, i);
|
|
769
|
+
return itemSchema === void 0 ? item : this.dropOptionalNulls(item, itemSchema, b);
|
|
770
|
+
});
|
|
771
|
+
if (!isPlainObject(value)) return value;
|
|
475
772
|
const properties = isSchemaObject(schema.properties) ? schema.properties : {};
|
|
476
773
|
const patterns = isSchemaObject(schema.patternProperties) ? Object.entries(schema.patternProperties).map(([pattern, item]) => [sharedRegex(pattern), item]) : [];
|
|
477
774
|
const additional = isSchemaObject(schema.additionalProperties) ? schema.additionalProperties : void 0;
|
|
775
|
+
if (patterns.length === 0 && additional === void 0 && !isSchemaObject(schema.properties)) return value;
|
|
776
|
+
const required = new Set(Array.isArray(schema.required) ? schema.required : []);
|
|
478
777
|
let out;
|
|
479
778
|
const set = (key, next) => {
|
|
480
779
|
if (next === (out ?? value)[key]) return;
|
|
481
780
|
out ??= { ...value };
|
|
482
781
|
out[key] = next;
|
|
483
782
|
};
|
|
484
|
-
for (const [key, property] of Object.entries(properties))
|
|
485
|
-
|
|
783
|
+
for (const [key, property] of Object.entries(properties)) {
|
|
784
|
+
if (!Object.hasOwn(value, key)) continue;
|
|
785
|
+
const isRef = isSchemaObject(property) && typeof property.$ref === "string";
|
|
786
|
+
if (value[key] === null && !required.has(key) && !isRef && this.passes(property, b, null) === false) {
|
|
787
|
+
out ??= { ...value };
|
|
788
|
+
delete out[key];
|
|
789
|
+
continue;
|
|
790
|
+
}
|
|
791
|
+
set(key, this.dropOptionalNulls(value[key], property, b));
|
|
792
|
+
}
|
|
793
|
+
if (patterns.length > 0 || additional !== void 0) for (const key of Object.keys(value)) {
|
|
486
794
|
let matched = Object.hasOwn(properties, key);
|
|
487
795
|
for (const [pattern, item] of patterns) {
|
|
488
796
|
if (!pattern.test(key)) continue;
|
|
489
797
|
matched = true;
|
|
490
|
-
set(key, this.
|
|
798
|
+
set(key, this.dropOptionalNulls((out ?? value)[key], item, b));
|
|
491
799
|
}
|
|
492
|
-
if (!matched && additional !== void 0) set(key, this.
|
|
800
|
+
if (!matched && additional !== void 0) set(key, this.dropOptionalNulls(value[key], additional, b));
|
|
493
801
|
}
|
|
494
802
|
return out ?? value;
|
|
495
803
|
}
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
* `items` after `prefixItems`, or `items` for every item. Undefined when nothing applies.
|
|
506
|
-
*/
|
|
507
|
-
function itemSchemaAt(schema, index) {
|
|
508
|
-
if (Array.isArray(schema.items)) return schema.items[index];
|
|
509
|
-
if (Array.isArray(schema.prefixItems) && index < schema.prefixItems.length) return schema.prefixItems[index];
|
|
510
|
-
return schema.items;
|
|
511
|
-
}
|
|
512
|
-
/** `items` mapped by `map`; the same array when every item maps to itself. */
|
|
513
|
-
function mapItems(items, map) {
|
|
514
|
-
let out;
|
|
515
|
-
for (let i = 0; i < items.length; i++) {
|
|
516
|
-
const next = map(items[i], i);
|
|
517
|
-
if (next === items[i]) continue;
|
|
518
|
-
out ??= items.slice();
|
|
519
|
-
out[i] = next;
|
|
520
|
-
}
|
|
521
|
-
return out ?? items;
|
|
522
|
-
}
|
|
523
|
-
function orValue(result, value) {
|
|
524
|
-
return result === void 0 ? value : result.value;
|
|
525
|
-
}
|
|
526
|
-
/** A literal (`const`, an enum value) taken from a value of another type that converts to exactly it. */
|
|
527
|
-
function convertLiteral(literal, value) {
|
|
528
|
-
if (literal === value) return value;
|
|
529
|
-
const result = typeof literal === "string" ? tryString(value) : typeof literal === "number" ? tryNumber(value) : typeof literal === "boolean" ? tryBoolean(value) : void 0;
|
|
530
|
-
return result !== void 0 && result.value === literal ? result.value : value;
|
|
531
|
-
}
|
|
532
|
-
function tryString(value) {
|
|
533
|
-
if (typeof value === "bigint" || typeof value === "boolean" || isFiniteNumber(value)) return { value: String(value) };
|
|
534
|
-
if (value === null) return { value: "null" };
|
|
535
|
-
if (typeof value === "string") return { value };
|
|
536
|
-
if (value === void 0) return { value: "" };
|
|
537
|
-
}
|
|
538
|
-
const MAX_BIGINT = BigInt(Number.MAX_SAFE_INTEGER);
|
|
539
|
-
const MIN_BIGINT = BigInt(Number.MIN_SAFE_INTEGER);
|
|
540
|
-
function tryNumber(value) {
|
|
541
|
-
if (typeof value === "bigint") return value <= MAX_BIGINT && value >= MIN_BIGINT ? { value: Number(value) } : void 0;
|
|
542
|
-
if (typeof value === "boolean") return { value: value ? 1 : 0 };
|
|
543
|
-
if (isFiniteNumber(value)) return { value };
|
|
544
|
-
if (value === null || value === void 0) return { value: 0 };
|
|
545
|
-
if (typeof value !== "string") return void 0;
|
|
546
|
-
const coerced = +value;
|
|
547
|
-
if (Number.isFinite(coerced)) return { value: coerced };
|
|
548
|
-
const lowercase = value.toLowerCase();
|
|
549
|
-
if (lowercase === "false") return { value: 0 };
|
|
550
|
-
if (lowercase === "true") return { value: 1 };
|
|
551
|
-
const big = bigIntOfString(value);
|
|
552
|
-
return big !== void 0 && big <= MAX_BIGINT && big >= MIN_BIGINT ? { value: Number(big) } : void 0;
|
|
553
|
-
}
|
|
554
|
-
/** TypeBox's TryBigInt of a string: `12n`, `12.5` (truncated) or `12`; true and false are not reached from tryNumber. */
|
|
555
|
-
function bigIntOfString(value) {
|
|
556
|
-
if (/^-?(0|[1-9]\d*)n$/.test(value)) return BigInt(value.slice(0, -1));
|
|
557
|
-
if (/^-?(0|[1-9]\d*)\.\d+$/.test(value)) return BigInt(value.split(".")[0]);
|
|
558
|
-
if (/^-?(0|[1-9]\d*)$/.test(value)) return BigInt(value);
|
|
559
|
-
}
|
|
560
|
-
function tryBoolean(value) {
|
|
561
|
-
if (typeof value === "bigint") return value === 0n ? { value: false } : value === 1n ? { value: true } : void 0;
|
|
562
|
-
if (typeof value === "boolean") return { value };
|
|
563
|
-
if (isFiniteNumber(value)) return value === 0 ? { value: false } : value === 1 ? { value: true } : void 0;
|
|
564
|
-
if (value === null || value === void 0) return { value: false };
|
|
565
|
-
if (typeof value !== "string") return void 0;
|
|
566
|
-
const lowercase = value.toLowerCase();
|
|
567
|
-
if (lowercase === "false" || value === "0") return { value: false };
|
|
568
|
-
if (lowercase === "true" || value === "1") return { value: true };
|
|
569
|
-
}
|
|
570
|
-
function tryNull(value) {
|
|
571
|
-
if (typeof value === "bigint") return value === 0n ? { value: null } : void 0;
|
|
572
|
-
if (typeof value === "boolean") return value === false ? { value: null } : void 0;
|
|
573
|
-
if (isFiniteNumber(value)) return value === 0 ? { value: null } : void 0;
|
|
574
|
-
if (value === null || value === void 0) return { value: null };
|
|
575
|
-
if (typeof value !== "string") return void 0;
|
|
576
|
-
const lowercase = value.toLowerCase();
|
|
577
|
-
return lowercase === "undefined" || lowercase === "null" || value === "" || value === "0" ? { value: null } : void 0;
|
|
578
|
-
}
|
|
579
|
-
function isFiniteNumber(value) {
|
|
580
|
-
return typeof value === "number" && Number.isFinite(value);
|
|
581
|
-
}
|
|
582
|
-
function typesOf(schema) {
|
|
583
|
-
if (typeof schema.type === "string") return [schema.type];
|
|
584
|
-
if (Array.isArray(schema.type)) return schema.type.filter((type) => typeof type === "string");
|
|
585
|
-
return [];
|
|
586
|
-
}
|
|
587
|
-
function matchesType(value, type) {
|
|
588
|
-
switch (type) {
|
|
589
|
-
case "number": return typeof value === "number";
|
|
590
|
-
case "integer": return typeof value === "number" && Number.isInteger(value);
|
|
591
|
-
case "boolean": return typeof value === "boolean";
|
|
592
|
-
case "string": return typeof value === "string";
|
|
593
|
-
case "null": return value === null;
|
|
594
|
-
case "array": return Array.isArray(value);
|
|
595
|
-
case "object": return isPlainObject(value);
|
|
596
|
-
default: return false;
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
function coercePrimitive(value, type) {
|
|
600
|
-
switch (type) {
|
|
601
|
-
case "number":
|
|
602
|
-
case "integer":
|
|
603
|
-
if (value === null) return 0;
|
|
604
|
-
if (typeof value === "string" && value.trim() !== "") {
|
|
605
|
-
const parsed = Number(value);
|
|
606
|
-
if (type === "number" ? Number.isFinite(parsed) : Number.isInteger(parsed)) return parsed;
|
|
804
|
+
convert(node, base, value) {
|
|
805
|
+
if (!isSchemaObject(node)) return value;
|
|
806
|
+
const b = this.baseOf(node, base);
|
|
807
|
+
if (typeof node.$ref === "string") {
|
|
808
|
+
let target;
|
|
809
|
+
try {
|
|
810
|
+
target = this.c.r.resolve(node.$ref, b);
|
|
811
|
+
} catch {
|
|
812
|
+
return value;
|
|
607
813
|
}
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
if (value === null) return false;
|
|
612
|
-
if (value === "true" || value === 1) return true;
|
|
613
|
-
if (value === "false" || value === 0) return false;
|
|
614
|
-
return value;
|
|
615
|
-
case "string":
|
|
616
|
-
if (value === null) return "";
|
|
617
|
-
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
618
|
-
return value;
|
|
619
|
-
case "null": return value === "" || value === 0 || value === false ? null : value;
|
|
620
|
-
default: return value;
|
|
814
|
+
return isSchemaObject(target.node) ? this.reenter(target.node, target.base, value, true) : value;
|
|
815
|
+
}
|
|
816
|
+
return this.convertNode(node, b, value);
|
|
621
817
|
}
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
const
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
const
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
["bytes", IS_BYTES]
|
|
653
|
-
]);
|
|
654
|
-
/** Leaf checks that object and array loops run inline by code instead of calling them. */
|
|
655
|
-
const LEAF_CODES = new Map([
|
|
656
|
-
[IS_STRING, 1],
|
|
657
|
-
[IS_NUMBER, 2],
|
|
658
|
-
[IS_INTEGER, 3],
|
|
659
|
-
[IS_BOOLEAN, 4],
|
|
660
|
-
[IS_NULL, 5],
|
|
661
|
-
[ALWAYS, 6]
|
|
662
|
-
]);
|
|
663
|
-
function leafOk(code, x) {
|
|
664
|
-
switch (code) {
|
|
665
|
-
case 1: return typeof x === "string";
|
|
666
|
-
case 2: return Number.isFinite(x);
|
|
667
|
-
case 3: return Number.isInteger(x);
|
|
668
|
-
case 4: return typeof x === "boolean";
|
|
669
|
-
case 5: return x === null;
|
|
670
|
-
default: return true;
|
|
818
|
+
/** One schema node's conversion: its first keyword among `const`, `enum`, the unions, `allOf` and `type`. */
|
|
819
|
+
convertNode(node, b, value) {
|
|
820
|
+
if (Object.hasOwn(node, "const")) return convertLiteral(node.const, value);
|
|
821
|
+
if (Array.isArray(node.enum)) return this.convertEnum(node, node.enum, b, value);
|
|
822
|
+
if (Array.isArray(node.anyOf)) return this.convertUnion(node, node.anyOf, b, value);
|
|
823
|
+
if (Array.isArray(node.oneOf)) return this.convertUnion(node, node.oneOf, b, value);
|
|
824
|
+
if (Array.isArray(node.allOf)) {
|
|
825
|
+
let next = value;
|
|
826
|
+
for (const member of node.allOf) next = this.convert(member, b, next);
|
|
827
|
+
return this.convertMembers(node, b, next);
|
|
828
|
+
}
|
|
829
|
+
switch (node.type) {
|
|
830
|
+
case "string": return orValue(tryString(value), value);
|
|
831
|
+
case "number": return orValue(tryNumber(value), value);
|
|
832
|
+
case "integer": {
|
|
833
|
+
const result = tryNumber(value);
|
|
834
|
+
return result === void 0 ? value : Math.trunc(result.value);
|
|
835
|
+
}
|
|
836
|
+
case "boolean": return orValue(tryBoolean(value), value);
|
|
837
|
+
case "null": return orValue(tryNull(value), value);
|
|
838
|
+
case "bigint": return orValue(tryBigInt(value), value);
|
|
839
|
+
case "undefined": return orValue(tryUndefined(value), value);
|
|
840
|
+
case "array": return this.convertArray(node, b, value);
|
|
841
|
+
case "object": return this.convertObject(node, b, value);
|
|
842
|
+
case void 0:
|
|
843
|
+
if (node.properties !== void 0 || node.patternProperties !== void 0) return this.convertObject(node, b, value);
|
|
844
|
+
if (node.items !== void 0 || node.prefixItems !== void 0) return this.convertArray(node, b, value);
|
|
845
|
+
return value;
|
|
846
|
+
default: return value;
|
|
847
|
+
}
|
|
671
848
|
}
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
const
|
|
676
|
-
const
|
|
677
|
-
|
|
678
|
-
const CODE = 7;
|
|
679
|
-
const REQUIRED = 8;
|
|
680
|
-
const INHERITED = 16;
|
|
681
|
-
/** All of `parts`; two and three parts get closures without a loop. */
|
|
682
|
-
function and(parts) {
|
|
683
|
-
switch (parts.length) {
|
|
684
|
-
case 0: return ALWAYS;
|
|
685
|
-
case 1: return parts[0];
|
|
686
|
-
case 2: {
|
|
687
|
-
const [a, b] = parts;
|
|
688
|
-
return (v) => a(v) && b(v);
|
|
849
|
+
/** An enum is a union of its values: a member stays, else the first value the input converts to. */
|
|
850
|
+
convertEnum(node, values, base, value) {
|
|
851
|
+
if (values.some((member) => member === value)) return value;
|
|
852
|
+
for (const member of values) {
|
|
853
|
+
const candidate = convertLiteral(member, value);
|
|
854
|
+
if (candidate !== value && this.passes(node, base, candidate) === true) return candidate;
|
|
689
855
|
}
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
856
|
+
return value;
|
|
857
|
+
}
|
|
858
|
+
/**
|
|
859
|
+
* A value one branch accepts stays; else the first branch's conversion that the whole union accepts. Branches are
|
|
860
|
+
* converted in order and only until one is accepted, so a later recursive branch is not entered needlessly. A node
|
|
861
|
+
* that also declares members (plain JSON Schema; TypeBox's never do) converts them into each candidate before it is
|
|
862
|
+
* checked, so a branch is not discarded for a sibling property that is converted after it.
|
|
863
|
+
*/
|
|
864
|
+
convertUnion(node, branches, base, value) {
|
|
865
|
+
if (branches.some((branch) => this.passes(branch, base, value) === true)) return this.convertMembers(node, base, value);
|
|
866
|
+
for (const branch of branches) {
|
|
867
|
+
const candidate = this.convertMembers(node, base, this.convert(branch, base, value));
|
|
868
|
+
if (this.passes(node, base, candidate) === true) return candidate;
|
|
693
869
|
}
|
|
694
|
-
|
|
695
|
-
for (let i = 0; i < parts.length; i++) if (!parts[i](v)) return false;
|
|
696
|
-
return true;
|
|
697
|
-
};
|
|
870
|
+
return this.convertMembers(node, base, value);
|
|
698
871
|
}
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
}
|
|
713
|
-
/** Any of the named types. */
|
|
714
|
-
function typeCheck(types) {
|
|
715
|
-
const checks = types.map((t) => (typeof t === "string" ? TYPE_CHECKS.get(t) : void 0) ?? NEVER);
|
|
716
|
-
if (checks.length === 1) return checks[0];
|
|
717
|
-
return (v) => {
|
|
718
|
-
for (let i = 0; i < checks.length; i++) if (checks[i](v)) return true;
|
|
719
|
-
return false;
|
|
720
|
-
};
|
|
721
|
-
}
|
|
722
|
-
/** A Uint8Array within the length bounds. */
|
|
723
|
-
function bytesCheck(min, max) {
|
|
724
|
-
return (v) => v instanceof Uint8Array && v.length >= min && v.length <= max;
|
|
725
|
-
}
|
|
726
|
-
/** The number of Unicode code points of `s`: JSON Schema string lengths count code points, not UTF-16 units. */
|
|
727
|
-
function codePoints(s) {
|
|
728
|
-
let n = 0;
|
|
729
|
-
for (let i = 0; i < s.length; i++, n++) {
|
|
730
|
-
const c = s.charCodeAt(i);
|
|
731
|
-
if (c >= 55296 && c <= 56319 && i + 1 < s.length) {
|
|
732
|
-
const next = s.charCodeAt(i + 1);
|
|
733
|
-
if (next >= 56320 && next <= 57343) i++;
|
|
872
|
+
/** The object members of a union or intersection node, when it declares any. */
|
|
873
|
+
convertMembers(node, base, value) {
|
|
874
|
+
return node.properties !== void 0 || node.patternProperties !== void 0 || node.additionalProperties !== void 0 ? this.convertObject(node, base, value) : value;
|
|
875
|
+
}
|
|
876
|
+
convertArray(node, base, value) {
|
|
877
|
+
const prefix = Array.isArray(node.items) ? node.items : Array.isArray(node.prefixItems) ? node.prefixItems : void 0;
|
|
878
|
+
if (prefix !== void 0) {
|
|
879
|
+
if (!Array.isArray(value)) return value;
|
|
880
|
+
const rest = Array.isArray(node.items) ? void 0 : node.items;
|
|
881
|
+
return mapItems(value, (item$1, i) => {
|
|
882
|
+
const schema = i < prefix.length ? prefix[i] : rest;
|
|
883
|
+
return schema === void 0 ? item$1 : this.convert(schema, base, item$1);
|
|
884
|
+
});
|
|
734
885
|
}
|
|
886
|
+
if (Array.isArray(value)) return node.items === void 0 ? value : mapItems(value, (item$1) => this.convert(node.items, base, item$1));
|
|
887
|
+
const item = node.items;
|
|
888
|
+
return [isSchemaObject(item) ? this.reenter(item, base, value, false) : value];
|
|
735
889
|
}
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
};
|
|
752
|
-
}
|
|
753
|
-
/** The number keywords. `mode`: 0 passes other types, 1 requires a number, 2 an integer. */
|
|
754
|
-
function numberCheck(mode, min, max, xmin, xmax, multipleOf) {
|
|
755
|
-
return (v) => {
|
|
756
|
-
if (typeof v !== "number") return mode === 0;
|
|
757
|
-
if (mode === 2 && !Number.isInteger(v)) return false;
|
|
758
|
-
return v >= min && v <= max && v > xmin && v < xmax && (multipleOf === void 0 || isMultiple(v, multipleOf));
|
|
759
|
-
};
|
|
760
|
-
}
|
|
761
|
-
/** Whether `v` is a multiple of `d`, exactly for decimals: 0.0075 / 0.0001 is 74.99999999999999 in floating point. */
|
|
762
|
-
function isMultiple(v, d) {
|
|
763
|
-
const q = v / d;
|
|
764
|
-
if (Number.isInteger(q)) return true;
|
|
765
|
-
if (!Number.isFinite(q)) return v % d === 0;
|
|
766
|
-
const [vc, ve] = decimal(v);
|
|
767
|
-
const [dc, de] = decimal(d);
|
|
768
|
-
return ve >= de ? vc * 10n ** BigInt(ve - de) % dc === 0n : vc % (dc * 10n ** BigInt(de - ve)) === 0n;
|
|
769
|
-
}
|
|
770
|
-
function decimal(x) {
|
|
771
|
-
const [mantissa, exponent = "0"] = Math.abs(x).toString().split("e");
|
|
772
|
-
const point = mantissa.indexOf(".");
|
|
773
|
-
const fraction = point < 0 ? 0 : mantissa.length - point - 1;
|
|
774
|
-
return [BigInt(mantissa.replace(".", "")), Number(exponent) - fraction];
|
|
775
|
-
}
|
|
776
|
-
/** `inner` for objects; other values pass unless `strict`. */
|
|
777
|
-
function guardObject(inner, strict) {
|
|
778
|
-
return strict ? (v) => isObject(v) && inner(v) : (v) => !isObject(v) || inner(v);
|
|
779
|
-
}
|
|
780
|
-
/** `inner` for arrays; other values pass unless `strict`. */
|
|
781
|
-
function guardArray(inner, strict) {
|
|
782
|
-
return strict ? (v) => Array.isArray(v) && inner(v) : (v) => !Array.isArray(v) || inner(v);
|
|
783
|
-
}
|
|
784
|
-
/**
|
|
785
|
-
* An object with declared properties: `entries` is `[key, check, flags]` per property. The value's own keys are
|
|
786
|
-
* walked with for...in (whose `o[k]` V8 reads from the enum cache, unlike a load by a name from a list), each found
|
|
787
|
-
* among the declared names; `closed` rejects any other key. A property whose value is `undefined` counts as absent.
|
|
788
|
-
*/
|
|
789
|
-
function declaredObject(entries$1, requiredCount, closed, guard) {
|
|
790
|
-
const n = entries$1.length;
|
|
791
|
-
return (v) => {
|
|
792
|
-
if (guard !== GUARD_NONE && !isObject(v)) return guard === GUARD_LOOSE;
|
|
793
|
-
const o = v;
|
|
794
|
-
let required = 0;
|
|
795
|
-
for (const k in o) {
|
|
796
|
-
let i = 0;
|
|
797
|
-
while (i < n && entries$1[i] !== k) i += 3;
|
|
798
|
-
if (i === n) {
|
|
799
|
-
if (closed) return false;
|
|
800
|
-
continue;
|
|
890
|
+
convertObject(node, base, value) {
|
|
891
|
+
if (!isPlainObject(value)) return value;
|
|
892
|
+
const properties = isSchemaObject(node.properties) ? node.properties : {};
|
|
893
|
+
const patterns = isSchemaObject(node.patternProperties) ? Object.entries(node.patternProperties).map(([pattern, schema]) => [sharedRegex(pattern), schema]) : [];
|
|
894
|
+
const additional = isSchemaObject(node.additionalProperties) ? node.additionalProperties : void 0;
|
|
895
|
+
let out;
|
|
896
|
+
for (const key of Object.keys(value)) {
|
|
897
|
+
if (value[key] === void 0) continue;
|
|
898
|
+
let next = value[key];
|
|
899
|
+
let matched = Object.hasOwn(properties, key);
|
|
900
|
+
if (matched) next = this.convert(properties[key], base, next);
|
|
901
|
+
for (const [pattern, schema] of patterns) {
|
|
902
|
+
if (!pattern.test(key)) continue;
|
|
903
|
+
matched = true;
|
|
904
|
+
next = this.convert(schema, base, next);
|
|
801
905
|
}
|
|
802
|
-
|
|
803
|
-
if (
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
if (c !== 0 ? !leafOk(c, x) : !entries$1[i + 1](x)) return false;
|
|
807
|
-
if (f & REQUIRED) required++;
|
|
808
|
-
}
|
|
809
|
-
return required === requiredCount;
|
|
810
|
-
};
|
|
811
|
-
}
|
|
812
|
-
/** An object with declared properties, required keys not declared, pattern properties and additional properties. */
|
|
813
|
-
function openObject(entries$1, keys, extraRequired, patterns, additional, guard) {
|
|
814
|
-
const known = patterns.length > 0 || additional !== void 0 ? new Set(keys) : void 0;
|
|
815
|
-
return (v) => {
|
|
816
|
-
if (guard !== GUARD_NONE && !isObject(v)) return guard === GUARD_LOOSE;
|
|
817
|
-
const o = v;
|
|
818
|
-
for (let i = 0; i < entries$1.length; i += 3) {
|
|
819
|
-
const k = entries$1[i];
|
|
820
|
-
const x = o[k];
|
|
821
|
-
const f = entries$1[i + 2];
|
|
822
|
-
if (x === void 0 || f & INHERITED && !Object.hasOwn(o, k)) {
|
|
823
|
-
if (f & REQUIRED) return false;
|
|
824
|
-
continue;
|
|
906
|
+
if (!matched && additional !== void 0) next = this.convert(additional, base, next);
|
|
907
|
+
if (next !== value[key]) {
|
|
908
|
+
out ??= { ...value };
|
|
909
|
+
out[key] = next;
|
|
825
910
|
}
|
|
826
|
-
const c = f & CODE;
|
|
827
|
-
if (c !== 0 ? !leafOk(c, x) : !entries$1[i + 1](x)) return false;
|
|
828
911
|
}
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
912
|
+
return out ?? value;
|
|
913
|
+
}
|
|
914
|
+
coerceTypes(value, schema, base) {
|
|
915
|
+
if (!isSchemaObject(schema)) return value;
|
|
916
|
+
const b = this.baseOf(schema, base);
|
|
917
|
+
let next = value;
|
|
918
|
+
if (Array.isArray(schema.allOf)) for (const member of schema.allOf) next = this.coerceTypes(next, member, b);
|
|
919
|
+
if (Array.isArray(schema.anyOf)) next = this.coerceUnion(next, schema.anyOf, b);
|
|
920
|
+
if (Array.isArray(schema.oneOf)) next = this.coerceUnion(next, schema.oneOf, b);
|
|
921
|
+
const types = typesOf(schema);
|
|
922
|
+
const matchesUnionMember = types.length > 1 && types.some((type) => matchesType(next, type));
|
|
923
|
+
if (types.length > 0 && !matchesUnionMember) for (const type of types) {
|
|
924
|
+
const candidate = coercePrimitive(next, type);
|
|
925
|
+
if (candidate !== next) {
|
|
926
|
+
next = candidate;
|
|
927
|
+
break;
|
|
836
928
|
}
|
|
837
|
-
if (!matched && additional !== void 0 && !additional(o[k])) return false;
|
|
838
929
|
}
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
for (const
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
for (const [
|
|
862
|
-
|
|
930
|
+
if (types.includes("object") && isPlainObject(next)) next = this.coerceProperties(next, schema, b);
|
|
931
|
+
if (types.includes("array") && Array.isArray(next)) next = this.coerceItems(next, schema, b);
|
|
932
|
+
return next;
|
|
933
|
+
}
|
|
934
|
+
coerceUnion(value, branches, base) {
|
|
935
|
+
for (const branch of branches) if (this.passes(branch, base, value) === true) return value;
|
|
936
|
+
for (const branch of branches) {
|
|
937
|
+
const coerced = this.coerceTypes(value, branch, base);
|
|
938
|
+
if (this.passes(branch, base, coerced) === true) return coerced;
|
|
939
|
+
}
|
|
940
|
+
return value;
|
|
941
|
+
}
|
|
942
|
+
coerceProperties(value, schema, base) {
|
|
943
|
+
const properties = isSchemaObject(schema.properties) ? schema.properties : {};
|
|
944
|
+
const patterns = isSchemaObject(schema.patternProperties) ? Object.entries(schema.patternProperties).map(([pattern, item]) => [sharedRegex(pattern), item]) : [];
|
|
945
|
+
const additional = isSchemaObject(schema.additionalProperties) ? schema.additionalProperties : void 0;
|
|
946
|
+
let out;
|
|
947
|
+
const set = (key, next) => {
|
|
948
|
+
if (next === (out ?? value)[key]) return;
|
|
949
|
+
out ??= { ...value };
|
|
950
|
+
out[key] = next;
|
|
951
|
+
};
|
|
952
|
+
for (const [key, property] of Object.entries(properties)) if (key in value) set(key, this.coerceTypes(value[key], property, base));
|
|
953
|
+
for (const key of Object.keys(value)) {
|
|
954
|
+
let matched = Object.hasOwn(properties, key);
|
|
955
|
+
for (const [pattern, item] of patterns) {
|
|
956
|
+
if (!pattern.test(key)) continue;
|
|
957
|
+
matched = true;
|
|
958
|
+
set(key, this.coerceTypes((out ?? value)[key], item, base));
|
|
959
|
+
}
|
|
960
|
+
if (!matched && additional !== void 0) set(key, this.coerceTypes(value[key], additional, base));
|
|
863
961
|
}
|
|
864
|
-
return
|
|
865
|
-
}
|
|
962
|
+
return out ?? value;
|
|
963
|
+
}
|
|
964
|
+
coerceItems(value, schema, base) {
|
|
965
|
+
return mapItems(value, (item, i) => {
|
|
966
|
+
const itemSchema = itemSchemaAt(schema, i);
|
|
967
|
+
return isSchemaObject(itemSchema) ? this.coerceTypes(item, itemSchema, base) : item;
|
|
968
|
+
});
|
|
969
|
+
}
|
|
970
|
+
};
|
|
971
|
+
/**
|
|
972
|
+
* The schema of an array's item at `index`: a tuple's (`prefixItems`, or a draft-7 `items` array) for its prefix,
|
|
973
|
+
* `items` after `prefixItems`, or `items` for every item. Undefined when nothing applies.
|
|
974
|
+
*/
|
|
975
|
+
function itemSchemaAt(schema, index) {
|
|
976
|
+
if (Array.isArray(schema.items)) return schema.items[index];
|
|
977
|
+
if (Array.isArray(schema.prefixItems) && index < schema.prefixItems.length) return schema.prefixItems[index];
|
|
978
|
+
return schema.items;
|
|
866
979
|
}
|
|
867
|
-
/** `
|
|
868
|
-
function
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
980
|
+
/** `items` mapped by `map`; the same array when every item maps to itself. */
|
|
981
|
+
function mapItems(items, map) {
|
|
982
|
+
let out;
|
|
983
|
+
for (let i = 0; i < items.length; i++) {
|
|
984
|
+
const next = map(items[i], i);
|
|
985
|
+
if (next === items[i]) continue;
|
|
986
|
+
out ??= items.slice();
|
|
987
|
+
out[i] = next;
|
|
988
|
+
}
|
|
989
|
+
return out ?? items;
|
|
873
990
|
}
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
const n = prefix.length;
|
|
877
|
-
const code = rest === void 0 ? 0 : LEAF_CODES.get(rest) ?? 0;
|
|
878
|
-
return (v) => {
|
|
879
|
-
if (!Array.isArray(v)) return guard === GUARD_LOOSE;
|
|
880
|
-
const length = v.length;
|
|
881
|
-
if (length < min || length > max) return false;
|
|
882
|
-
const m = length < n ? length : n;
|
|
883
|
-
for (let i = 0; i < m; i++) if (!prefix[i](v[i])) return false;
|
|
884
|
-
if (rest === void 0) return true;
|
|
885
|
-
if (code !== 0) {
|
|
886
|
-
for (let i = n; i < length; i++) if (!leafOk(code, v[i])) return false;
|
|
887
|
-
return true;
|
|
888
|
-
}
|
|
889
|
-
for (let i = n; i < length; i++) if (!rest(v[i])) return false;
|
|
890
|
-
return true;
|
|
891
|
-
};
|
|
991
|
+
function orValue(result, value) {
|
|
992
|
+
return result === void 0 ? value : result.value;
|
|
892
993
|
}
|
|
893
|
-
/**
|
|
894
|
-
function
|
|
895
|
-
|
|
994
|
+
/** A literal (`const`, an enum value) taken from a value of another type that converts to exactly it. */
|
|
995
|
+
function convertLiteral(literal, value) {
|
|
996
|
+
if (literal === value) return value;
|
|
997
|
+
const result = typeof literal === "string" ? tryString(value) : typeof literal === "number" ? tryNumber(value) : typeof literal === "boolean" ? tryBoolean(value) : typeof literal === "bigint" ? tryBigInt(value) : void 0;
|
|
998
|
+
return result !== void 0 && result.value === literal ? result.value : value;
|
|
896
999
|
}
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
1000
|
+
function tryString(value) {
|
|
1001
|
+
if (typeof value === "bigint" || typeof value === "boolean" || isFiniteNumber(value)) return { value: String(value) };
|
|
1002
|
+
if (value === null) return { value: "null" };
|
|
1003
|
+
if (typeof value === "string") return { value };
|
|
1004
|
+
if (value === void 0) return { value: "" };
|
|
900
1005
|
}
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
1006
|
+
const MAX_BIGINT = BigInt(Number.MAX_SAFE_INTEGER);
|
|
1007
|
+
const MIN_BIGINT = BigInt(Number.MIN_SAFE_INTEGER);
|
|
1008
|
+
function tryNumber(value) {
|
|
1009
|
+
if (typeof value === "bigint") return value <= MAX_BIGINT && value >= MIN_BIGINT ? { value: Number(value) } : void 0;
|
|
1010
|
+
if (typeof value === "boolean") return { value: value ? 1 : 0 };
|
|
1011
|
+
if (isFiniteNumber(value)) return { value };
|
|
1012
|
+
if (value === null || value === void 0) return { value: 0 };
|
|
1013
|
+
if (typeof value !== "string") return void 0;
|
|
1014
|
+
const coerced = +value;
|
|
1015
|
+
if (Number.isFinite(coerced)) return { value: coerced };
|
|
1016
|
+
const lowercase = value.toLowerCase();
|
|
1017
|
+
if (lowercase === "false") return { value: 0 };
|
|
1018
|
+
if (lowercase === "true") return { value: 1 };
|
|
1019
|
+
const big = bigIntOfString(value);
|
|
1020
|
+
return big !== void 0 && big <= MAX_BIGINT && big >= MIN_BIGINT ? { value: Number(big) } : void 0;
|
|
909
1021
|
}
|
|
910
|
-
/** `
|
|
911
|
-
function
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
return
|
|
1022
|
+
/** TypeBox's TryBigInt of a string: `12n`, `12.5` (truncated) or `12`; true and false are not reached from tryNumber. */
|
|
1023
|
+
function bigIntOfString(value) {
|
|
1024
|
+
if (/^-?(0|[1-9]\d*)n$/.test(value)) return BigInt(value.slice(0, -1));
|
|
1025
|
+
if (/^-?(0|[1-9]\d*)\.\d+$/.test(value)) return BigInt(value.split(".")[0]);
|
|
1026
|
+
if (/^-?(0|[1-9]\d*)$/.test(value)) return BigInt(value);
|
|
915
1027
|
}
|
|
916
|
-
/**
|
|
917
|
-
function
|
|
918
|
-
|
|
919
|
-
if (
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
const
|
|
924
|
-
|
|
925
|
-
|
|
1028
|
+
/** TypeBox's TryBigInt: booleans to 1n/0n, numbers truncated, null and undefined to 0n, bigint-like strings. */
|
|
1029
|
+
function tryBigInt(value) {
|
|
1030
|
+
if (typeof value === "bigint") return { value };
|
|
1031
|
+
if (typeof value === "boolean") return { value: value ? 1n : 0n };
|
|
1032
|
+
if (isFiniteNumber(value)) return { value: BigInt(Math.trunc(value)) };
|
|
1033
|
+
if (value === null || value === void 0) return { value: 0n };
|
|
1034
|
+
if (typeof value !== "string") return void 0;
|
|
1035
|
+
const big = bigIntOfString(value);
|
|
1036
|
+
if (big !== void 0) return { value: big };
|
|
1037
|
+
const lowercase = value.toLowerCase();
|
|
1038
|
+
return lowercase === "false" ? { value: 0n } : lowercase === "true" ? { value: 1n } : void 0;
|
|
926
1039
|
}
|
|
927
|
-
/** `
|
|
928
|
-
function
|
|
929
|
-
if (
|
|
930
|
-
|
|
1040
|
+
/** TypeBox's TryUndefined: 0n, false, 0, null, `"undefined"`, `"null"`, `""` and `"0"` to undefined. */
|
|
1041
|
+
function tryUndefined(value) {
|
|
1042
|
+
if (value === void 0 || value === null || value === 0n || value === false || value === 0) return { value: void 0 };
|
|
1043
|
+
if (typeof value !== "string") return void 0;
|
|
1044
|
+
const lowercase = value.toLowerCase();
|
|
1045
|
+
return lowercase === "undefined" || lowercase === "null" || value === "" || value === "0" ? { value: void 0 } : void 0;
|
|
931
1046
|
}
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
1047
|
+
function tryBoolean(value) {
|
|
1048
|
+
if (typeof value === "bigint") return value === 0n ? { value: false } : value === 1n ? { value: true } : void 0;
|
|
1049
|
+
if (typeof value === "boolean") return { value };
|
|
1050
|
+
if (isFiniteNumber(value)) return value === 0 ? { value: false } : value === 1 ? { value: true } : void 0;
|
|
1051
|
+
if (value === null || value === void 0) return { value: false };
|
|
1052
|
+
if (typeof value !== "string") return void 0;
|
|
1053
|
+
const lowercase = value.toLowerCase();
|
|
1054
|
+
if (lowercase === "false" || value === "0") return { value: false };
|
|
1055
|
+
if (lowercase === "true" || value === "1") return { value: true };
|
|
938
1056
|
}
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
1057
|
+
function tryNull(value) {
|
|
1058
|
+
if (typeof value === "bigint") return value === 0n ? { value: null } : void 0;
|
|
1059
|
+
if (typeof value === "boolean") return value === false ? { value: null } : void 0;
|
|
1060
|
+
if (isFiniteNumber(value)) return value === 0 ? { value: null } : void 0;
|
|
1061
|
+
if (value === null || value === void 0) return { value: null };
|
|
1062
|
+
if (typeof value !== "string") return void 0;
|
|
1063
|
+
const lowercase = value.toLowerCase();
|
|
1064
|
+
return lowercase === "undefined" || lowercase === "null" || value === "" || value === "0" ? { value: null } : void 0;
|
|
946
1065
|
}
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
return (v) => !check(v);
|
|
1066
|
+
function isFiniteNumber(value) {
|
|
1067
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
950
1068
|
}
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
1069
|
+
function typesOf(schema) {
|
|
1070
|
+
if (typeof schema.type === "string") return [schema.type];
|
|
1071
|
+
if (Array.isArray(schema.type)) return schema.type.filter((type) => typeof type === "string");
|
|
1072
|
+
return [];
|
|
954
1073
|
}
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
return
|
|
962
|
-
|
|
1074
|
+
function matchesType(value, type) {
|
|
1075
|
+
switch (type) {
|
|
1076
|
+
case "number": return Number.isFinite(value);
|
|
1077
|
+
case "integer": return typeof value === "number" && Number.isInteger(value);
|
|
1078
|
+
case "boolean": return typeof value === "boolean";
|
|
1079
|
+
case "string": return typeof value === "string";
|
|
1080
|
+
case "null": return value === null;
|
|
1081
|
+
case "bigint": return typeof value === "bigint";
|
|
1082
|
+
case "undefined": return value === void 0;
|
|
1083
|
+
case "array": return Array.isArray(value);
|
|
1084
|
+
case "object": return isPlainObject(value);
|
|
1085
|
+
default: return false;
|
|
1086
|
+
}
|
|
963
1087
|
}
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
1088
|
+
function coercePrimitive(value, type) {
|
|
1089
|
+
switch (type) {
|
|
1090
|
+
case "number":
|
|
1091
|
+
case "integer":
|
|
1092
|
+
if (value === null) return 0;
|
|
1093
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
1094
|
+
const parsed = Number(value);
|
|
1095
|
+
if (type === "number" ? Number.isFinite(parsed) : Number.isInteger(parsed)) return parsed;
|
|
1096
|
+
}
|
|
1097
|
+
if (typeof value === "boolean") return value ? 1 : 0;
|
|
1098
|
+
return value;
|
|
1099
|
+
case "boolean":
|
|
1100
|
+
if (value === null) return false;
|
|
1101
|
+
if (value === "true" || value === 1) return true;
|
|
1102
|
+
if (value === "false" || value === 0) return false;
|
|
1103
|
+
return value;
|
|
1104
|
+
case "string":
|
|
1105
|
+
if (value === null) return "";
|
|
1106
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
1107
|
+
return value;
|
|
1108
|
+
case "null": return value === "" || value === 0 || value === false ? null : value;
|
|
1109
|
+
default: return value;
|
|
976
1110
|
}
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
if (keys.length !== Object.keys(y).length) return false;
|
|
981
|
-
for (const k of keys) if (!Object.hasOwn(y, k) || !deepEqual(x[k], y[k])) return false;
|
|
982
|
-
return true;
|
|
1111
|
+
}
|
|
1112
|
+
function isPlainObject(value) {
|
|
1113
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
983
1114
|
}
|
|
984
1115
|
|
|
985
1116
|
//#endregion
|
|
986
|
-
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.
|
|
1117
|
+
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.6.0/node_modules/@earendil-works/pi-schema/dist/annotations.js
|
|
987
1118
|
/** Empty annotations. */
|
|
988
1119
|
function newAnnotations() {
|
|
989
1120
|
return {
|
|
@@ -1147,7 +1278,7 @@ function unevaluatedItems(check) {
|
|
|
1147
1278
|
}
|
|
1148
1279
|
|
|
1149
1280
|
//#endregion
|
|
1150
|
-
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.
|
|
1281
|
+
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.6.0/node_modules/@earendil-works/pi-schema/dist/compiler.js
|
|
1151
1282
|
/** Thrown when a compile without dynamic scope tracking meets a bookended `$dynamicRef`: compile again with it. */
|
|
1152
1283
|
const RESTART = Symbol("restart");
|
|
1153
1284
|
/** One compile of a schema. Its caches and indexes are garbage once the root check is built. */
|
|
@@ -1218,8 +1349,9 @@ var Compiler = class {
|
|
|
1218
1349
|
} else {
|
|
1219
1350
|
const string = this.stringGroup(node, single === "string");
|
|
1220
1351
|
if (string !== void 0) parts.push(string);
|
|
1221
|
-
const numeric = single === "number" || single === "integer";
|
|
1222
|
-
const
|
|
1352
|
+
const numeric = single === "number" || single === "integer" || single === "bigint";
|
|
1353
|
+
const mode = single === "number" ? 1 : single === "integer" ? 2 : single === "bigint" ? 3 : 0;
|
|
1354
|
+
const number$1 = this.numberGroup(node, mode);
|
|
1223
1355
|
if (number$1 !== void 0) parts.push(number$1);
|
|
1224
1356
|
const object = this.objectGroup(node, base, single === "object");
|
|
1225
1357
|
if (object !== void 0) parts.push(object);
|
|
@@ -1248,7 +1380,7 @@ var Compiler = class {
|
|
|
1248
1380
|
}
|
|
1249
1381
|
union(branches, base, exactlyOne) {
|
|
1250
1382
|
const checks = () => branches.map((s) => this.check(s, base));
|
|
1251
|
-
return constUnion(branches, exactlyOne) ?? this.tagged(branches, base) ?? (exactlyOne ? oneCheck(checks()) :
|
|
1383
|
+
return constUnion(branches, exactlyOne) ?? this.tagged(branches, base) ?? (exactlyOne ? oneCheck(checks()) : anyOf(checks()));
|
|
1252
1384
|
}
|
|
1253
1385
|
ref(ref, base) {
|
|
1254
1386
|
const target = this.r.resolve(ref, base);
|
|
@@ -1324,11 +1456,11 @@ var Compiler = class {
|
|
|
1324
1456
|
return stringCheck(strict, min, max, re, format);
|
|
1325
1457
|
}
|
|
1326
1458
|
numberGroup(node, mode) {
|
|
1327
|
-
const min =
|
|
1328
|
-
const max =
|
|
1329
|
-
const xmin =
|
|
1330
|
-
const xmax =
|
|
1331
|
-
const multipleOf =
|
|
1459
|
+
const min = bound(node.minimum);
|
|
1460
|
+
const max = bound(node.maximum);
|
|
1461
|
+
const xmin = bound(node.exclusiveMinimum);
|
|
1462
|
+
const xmax = bound(node.exclusiveMaximum);
|
|
1463
|
+
const multipleOf = bound(node.multipleOf);
|
|
1332
1464
|
if ([
|
|
1333
1465
|
min,
|
|
1334
1466
|
max,
|
|
@@ -1372,8 +1504,9 @@ var Compiler = class {
|
|
|
1372
1504
|
const entries$1 = [];
|
|
1373
1505
|
for (const k of keys) {
|
|
1374
1506
|
const check = this.check(properties[k], base);
|
|
1375
|
-
const
|
|
1376
|
-
|
|
1507
|
+
const leaf$1 = leafOf(check);
|
|
1508
|
+
const flags = (leaf$1?.[0] ?? 0) | (required.includes(k) ? REQUIRED : 0) | (k in Object.prototype ? INHERITED : 0);
|
|
1509
|
+
entries$1.push(k, leaf$1 === void 0 ? check : leaf$1[1], flags);
|
|
1377
1510
|
}
|
|
1378
1511
|
const extraRequired = required.filter((k) => !Object.hasOwn(properties, k));
|
|
1379
1512
|
const pats = patternKeys.map((p) => [sharedRegex(p), this.check(patterns[p], base)]);
|
|
@@ -1545,6 +1678,18 @@ function constUnion(branches, exactlyOne) {
|
|
|
1545
1678
|
}
|
|
1546
1679
|
return enumCheck(values);
|
|
1547
1680
|
}
|
|
1681
|
+
/** `anyOf`: its bare-type branches (`nullable` gives one) run as one type test. */
|
|
1682
|
+
function anyOf(checks) {
|
|
1683
|
+
const types = [];
|
|
1684
|
+
const rest = [];
|
|
1685
|
+
for (const check of checks) {
|
|
1686
|
+
const type = BARE_TYPES.get(check);
|
|
1687
|
+
if (type === void 0) rest.push(check);
|
|
1688
|
+
else types.push(type);
|
|
1689
|
+
}
|
|
1690
|
+
return types.length === 0 ? anyCheck(rest) : maskedAnyCheck(typeMask(types), rest);
|
|
1691
|
+
}
|
|
1692
|
+
const BARE_TYPES = new Map([...TYPE_CHECKS].map(([type, check]) => [check, type]));
|
|
1548
1693
|
function tagOf(branch, key) {
|
|
1549
1694
|
const properties = branch.properties;
|
|
1550
1695
|
if (branch.type !== "object" || !Array.isArray(branch.required) || !branch.required.includes(key)) return void 0;
|
|
@@ -1552,6 +1697,10 @@ function tagOf(branch, key) {
|
|
|
1552
1697
|
if (!isSchemaObject(tag) || !Object.hasOwn(tag, "const")) return void 0;
|
|
1553
1698
|
return typeof tag.const === "object" && tag.const !== null ? void 0 : tag.const;
|
|
1554
1699
|
}
|
|
1700
|
+
/** A number keyword's value: a number, or a bigint (TypeBox's `Type.BigInt` bounds). */
|
|
1701
|
+
function bound(value) {
|
|
1702
|
+
return typeof value === "number" || typeof value === "bigint" ? value : void 0;
|
|
1703
|
+
}
|
|
1555
1704
|
function number(value) {
|
|
1556
1705
|
return typeof value === "number" ? value : void 0;
|
|
1557
1706
|
}
|
|
@@ -1560,7 +1709,7 @@ function entries(value) {
|
|
|
1560
1709
|
}
|
|
1561
1710
|
|
|
1562
1711
|
//#endregion
|
|
1563
|
-
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.
|
|
1712
|
+
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.6.0/node_modules/@earendil-works/pi-schema/dist/explain.js
|
|
1564
1713
|
/** Explains failures of one compiled schema. */
|
|
1565
1714
|
var Explainer = class {
|
|
1566
1715
|
c;
|
|
@@ -1629,7 +1778,7 @@ var Explainer = class {
|
|
|
1629
1778
|
if (Object.hasOwn(node, "const") && !constCheck(node.const)(v)) push("const", `expected ${JSON.stringify(node.const)}`);
|
|
1630
1779
|
if (typeof v === "string" && node.type !== "bytes") this.walkString(node, v, push);
|
|
1631
1780
|
if (v instanceof Uint8Array && node.type === "bytes") this.walkBytes(node, v, push);
|
|
1632
|
-
if (typeof v === "number") this.walkNumber(node, v, push);
|
|
1781
|
+
if (typeof v === "number" || typeof v === "bigint") this.walkNumber(node, v, push);
|
|
1633
1782
|
if (isObject(v)) this.walkObject(node, b, v, path, out, push);
|
|
1634
1783
|
if (Array.isArray(v)) this.walkArray(node, b, v, path, out, push);
|
|
1635
1784
|
if (Array.isArray(node.allOf)) for (const s of node.allOf) this.walk(s, b, v, path, out);
|
|
@@ -1691,16 +1840,21 @@ var Explainer = class {
|
|
|
1691
1840
|
if (typeof node.maxLength === "number" && v.length > node.maxLength) push("maxLength", `expected at most ${node.maxLength} bytes`);
|
|
1692
1841
|
}
|
|
1693
1842
|
walkNumber(node, v, push) {
|
|
1694
|
-
|
|
1695
|
-
if (
|
|
1696
|
-
if (
|
|
1697
|
-
if (
|
|
1698
|
-
if (
|
|
1843
|
+
const { minimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf } = node;
|
|
1844
|
+
if (isBound(minimum) && v < minimum) push("minimum", `expected >= ${minimum}`);
|
|
1845
|
+
if (isBound(maximum) && v > maximum) push("maximum", `expected <= ${maximum}`);
|
|
1846
|
+
if (isBound(exclusiveMinimum) && v <= exclusiveMinimum) push("exclusiveMinimum", `expected > ${exclusiveMinimum}`);
|
|
1847
|
+
if (isBound(exclusiveMaximum) && v >= exclusiveMaximum) push("exclusiveMaximum", `expected < ${exclusiveMaximum}`);
|
|
1848
|
+
if (isBound(multipleOf) && !isMultiple(v, multipleOf)) push("multipleOf", `expected a multiple of ${multipleOf}`);
|
|
1699
1849
|
}
|
|
1700
1850
|
walkObject(node, b, v, path, out, push) {
|
|
1701
1851
|
const properties = isSchemaObject(node.properties) ? node.properties : {};
|
|
1702
1852
|
const required = Array.isArray(node.required) ? node.required : [];
|
|
1703
|
-
for (const k of required)
|
|
1853
|
+
for (const k of required) {
|
|
1854
|
+
if (typeof k !== "string") continue;
|
|
1855
|
+
const accepted = v[k] === void 0 && Object.hasOwn(properties, k) && this.ok(properties[k], b, void 0);
|
|
1856
|
+
if (!Object.hasOwn(v, k) || v[k] === void 0 && !accepted) push("required", "missing", pointer(path, k));
|
|
1857
|
+
}
|
|
1704
1858
|
for (const k of Object.keys(properties)) if (Object.hasOwn(v, k) && v[k] !== void 0) this.walk(properties[k], b, v[k], pointer(path, k), out);
|
|
1705
1859
|
const patterns = isSchemaObject(node.patternProperties) ? Object.entries(node.patternProperties) : [];
|
|
1706
1860
|
for (const k of Object.keys(v)) {
|
|
@@ -1795,12 +1949,12 @@ var Explainer = class {
|
|
|
1795
1949
|
}
|
|
1796
1950
|
/** How many of the keys of `v` the branch `s` (through a `$ref`) declares in `properties`. */
|
|
1797
1951
|
declaredKeys(s, b, v) {
|
|
1798
|
-
const node = isSchemaObject(s) && typeof s.$ref === "string" ? this.c.r.resolve(s.$ref, b).node : s;
|
|
1952
|
+
const node = isSchemaObject(s) && typeof s.$ref === "string" ? this.c.r.resolve(s.$ref, this.c.r.baseFor(s, b)).node : s;
|
|
1799
1953
|
const properties = isSchemaObject(node) && isSchemaObject(node.properties) ? node.properties : {};
|
|
1800
1954
|
return Object.keys(v).filter((k) => Object.hasOwn(properties, k)).length;
|
|
1801
1955
|
}
|
|
1802
1956
|
tagOf(branches, b) {
|
|
1803
|
-
const resolved = branches.map((s) => isSchemaObject(s) && typeof s.$ref === "string" ? this.c.r.resolve(s.$ref, b).node : s);
|
|
1957
|
+
const resolved = branches.map((s) => isSchemaObject(s) && typeof s.$ref === "string" ? this.c.r.resolve(s.$ref, this.c.r.baseFor(s, b)).node : s);
|
|
1804
1958
|
const first = resolved[0];
|
|
1805
1959
|
if (!isSchemaObject(first) || !isSchemaObject(first.properties)) return void 0;
|
|
1806
1960
|
for (const key of Object.keys(first.properties)) {
|
|
@@ -1815,7 +1969,7 @@ var Explainer = class {
|
|
|
1815
1969
|
}
|
|
1816
1970
|
}
|
|
1817
1971
|
walkUnevaluated(node, b, v, path, out) {
|
|
1818
|
-
const { unevaluatedProperties: unevaluatedProperties$1, unevaluatedItems: unevaluatedItems$1,...rest } = node;
|
|
1972
|
+
const { unevaluatedProperties: unevaluatedProperties$1, unevaluatedItems: unevaluatedItems$1, $id: _id,...rest } = node;
|
|
1819
1973
|
const a = newAnnotations();
|
|
1820
1974
|
this.c.annotating(rest, b)(v, a);
|
|
1821
1975
|
if (isObject(v) && unevaluatedProperties$1 !== void 0 && !a.allProps) for (const k of Object.keys(v)) {
|
|
@@ -1849,6 +2003,9 @@ function containsMessage(min, max) {
|
|
|
1849
2003
|
if (min === 0) return `expected at most ${max} matching ${max === 1 ? "item" : "items"}`;
|
|
1850
2004
|
return `expected between ${min} and ${max} matching items`;
|
|
1851
2005
|
}
|
|
2006
|
+
function isBound(value) {
|
|
2007
|
+
return typeof value === "number" || typeof value === "bigint";
|
|
2008
|
+
}
|
|
1852
2009
|
function pointer(path, key) {
|
|
1853
2010
|
return `${path}/${String(key).replace(/~/g, "~0").replace(/\//g, "~1")}`;
|
|
1854
2011
|
}
|
|
@@ -1856,12 +2013,14 @@ function typeName(v) {
|
|
|
1856
2013
|
if (v === null) return "null";
|
|
1857
2014
|
if (Array.isArray(v)) return "array";
|
|
1858
2015
|
if (v instanceof Uint8Array) return "bytes";
|
|
2016
|
+
if (typeof v === "bigint") return "bigint";
|
|
1859
2017
|
if (typeof v === "number" && Number.isInteger(v)) return "integer";
|
|
2018
|
+
if (typeof v === "number" && !Number.isFinite(v)) return String(v);
|
|
1860
2019
|
return typeof v;
|
|
1861
2020
|
}
|
|
1862
2021
|
|
|
1863
2022
|
//#endregion
|
|
1864
|
-
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.
|
|
2023
|
+
//#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.6.0/node_modules/@earendil-works/pi-schema/dist/validator.js
|
|
1865
2024
|
function compile(schema, options = NO_OPTIONS) {
|
|
1866
2025
|
const check = compileCheck(schema, options);
|
|
1867
2026
|
return options.coerce === true ? new CoercingValidator(schema, options, check) : new Validator(schema, options, check);
|
|
@@ -1888,6 +2047,23 @@ var Validator = class {
|
|
|
1888
2047
|
this.explainer ??= new Explainer(compiler(this.schema, this.options), this.schema);
|
|
1889
2048
|
return this.explainer.errors(value);
|
|
1890
2049
|
}
|
|
2050
|
+
/**
|
|
2051
|
+
* `value` itself when it is valid, else its errors: check() and errors() in one result, without coercion.
|
|
2052
|
+
*
|
|
2053
|
+
* Not TypeBox's `Value.Parse`, which also clones, fills defaults, converts and strips undeclared properties before
|
|
2054
|
+
* it checks. This only checks: the strict replacement where `Value.Parse` is used to validate (its other steps
|
|
2055
|
+
* change nothing for a valid value of a schema without defaults or extra properties). Use `parse()` with
|
|
2056
|
+
* `coerce: true` for conversion.
|
|
2057
|
+
*/
|
|
2058
|
+
validate(value) {
|
|
2059
|
+
return this.check(value) ? {
|
|
2060
|
+
ok: true,
|
|
2061
|
+
value
|
|
2062
|
+
} : {
|
|
2063
|
+
ok: false,
|
|
2064
|
+
errors: this.errors(value)
|
|
2065
|
+
};
|
|
2066
|
+
}
|
|
1891
2067
|
};
|
|
1892
2068
|
/** A validator compiled with `coerce: true`: it also coerces loosely typed input towards the schema. */
|
|
1893
2069
|
var CoercingValidator = class extends Validator {
|
|
@@ -1904,14 +2080,7 @@ var CoercingValidator = class extends Validator {
|
|
|
1904
2080
|
}
|
|
1905
2081
|
/** Coerces `value`, then checks it: the coerced value when it is valid, else its errors. */
|
|
1906
2082
|
parse(value) {
|
|
1907
|
-
|
|
1908
|
-
return this.check(coerced) ? {
|
|
1909
|
-
ok: true,
|
|
1910
|
-
value: coerced
|
|
1911
|
-
} : {
|
|
1912
|
-
ok: false,
|
|
1913
|
-
errors: this.errors(coerced)
|
|
1914
|
-
};
|
|
2083
|
+
return this.validate(this.coerce(value));
|
|
1915
2084
|
}
|
|
1916
2085
|
};
|
|
1917
2086
|
function compileCheck(schema, options) {
|