@orpc/json-schema 2.0.0-beta.49 → 2.0.0-beta.50
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/coercer.d.ts +26 -26
- package/dist/coercer.js +316 -460
- package/dist/composition-utils.d.ts +4 -8
- package/dist/composition-utils.js +281 -351
- package/dist/constants.js +63 -62
- package/dist/convert.d.ts +16 -16
- package/dist/convert.js +12 -14
- package/dist/index.d.ts +11 -10
- package/dist/index.js +9 -9
- package/dist/ref-utils.d.ts +3 -7
- package/dist/ref-utils.js +178 -228
- package/dist/smart-coercion-handler-plugin.d.ts +13 -13
- package/dist/smart-coercion-handler-plugin.js +46 -49
- package/dist/smart-coercion-link-plugin.d.ts +19 -19
- package/dist/smart-coercion-link-plugin.js +62 -74
- package/dist/standard-json-schema-converter.d.ts +8 -8
- package/dist/standard-json-schema-converter.js +34 -43
- package/dist/types.d.ts +34 -36
- package/dist/types.js +42 -40
- package/dist/utils.d.ts +6 -10
- package/dist/utils.js +59 -91
- package/package.json +6 -6
- package/dist/constants.d.ts +0 -8
package/dist/coercer.js
CHANGED
|
@@ -1,498 +1,354 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
1
|
+
import { resolveJsonSchemaLocalPointer } from "./ref-utils.js";
|
|
2
|
+
import "./types.js";
|
|
3
|
+
import { DEFAULT_MAX_BIGINT_DIGITS, INTEGER_REGEX, getOwn, isNonPrimitive, isPlainObject, isPlainObjectOrArray, parseBigIntString, toArray, tryOrUndefined } from "@orpc/shared";
|
|
4
|
+
// src/coercer.ts
|
|
4
5
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
* How well a value matches a schema, used to pick the best union branch.
|
|
7
|
+
*/
|
|
7
8
|
const UNSATISFIED = 0;
|
|
8
9
|
/**
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
* Every keyword matches except some object keys or array items no schema describes.
|
|
11
|
+
* JSON Schema allows those, and validators usually strip or ignore them, but a branch
|
|
12
|
+
* that describes every key is still a better match.
|
|
13
|
+
*/
|
|
13
14
|
const LOOSELY_SATISFIED = 1;
|
|
14
15
|
/** Every keyword matches. */
|
|
15
16
|
const SATISFIED = 2;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
{
|
|
49
|
-
types: ['array'],
|
|
50
|
-
is: (value) => value instanceof Set,
|
|
51
|
-
coerce: (value) => (Array.isArray(value) ? arrayToSet(value) : value),
|
|
52
|
-
afterTypeCheck: true,
|
|
53
|
-
},
|
|
54
|
-
],
|
|
55
|
-
[
|
|
56
|
-
JsonSchemaXNativeType.Map,
|
|
57
|
-
{
|
|
58
|
-
types: ['array'],
|
|
59
|
-
is: (value) => value instanceof Map,
|
|
60
|
-
coerce: (value) => (Array.isArray(value) ? arrayToMap(value) : value),
|
|
61
|
-
afterTypeCheck: true,
|
|
62
|
-
},
|
|
63
|
-
],
|
|
17
|
+
const NATIVE_TYPES = /* @__PURE__ */ (() => /* @__PURE__ */ new Map([
|
|
18
|
+
["date", {
|
|
19
|
+
types: ["string"],
|
|
20
|
+
is: (value) => value instanceof Date,
|
|
21
|
+
coerce: (value) => typeof value === "string" ? stringToDate(value) : value
|
|
22
|
+
}],
|
|
23
|
+
["bigint", {
|
|
24
|
+
types: [
|
|
25
|
+
"string",
|
|
26
|
+
"integer",
|
|
27
|
+
"number"
|
|
28
|
+
],
|
|
29
|
+
is: (value) => typeof value === "bigint",
|
|
30
|
+
coerce: (value, { maxBigIntDigits }) => typeof value === "string" ? stringToBigInt(value, maxBigIntDigits) : typeof value === "number" ? numberToBigInt(value) : value
|
|
31
|
+
}],
|
|
32
|
+
["url", {
|
|
33
|
+
types: ["string"],
|
|
34
|
+
is: (value) => value instanceof URL,
|
|
35
|
+
coerce: (value) => typeof value === "string" ? stringToURL(value) : value
|
|
36
|
+
}],
|
|
37
|
+
["set", {
|
|
38
|
+
types: ["array"],
|
|
39
|
+
is: (value) => value instanceof Set,
|
|
40
|
+
coerce: (value) => Array.isArray(value) ? arrayToSet(value) : value,
|
|
41
|
+
afterTypeCheck: true
|
|
42
|
+
}],
|
|
43
|
+
["map", {
|
|
44
|
+
types: ["array"],
|
|
45
|
+
is: (value) => value instanceof Map,
|
|
46
|
+
coerce: (value) => Array.isArray(value) ? arrayToMap(value) : value,
|
|
47
|
+
afterTypeCheck: true
|
|
48
|
+
}]
|
|
64
49
|
]))();
|
|
65
50
|
function minSatisfaction(a, b) {
|
|
66
|
-
|
|
51
|
+
return a < b ? a : b;
|
|
67
52
|
}
|
|
68
53
|
const MIN_BUDGETED_VALUES = 100;
|
|
69
54
|
function createContext() {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
class CoercionBudgetExceededError extends Error {
|
|
80
|
-
}
|
|
81
|
-
export class JsonSchemaCoercer {
|
|
82
|
-
nativeCoerceOptions;
|
|
83
|
-
maxStepsPerValue;
|
|
84
|
-
/** Reused between calls, since building a context is slow until V8 warms up. */
|
|
85
|
-
idleContext = createContext();
|
|
86
|
-
constructor(options = {}) {
|
|
87
|
-
this.nativeCoerceOptions = {
|
|
88
|
-
maxBigIntDigits: options.maxBigIntDigits ?? DEFAULT_MAX_BIGINT_DIGITS,
|
|
89
|
-
};
|
|
90
|
-
this.maxStepsPerValue = options.maxStepsPerValue ?? 1_000;
|
|
91
|
-
}
|
|
92
|
-
coerce([schema, optional], value) {
|
|
93
|
-
if (optional && value === undefined) {
|
|
94
|
-
return value;
|
|
95
|
-
}
|
|
96
|
-
const context = this.idleContext ?? createContext();
|
|
97
|
-
this.idleContext = undefined;
|
|
98
|
-
context.rootSchema = schema;
|
|
99
|
-
context.memoize = false;
|
|
100
|
-
context.steps = 0;
|
|
101
|
-
context.maxSteps = this.maxStepsPerValue * MIN_BUDGETED_VALUES;
|
|
102
|
-
context.input = value;
|
|
103
|
-
try {
|
|
104
|
-
const [, coerced] = this.coerceInternal(context, schema, value);
|
|
105
|
-
return coerced;
|
|
106
|
-
}
|
|
107
|
-
catch (error) {
|
|
108
|
-
// partially coerced output would be harder to predict than the original input
|
|
109
|
-
if (error instanceof CoercionBudgetExceededError) {
|
|
110
|
-
return value;
|
|
111
|
-
}
|
|
112
|
-
throw error;
|
|
113
|
-
}
|
|
114
|
-
finally {
|
|
115
|
-
// keep no reference to this call's input or results
|
|
116
|
-
context.memo = undefined;
|
|
117
|
-
context.input = undefined;
|
|
118
|
-
this.idleContext = context;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
coerceInternal(context, schema, value,
|
|
122
|
-
/**
|
|
123
|
-
* Schemas already applied to this exact value, used to stop `$ref` cycles.
|
|
124
|
-
* Only shared between keywords that re-apply a schema to the same value, never with nested values.
|
|
125
|
-
*/
|
|
126
|
-
appliedRefs) {
|
|
127
|
-
if (++context.steps > context.maxSteps) {
|
|
128
|
-
this.extendBudget(context);
|
|
129
|
-
}
|
|
130
|
-
if (typeof schema === 'boolean') {
|
|
131
|
-
return [schema ? SATISFIED : UNSATISFIED, value];
|
|
132
|
-
}
|
|
133
|
-
if (Array.isArray(schema.type)) {
|
|
134
|
-
return this.coerceInternal(context, { anyOf: schema.type.map((type) => ({ ...schema, type })) }, value, appliedRefs);
|
|
135
|
-
}
|
|
136
|
-
let coerced = value;
|
|
137
|
-
let satisfied = SATISFIED;
|
|
138
|
-
if (typeof schema.$ref === 'string') {
|
|
139
|
-
const resolved = resolveJsonSchemaLocalPointer(context.rootSchema, schema.$ref);
|
|
140
|
-
if (resolved !== undefined && !appliedRefs?.has(resolved)) {
|
|
141
|
-
if (schema.type === 'object' || schema.type === 'array') {
|
|
142
|
-
context.memoize = true;
|
|
143
|
-
}
|
|
144
|
-
const [subSatisfied, subCoerced] = this.coerceRef(context, resolved, coerced, appliedRefs);
|
|
145
|
-
coerced = subCoerced;
|
|
146
|
-
satisfied = minSatisfaction(satisfied, subSatisfied);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
const enumValues = schema.const !== undefined ? [schema.const] : schema.enum;
|
|
150
|
-
if (enumValues !== undefined && !enumValues.includes(coerced)) {
|
|
151
|
-
if (typeof coerced === 'string') {
|
|
152
|
-
const numberValue = stringToNumber(coerced);
|
|
153
|
-
if (enumValues.includes(numberValue)) {
|
|
154
|
-
coerced = numberValue;
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
const booleanValue = stringToBoolean(coerced);
|
|
158
|
-
if (enumValues.includes(booleanValue)) {
|
|
159
|
-
coerced = booleanValue;
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
satisfied = UNSATISFIED;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
satisfied = UNSATISFIED;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
const declaredNativeType = NATIVE_TYPES.get(schema['x-native-type']);
|
|
171
|
-
const nativeType = schema.type === undefined || declaredNativeType?.types.includes(schema.type)
|
|
172
|
-
? declaredNativeType
|
|
173
|
-
: undefined;
|
|
174
|
-
if (nativeType && !nativeType.afterTypeCheck) {
|
|
175
|
-
coerced = nativeType.coerce(coerced, this.nativeCoerceOptions);
|
|
176
|
-
}
|
|
177
|
-
if (schema.type && !nativeType?.is(coerced)) {
|
|
178
|
-
switch (schema.type) {
|
|
179
|
-
case 'null': {
|
|
180
|
-
if (coerced !== null) {
|
|
181
|
-
satisfied = UNSATISFIED;
|
|
182
|
-
}
|
|
183
|
-
break;
|
|
184
|
-
}
|
|
185
|
-
case 'string': {
|
|
186
|
-
if (typeof coerced !== 'string') {
|
|
187
|
-
satisfied = UNSATISFIED;
|
|
188
|
-
}
|
|
189
|
-
break;
|
|
190
|
-
}
|
|
191
|
-
case 'number': {
|
|
192
|
-
if (typeof coerced === 'string') {
|
|
193
|
-
coerced = stringToNumber(coerced);
|
|
194
|
-
}
|
|
195
|
-
if (typeof coerced !== 'number') {
|
|
196
|
-
satisfied = UNSATISFIED;
|
|
197
|
-
}
|
|
198
|
-
break;
|
|
199
|
-
}
|
|
200
|
-
case 'integer': {
|
|
201
|
-
if (typeof coerced === 'string') {
|
|
202
|
-
coerced = stringToInteger(coerced);
|
|
203
|
-
}
|
|
204
|
-
if (typeof coerced !== 'number' || !Number.isInteger(coerced)) {
|
|
205
|
-
satisfied = UNSATISFIED;
|
|
206
|
-
}
|
|
207
|
-
break;
|
|
208
|
-
}
|
|
209
|
-
case 'boolean': {
|
|
210
|
-
if (typeof coerced === 'string') {
|
|
211
|
-
coerced = stringToBoolean(coerced);
|
|
212
|
-
}
|
|
213
|
-
if (typeof coerced !== 'boolean') {
|
|
214
|
-
satisfied = UNSATISFIED;
|
|
215
|
-
}
|
|
216
|
-
break;
|
|
217
|
-
}
|
|
218
|
-
case 'array': {
|
|
219
|
-
if (Array.isArray(coerced)) {
|
|
220
|
-
const prefixItemSchemas = 'prefixItems' in schema
|
|
221
|
-
? toArray(schema.prefixItems)
|
|
222
|
-
: Array.isArray(schema.items)
|
|
223
|
-
? schema.items
|
|
224
|
-
: [];
|
|
225
|
-
const itemSchema = Array.isArray(schema.items)
|
|
226
|
-
? schema.additionalItems
|
|
227
|
-
: schema.items;
|
|
228
|
-
let shouldUseCoercedItems = false;
|
|
229
|
-
const coercedItems = coerced.map((item, i) => {
|
|
230
|
-
const subSchema = prefixItemSchemas[i] ?? itemSchema;
|
|
231
|
-
if (subSchema === undefined) {
|
|
232
|
-
satisfied = minSatisfaction(satisfied, LOOSELY_SATISFIED);
|
|
233
|
-
return item;
|
|
234
|
-
}
|
|
235
|
-
const [subSatisfied, subCoerced] = this.coerceInternal(context, subSchema, item);
|
|
236
|
-
satisfied = minSatisfaction(satisfied, subSatisfied);
|
|
237
|
-
if (subCoerced !== item) {
|
|
238
|
-
shouldUseCoercedItems = true;
|
|
239
|
-
}
|
|
240
|
-
return subCoerced;
|
|
241
|
-
});
|
|
242
|
-
if (typeof schema.minItems === 'number' && coercedItems.length < schema.minItems) {
|
|
243
|
-
satisfied = UNSATISFIED;
|
|
244
|
-
}
|
|
245
|
-
if (shouldUseCoercedItems) {
|
|
246
|
-
coerced = coercedItems;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
else {
|
|
250
|
-
satisfied = UNSATISFIED;
|
|
251
|
-
}
|
|
252
|
-
break;
|
|
253
|
-
}
|
|
254
|
-
case 'object': {
|
|
255
|
-
if (Array.isArray(coerced)) {
|
|
256
|
-
// oxlint-disable-next-line typescript/no-misused-spread
|
|
257
|
-
coerced = { ...coerced };
|
|
258
|
-
}
|
|
259
|
-
if (isPlainObject(coerced)) {
|
|
260
|
-
let shouldUseCoercedItems = false;
|
|
261
|
-
// copy here so special keys like `__proto__` are kept as own properties
|
|
262
|
-
const coercedItems = { ...coerced };
|
|
263
|
-
const patternProperties = Object.entries(schema.patternProperties ?? {}).flatMap(([key, value]) => {
|
|
264
|
-
// an invalid pattern is a schema mistake, it should not break coercion of the other keys
|
|
265
|
-
const pattern = tryOrUndefined(() => new RegExp(key));
|
|
266
|
-
return pattern ? [[pattern, value]] : [];
|
|
267
|
-
});
|
|
268
|
-
const propertySchemas = schema.properties ?? {};
|
|
269
|
-
for (const key of Object.keys(coerced)) {
|
|
270
|
-
const value = coerced[key];
|
|
271
|
-
const subSchema = getOwn(propertySchemas, key) ??
|
|
272
|
-
patternProperties.find(([pattern]) => pattern.test(key))?.[1] ??
|
|
273
|
-
schema.additionalProperties;
|
|
274
|
-
if (value !== undefined || schema.required?.includes(key)) {
|
|
275
|
-
if (subSchema === undefined) {
|
|
276
|
-
satisfied = minSatisfaction(satisfied, LOOSELY_SATISFIED);
|
|
277
|
-
}
|
|
278
|
-
else {
|
|
279
|
-
const [subSatisfied, subCoerced] = this.coerceInternal(context, subSchema, value);
|
|
280
|
-
coercedItems[key] = subCoerced;
|
|
281
|
-
satisfied = minSatisfaction(satisfied, subSatisfied);
|
|
282
|
-
if (subCoerced !== value) {
|
|
283
|
-
shouldUseCoercedItems = true;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
if (schema.required?.some((key) => !Object.hasOwn(coercedItems, key))) {
|
|
289
|
-
satisfied = UNSATISFIED;
|
|
290
|
-
}
|
|
291
|
-
if (shouldUseCoercedItems) {
|
|
292
|
-
coerced = coercedItems;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
else {
|
|
296
|
-
satisfied = UNSATISFIED;
|
|
297
|
-
}
|
|
298
|
-
break;
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
if (nativeType) {
|
|
303
|
-
if (nativeType.afterTypeCheck) {
|
|
304
|
-
coerced = nativeType.coerce(coerced, this.nativeCoerceOptions);
|
|
305
|
-
}
|
|
306
|
-
if (!nativeType.is(coerced)) {
|
|
307
|
-
satisfied = UNSATISFIED;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
if (schema.allOf) {
|
|
311
|
-
context.memoize = true;
|
|
312
|
-
for (const subSchema of schema.allOf) {
|
|
313
|
-
const [subSatisfied, subCoerced] = this.coerceInternal(context, subSchema, coerced, appliedRefs);
|
|
314
|
-
coerced = subCoerced;
|
|
315
|
-
satisfied = minSatisfaction(satisfied, subSatisfied);
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
for (const key of ['anyOf', 'oneOf']) {
|
|
319
|
-
if (schema[key]) {
|
|
320
|
-
context.memoize = true;
|
|
321
|
-
/**
|
|
322
|
-
* The best branch is the first one that accepts the value untouched,
|
|
323
|
-
* then the most satisfied branch that accepts it after a conversion.
|
|
324
|
-
*/
|
|
325
|
-
let best;
|
|
326
|
-
for (const subSchema of schema[key]) {
|
|
327
|
-
const [subSatisfied, subCoerced] = this.coerceInternal(context, subSchema, coerced, appliedRefs);
|
|
328
|
-
if (subSatisfied === UNSATISFIED) {
|
|
329
|
-
continue;
|
|
330
|
-
}
|
|
331
|
-
if (subCoerced === coerced) {
|
|
332
|
-
best = { satisfied: subSatisfied, value: subCoerced };
|
|
333
|
-
break;
|
|
334
|
-
}
|
|
335
|
-
if (!best || subSatisfied > best.satisfied) {
|
|
336
|
-
best = { satisfied: subSatisfied, value: subCoerced };
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
// a matching branch never repairs a sibling keyword that already failed, so satisfaction only decreases
|
|
340
|
-
if (best) {
|
|
341
|
-
coerced = best.value;
|
|
342
|
-
satisfied = minSatisfaction(satisfied, best.satisfied);
|
|
343
|
-
}
|
|
344
|
-
else {
|
|
345
|
-
satisfied = UNSATISFIED;
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
if (typeof schema.not !== 'undefined') {
|
|
350
|
-
context.memoize = true;
|
|
351
|
-
const [notSatisfied] = this.coerceInternal(context, schema.not, coerced, appliedRefs);
|
|
352
|
-
if (notSatisfied !== UNSATISFIED) {
|
|
353
|
-
satisfied = UNSATISFIED;
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
return [satisfied, coerced];
|
|
357
|
-
}
|
|
358
|
-
coerceRef(context, resolved, value, appliedRefs) {
|
|
359
|
-
let memo;
|
|
360
|
-
// while another `$ref` is applied to the value, the cycle check changes the result
|
|
361
|
-
if (context.memoize && !appliedRefs?.size && isNonPrimitive(value)) {
|
|
362
|
-
context.memo ??= new Map();
|
|
363
|
-
memo = context.memo.get(resolved);
|
|
364
|
-
if (!memo) {
|
|
365
|
-
memo = new Map();
|
|
366
|
-
context.memo.set(resolved, memo);
|
|
367
|
-
}
|
|
368
|
-
const cached = memo.get(value);
|
|
369
|
-
if (cached) {
|
|
370
|
-
return cached;
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
appliedRefs ??= new Set();
|
|
374
|
-
appliedRefs.add(resolved);
|
|
375
|
-
const result = this.coerceInternal(context, resolved, value, appliedRefs);
|
|
376
|
-
appliedRefs.delete(resolved);
|
|
377
|
-
memo?.set(value, result);
|
|
378
|
-
return result;
|
|
379
|
-
}
|
|
380
|
-
extendBudget(context) {
|
|
381
|
-
const limit = (2 * context.steps) / this.maxStepsPerValue;
|
|
382
|
-
context.maxSteps =
|
|
383
|
-
this.maxStepsPerValue * Math.max(countValues(context.input, limit), MIN_BUDGETED_VALUES);
|
|
384
|
-
if (context.steps > context.maxSteps) {
|
|
385
|
-
throw new CoercionBudgetExceededError();
|
|
386
|
-
}
|
|
387
|
-
}
|
|
55
|
+
return {
|
|
56
|
+
rootSchema: true,
|
|
57
|
+
memo: void 0,
|
|
58
|
+
memoize: false,
|
|
59
|
+
steps: 0,
|
|
60
|
+
maxSteps: 0,
|
|
61
|
+
input: void 0
|
|
62
|
+
};
|
|
388
63
|
}
|
|
64
|
+
var CoercionBudgetExceededError = class extends Error {};
|
|
65
|
+
var JsonSchemaCoercer = class {
|
|
66
|
+
nativeCoerceOptions;
|
|
67
|
+
maxStepsPerValue;
|
|
68
|
+
/** Reused between calls, since building a context is slow until V8 warms up. */
|
|
69
|
+
idleContext = createContext();
|
|
70
|
+
constructor(options = {}) {
|
|
71
|
+
this.nativeCoerceOptions = { maxBigIntDigits: options.maxBigIntDigits ?? DEFAULT_MAX_BIGINT_DIGITS };
|
|
72
|
+
this.maxStepsPerValue = options.maxStepsPerValue ?? 1e3;
|
|
73
|
+
}
|
|
74
|
+
coerce([schema, optional], value) {
|
|
75
|
+
if (optional && value === void 0) return value;
|
|
76
|
+
const context = this.idleContext ?? createContext();
|
|
77
|
+
this.idleContext = void 0;
|
|
78
|
+
context.rootSchema = schema;
|
|
79
|
+
context.memoize = false;
|
|
80
|
+
context.steps = 0;
|
|
81
|
+
context.maxSteps = this.maxStepsPerValue * MIN_BUDGETED_VALUES;
|
|
82
|
+
context.input = value;
|
|
83
|
+
try {
|
|
84
|
+
const [, coerced] = this.coerceInternal(context, schema, value);
|
|
85
|
+
return coerced;
|
|
86
|
+
} catch (error) {
|
|
87
|
+
if (error instanceof CoercionBudgetExceededError) return value;
|
|
88
|
+
throw error;
|
|
89
|
+
} finally {
|
|
90
|
+
context.memo = void 0;
|
|
91
|
+
context.input = void 0;
|
|
92
|
+
this.idleContext = context;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
coerceInternal(context, schema, value, appliedRefs) {
|
|
96
|
+
if (++context.steps > context.maxSteps) this.extendBudget(context);
|
|
97
|
+
if (typeof schema === "boolean") return [schema ? SATISFIED : UNSATISFIED, value];
|
|
98
|
+
if (Array.isArray(schema.type)) return this.coerceInternal(context, { anyOf: schema.type.map((type) => ({
|
|
99
|
+
...schema,
|
|
100
|
+
type
|
|
101
|
+
})) }, value, appliedRefs);
|
|
102
|
+
let coerced = value;
|
|
103
|
+
let satisfied = SATISFIED;
|
|
104
|
+
if (typeof schema.$ref === "string") {
|
|
105
|
+
const resolved = resolveJsonSchemaLocalPointer(context.rootSchema, schema.$ref);
|
|
106
|
+
if (resolved !== void 0 && !appliedRefs?.has(resolved)) {
|
|
107
|
+
if (schema.type === "object" || schema.type === "array") context.memoize = true;
|
|
108
|
+
const [subSatisfied, subCoerced] = this.coerceRef(context, resolved, coerced, appliedRefs);
|
|
109
|
+
coerced = subCoerced;
|
|
110
|
+
satisfied = minSatisfaction(satisfied, subSatisfied);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const enumValues = schema.const !== void 0 ? [schema.const] : schema.enum;
|
|
114
|
+
if (enumValues !== void 0 && !enumValues.includes(coerced)) {
|
|
115
|
+
if (typeof coerced === "string") {
|
|
116
|
+
const numberValue = stringToNumber(coerced);
|
|
117
|
+
if (enumValues.includes(numberValue)) coerced = numberValue;
|
|
118
|
+
else {
|
|
119
|
+
const booleanValue = stringToBoolean(coerced);
|
|
120
|
+
if (enumValues.includes(booleanValue)) coerced = booleanValue;
|
|
121
|
+
else satisfied = UNSATISFIED;
|
|
122
|
+
}
|
|
123
|
+
} else satisfied = UNSATISFIED;
|
|
124
|
+
}
|
|
125
|
+
const declaredNativeType = NATIVE_TYPES.get(schema["x-native-type"]);
|
|
126
|
+
const nativeType = schema.type === void 0 || declaredNativeType?.types.includes(schema.type) ? declaredNativeType : void 0;
|
|
127
|
+
if (nativeType && !nativeType.afterTypeCheck) coerced = nativeType.coerce(coerced, this.nativeCoerceOptions);
|
|
128
|
+
if (schema.type && !nativeType?.is(coerced)) switch (schema.type) {
|
|
129
|
+
case "null":
|
|
130
|
+
if (coerced !== null) satisfied = UNSATISFIED;
|
|
131
|
+
break;
|
|
132
|
+
case "string":
|
|
133
|
+
if (typeof coerced !== "string") satisfied = UNSATISFIED;
|
|
134
|
+
break;
|
|
135
|
+
case "number":
|
|
136
|
+
if (typeof coerced === "string") coerced = stringToNumber(coerced);
|
|
137
|
+
if (typeof coerced !== "number") satisfied = UNSATISFIED;
|
|
138
|
+
break;
|
|
139
|
+
case "integer":
|
|
140
|
+
if (typeof coerced === "string") coerced = stringToInteger(coerced);
|
|
141
|
+
if (typeof coerced !== "number" || !Number.isInteger(coerced)) satisfied = UNSATISFIED;
|
|
142
|
+
break;
|
|
143
|
+
case "boolean":
|
|
144
|
+
if (typeof coerced === "string") coerced = stringToBoolean(coerced);
|
|
145
|
+
if (typeof coerced !== "boolean") satisfied = UNSATISFIED;
|
|
146
|
+
break;
|
|
147
|
+
case "array":
|
|
148
|
+
if (Array.isArray(coerced)) {
|
|
149
|
+
const prefixItemSchemas = "prefixItems" in schema ? toArray(schema.prefixItems) : Array.isArray(schema.items) ? schema.items : [];
|
|
150
|
+
const itemSchema = Array.isArray(schema.items) ? schema.additionalItems : schema.items;
|
|
151
|
+
let shouldUseCoercedItems = false;
|
|
152
|
+
const coercedItems = coerced.map((item, i) => {
|
|
153
|
+
const subSchema = prefixItemSchemas[i] ?? itemSchema;
|
|
154
|
+
if (subSchema === void 0) {
|
|
155
|
+
satisfied = minSatisfaction(satisfied, LOOSELY_SATISFIED);
|
|
156
|
+
return item;
|
|
157
|
+
}
|
|
158
|
+
const [subSatisfied, subCoerced] = this.coerceInternal(context, subSchema, item);
|
|
159
|
+
satisfied = minSatisfaction(satisfied, subSatisfied);
|
|
160
|
+
if (subCoerced !== item) shouldUseCoercedItems = true;
|
|
161
|
+
return subCoerced;
|
|
162
|
+
});
|
|
163
|
+
if (typeof schema.minItems === "number" && coercedItems.length < schema.minItems) satisfied = UNSATISFIED;
|
|
164
|
+
if (shouldUseCoercedItems) coerced = coercedItems;
|
|
165
|
+
} else satisfied = UNSATISFIED;
|
|
166
|
+
break;
|
|
167
|
+
case "object":
|
|
168
|
+
if (Array.isArray(coerced)) coerced = { ...coerced };
|
|
169
|
+
if (isPlainObject(coerced)) {
|
|
170
|
+
let shouldUseCoercedItems = false;
|
|
171
|
+
const coercedItems = { ...coerced };
|
|
172
|
+
const patternProperties = Object.entries(schema.patternProperties ?? {}).flatMap(([key, value]) => {
|
|
173
|
+
const pattern = tryOrUndefined(() => new RegExp(key));
|
|
174
|
+
return pattern ? [[pattern, value]] : [];
|
|
175
|
+
});
|
|
176
|
+
const propertySchemas = schema.properties ?? {};
|
|
177
|
+
for (const key of Object.keys(coerced)) {
|
|
178
|
+
const value = coerced[key];
|
|
179
|
+
const subSchema = getOwn(propertySchemas, key) ?? patternProperties.find(([pattern]) => pattern.test(key))?.[1] ?? schema.additionalProperties;
|
|
180
|
+
if (value !== void 0 || schema.required?.includes(key)) {
|
|
181
|
+
if (subSchema === void 0) satisfied = minSatisfaction(satisfied, LOOSELY_SATISFIED);
|
|
182
|
+
else {
|
|
183
|
+
const [subSatisfied, subCoerced] = this.coerceInternal(context, subSchema, value);
|
|
184
|
+
coercedItems[key] = subCoerced;
|
|
185
|
+
satisfied = minSatisfaction(satisfied, subSatisfied);
|
|
186
|
+
if (subCoerced !== value) shouldUseCoercedItems = true;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (schema.required?.some((key) => !Object.hasOwn(coercedItems, key))) satisfied = UNSATISFIED;
|
|
191
|
+
if (shouldUseCoercedItems) coerced = coercedItems;
|
|
192
|
+
} else satisfied = UNSATISFIED;
|
|
193
|
+
}
|
|
194
|
+
if (nativeType) {
|
|
195
|
+
if (nativeType.afterTypeCheck) coerced = nativeType.coerce(coerced, this.nativeCoerceOptions);
|
|
196
|
+
if (!nativeType.is(coerced)) satisfied = UNSATISFIED;
|
|
197
|
+
}
|
|
198
|
+
if (schema.allOf) {
|
|
199
|
+
context.memoize = true;
|
|
200
|
+
for (const subSchema of schema.allOf) {
|
|
201
|
+
const [subSatisfied, subCoerced] = this.coerceInternal(context, subSchema, coerced, appliedRefs);
|
|
202
|
+
coerced = subCoerced;
|
|
203
|
+
satisfied = minSatisfaction(satisfied, subSatisfied);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
for (const key of ["anyOf", "oneOf"]) if (schema[key]) {
|
|
207
|
+
context.memoize = true;
|
|
208
|
+
/**
|
|
209
|
+
* The best branch is the first one that accepts the value untouched,
|
|
210
|
+
* then the most satisfied branch that accepts it after a conversion.
|
|
211
|
+
*/
|
|
212
|
+
let best;
|
|
213
|
+
for (const subSchema of schema[key]) {
|
|
214
|
+
const [subSatisfied, subCoerced] = this.coerceInternal(context, subSchema, coerced, appliedRefs);
|
|
215
|
+
if (subSatisfied === UNSATISFIED) continue;
|
|
216
|
+
if (subCoerced === coerced) {
|
|
217
|
+
best = {
|
|
218
|
+
satisfied: subSatisfied,
|
|
219
|
+
value: subCoerced
|
|
220
|
+
};
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
if (!best || subSatisfied > best.satisfied) best = {
|
|
224
|
+
satisfied: subSatisfied,
|
|
225
|
+
value: subCoerced
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
if (best) {
|
|
229
|
+
coerced = best.value;
|
|
230
|
+
satisfied = minSatisfaction(satisfied, best.satisfied);
|
|
231
|
+
} else satisfied = UNSATISFIED;
|
|
232
|
+
}
|
|
233
|
+
if (typeof schema.not !== "undefined") {
|
|
234
|
+
context.memoize = true;
|
|
235
|
+
const [notSatisfied] = this.coerceInternal(context, schema.not, coerced, appliedRefs);
|
|
236
|
+
if (notSatisfied !== UNSATISFIED) satisfied = UNSATISFIED;
|
|
237
|
+
}
|
|
238
|
+
return [satisfied, coerced];
|
|
239
|
+
}
|
|
240
|
+
coerceRef(context, resolved, value, appliedRefs) {
|
|
241
|
+
let memo;
|
|
242
|
+
if (context.memoize && !appliedRefs?.size && isNonPrimitive(value)) {
|
|
243
|
+
context.memo ??= /* @__PURE__ */ new Map();
|
|
244
|
+
memo = context.memo.get(resolved);
|
|
245
|
+
if (!memo) {
|
|
246
|
+
memo = /* @__PURE__ */ new Map();
|
|
247
|
+
context.memo.set(resolved, memo);
|
|
248
|
+
}
|
|
249
|
+
const cached = memo.get(value);
|
|
250
|
+
if (cached) return cached;
|
|
251
|
+
}
|
|
252
|
+
appliedRefs ??= /* @__PURE__ */ new Set();
|
|
253
|
+
appliedRefs.add(resolved);
|
|
254
|
+
const result = this.coerceInternal(context, resolved, value, appliedRefs);
|
|
255
|
+
appliedRefs.delete(resolved);
|
|
256
|
+
memo?.set(value, result);
|
|
257
|
+
return result;
|
|
258
|
+
}
|
|
259
|
+
extendBudget(context) {
|
|
260
|
+
const limit = 2 * context.steps / this.maxStepsPerValue;
|
|
261
|
+
context.maxSteps = this.maxStepsPerValue * Math.max(countValues(context.input, limit), MIN_BUDGETED_VALUES);
|
|
262
|
+
if (context.steps > context.maxSteps) throw new CoercionBudgetExceededError();
|
|
263
|
+
}
|
|
264
|
+
};
|
|
389
265
|
function countValues(input, limit) {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
return count;
|
|
266
|
+
const walked = /* @__PURE__ */ new Set();
|
|
267
|
+
const stack = [input];
|
|
268
|
+
let count = 1;
|
|
269
|
+
while (stack.length) {
|
|
270
|
+
const value = stack.pop();
|
|
271
|
+
if (isPlainObjectOrArray(value) && !walked.has(value)) {
|
|
272
|
+
walked.add(value);
|
|
273
|
+
const items = Array.isArray(value) ? value : Object.values(value);
|
|
274
|
+
for (let i = 0; i < items.length; i++) {
|
|
275
|
+
if (++count >= limit) return count;
|
|
276
|
+
const item = items[i];
|
|
277
|
+
if (isNonPrimitive(item)) stack.push(item);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return count;
|
|
410
282
|
}
|
|
411
283
|
const NUMBER_PATTERN = /^-?(?:0|[1-9]\d*)(?:\.\d+)?$/;
|
|
412
284
|
function stringToNumber(value) {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
// beyond the safe range digits get lost, `'12345678901234567890'` would become `12345678901234567168`
|
|
418
|
-
if (num < Number.MIN_SAFE_INTEGER || num > Number.MAX_SAFE_INTEGER) {
|
|
419
|
-
return value;
|
|
420
|
-
}
|
|
421
|
-
return num;
|
|
285
|
+
if (!NUMBER_PATTERN.test(value)) return value;
|
|
286
|
+
const num = Number(value);
|
|
287
|
+
if (num < Number.MIN_SAFE_INTEGER || num > Number.MAX_SAFE_INTEGER) return value;
|
|
288
|
+
return num;
|
|
422
289
|
}
|
|
423
|
-
const INTEGER_PATTERN = /^-?(?:0|[1-9]\d*)$/;
|
|
424
290
|
function stringToInteger(value) {
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
if (!Number.isSafeInteger(num)) {
|
|
430
|
-
return value;
|
|
431
|
-
}
|
|
432
|
-
return num;
|
|
291
|
+
if (!INTEGER_REGEX.test(value)) return value;
|
|
292
|
+
const num = Number(value);
|
|
293
|
+
if (!Number.isSafeInteger(num)) return value;
|
|
294
|
+
return num;
|
|
433
295
|
}
|
|
434
296
|
function stringToBigInt(value, maxDigits) {
|
|
435
|
-
|
|
297
|
+
return parseBigIntString(value, maxDigits) ?? value;
|
|
436
298
|
}
|
|
437
299
|
function numberToBigInt(value) {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
}
|
|
441
|
-
return BigInt(value);
|
|
300
|
+
if (!Number.isInteger(value)) return value;
|
|
301
|
+
return BigInt(value);
|
|
442
302
|
}
|
|
443
303
|
function stringToBoolean(value) {
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
if (lower === 'true' || lower === 'on') {
|
|
449
|
-
return true;
|
|
450
|
-
}
|
|
451
|
-
return value;
|
|
304
|
+
const lower = value.toLowerCase();
|
|
305
|
+
if (lower === "false" || lower === "off") return false;
|
|
306
|
+
if (lower === "true" || lower === "on") return true;
|
|
307
|
+
return value;
|
|
452
308
|
}
|
|
453
309
|
const DATE_TIME_PATTERN = /^(?!-0{6})(?:[+-]\d{2})?\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?)?$/;
|
|
454
|
-
const DAYS_IN_MONTH = [
|
|
310
|
+
const DAYS_IN_MONTH = [
|
|
311
|
+
31,
|
|
312
|
+
29,
|
|
313
|
+
31,
|
|
314
|
+
30,
|
|
315
|
+
31,
|
|
316
|
+
30,
|
|
317
|
+
31,
|
|
318
|
+
31,
|
|
319
|
+
30,
|
|
320
|
+
31,
|
|
321
|
+
30,
|
|
322
|
+
31
|
|
323
|
+
];
|
|
455
324
|
function stringToDate(value) {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
(month === 2 && day === 29 && !isLeapYear(Number(value.slice(0, yearEnd))))) {
|
|
465
|
-
return value;
|
|
466
|
-
}
|
|
467
|
-
const date = new Date(value);
|
|
468
|
-
if (Number.isNaN(date.getTime())) {
|
|
469
|
-
return value;
|
|
470
|
-
}
|
|
471
|
-
return date;
|
|
325
|
+
if (!DATE_TIME_PATTERN.test(value)) return value;
|
|
326
|
+
const yearEnd = value.indexOf("-", 1);
|
|
327
|
+
const month = readTwoDigits(value, yearEnd + 1);
|
|
328
|
+
const day = readTwoDigits(value, yearEnd + 4);
|
|
329
|
+
if (day < 1 || day > (DAYS_IN_MONTH[month - 1] ?? 0) || month === 2 && day === 29 && !isLeapYear(Number(value.slice(0, yearEnd)))) return value;
|
|
330
|
+
const date = new Date(value);
|
|
331
|
+
if (Number.isNaN(date.getTime())) return value;
|
|
332
|
+
return date;
|
|
472
333
|
}
|
|
473
334
|
function readTwoDigits(value, index) {
|
|
474
|
-
|
|
335
|
+
return (value.charCodeAt(index) - 48) * 10 + value.charCodeAt(index + 1) - 48;
|
|
475
336
|
}
|
|
476
337
|
function isLeapYear(year) {
|
|
477
|
-
|
|
338
|
+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
478
339
|
}
|
|
479
340
|
function stringToURL(value) {
|
|
480
|
-
|
|
341
|
+
return tryOrUndefined(() => new URL(value)) ?? value;
|
|
481
342
|
}
|
|
482
343
|
function arrayToSet(value) {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
}
|
|
487
|
-
return set;
|
|
344
|
+
const set = new Set(value);
|
|
345
|
+
if (set.size !== value.length) return value;
|
|
346
|
+
return set;
|
|
488
347
|
}
|
|
489
348
|
function arrayToMap(value) {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
if (result.size !== value.length) {
|
|
495
|
-
return value;
|
|
496
|
-
}
|
|
497
|
-
return result;
|
|
349
|
+
if (value.some((item) => !Array.isArray(item) || item.length !== 2)) return value;
|
|
350
|
+
const result = new Map(value);
|
|
351
|
+
if (result.size !== value.length) return value;
|
|
352
|
+
return result;
|
|
498
353
|
}
|
|
354
|
+
export { JsonSchemaCoercer };
|