@orpc/json-schema 2.0.0-beta.5 → 2.0.0-beta.51
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/README.md +139 -102
- package/dist/coercer.d.ts +29 -0
- package/dist/coercer.js +354 -0
- package/dist/composition-utils.d.ts +45 -0
- package/dist/composition-utils.js +335 -0
- package/dist/constants.js +67 -0
- package/dist/convert.d.ts +29 -0
- package/dist/convert.js +12 -0
- package/dist/index.d.ts +11 -187
- package/dist/index.js +9 -0
- package/dist/ref-utils.d.ts +55 -0
- package/dist/ref-utils.js +203 -0
- package/dist/smart-coercion-handler-plugin.d.ts +22 -0
- package/dist/smart-coercion-handler-plugin.js +47 -0
- package/dist/smart-coercion-link-plugin.d.ts +28 -0
- package/dist/smart-coercion-link-plugin.js +63 -0
- package/dist/standard-json-schema-converter.d.ts +16 -0
- package/dist/standard-json-schema-converter.js +36 -0
- package/dist/types.d.ts +51 -0
- package/dist/types.js +42 -0
- package/dist/utils.d.ts +41 -0
- package/dist/utils.js +74 -0
- package/package.json +29 -22
- package/dist/index.d.mts +0 -187
- package/dist/index.mjs +0 -997
package/dist/index.mjs
DELETED
|
@@ -1,997 +0,0 @@
|
|
|
1
|
-
import { get, isPlainObject, toArray, tryOrUndefined, isDeepEqual, omit, isTypescriptObject } from '@orpc/shared';
|
|
2
|
-
export { Format as JsonSchemaFormat, TypeName as JsonSchemaType } from 'json-schema-typed/draft-2020-12';
|
|
3
|
-
import { ORPCError, cloneORPCError } from '@orpc/client';
|
|
4
|
-
import { getProcedureContractOrThrow } from '@orpc/contract';
|
|
5
|
-
|
|
6
|
-
const JSON_SCHEMA_LOGIC_KEYWORDS = /* @__PURE__ */ new Set([
|
|
7
|
-
"$dynamicRef",
|
|
8
|
-
"$ref",
|
|
9
|
-
"additionalItems",
|
|
10
|
-
"additionalProperties",
|
|
11
|
-
"allOf",
|
|
12
|
-
"anyOf",
|
|
13
|
-
"const",
|
|
14
|
-
"contains",
|
|
15
|
-
"contentEncoding",
|
|
16
|
-
"contentMediaType",
|
|
17
|
-
"contentSchema",
|
|
18
|
-
"dependencies",
|
|
19
|
-
"dependentRequired",
|
|
20
|
-
"dependentSchemas",
|
|
21
|
-
"else",
|
|
22
|
-
"enum",
|
|
23
|
-
"exclusiveMaximum",
|
|
24
|
-
"exclusiveMinimum",
|
|
25
|
-
"format",
|
|
26
|
-
"if",
|
|
27
|
-
"items",
|
|
28
|
-
"maxContains",
|
|
29
|
-
"maximum",
|
|
30
|
-
"maxItems",
|
|
31
|
-
"maxLength",
|
|
32
|
-
"maxProperties",
|
|
33
|
-
"minContains",
|
|
34
|
-
"minimum",
|
|
35
|
-
"minItems",
|
|
36
|
-
"minLength",
|
|
37
|
-
"minProperties",
|
|
38
|
-
"multipleOf",
|
|
39
|
-
"not",
|
|
40
|
-
"oneOf",
|
|
41
|
-
"pattern",
|
|
42
|
-
"patternProperties",
|
|
43
|
-
"prefixItems",
|
|
44
|
-
"properties",
|
|
45
|
-
"propertyNames",
|
|
46
|
-
"required",
|
|
47
|
-
"then",
|
|
48
|
-
"type",
|
|
49
|
-
"unevaluatedItems",
|
|
50
|
-
"unevaluatedProperties",
|
|
51
|
-
"uniqueItems"
|
|
52
|
-
]);
|
|
53
|
-
const JSON_SCHEMA_PRIMITIVE_TYPES = /* @__PURE__ */ new Set(["string", "number", "integer", "boolean", "null"]);
|
|
54
|
-
const JSON_SCHEMA_RECORD_KEYWORDS = /* @__PURE__ */ new Set(["properties", "patternProperties", "dependentSchemas", "dependencies", "$defs"]);
|
|
55
|
-
|
|
56
|
-
function encodeJsonPointerSegment(segment) {
|
|
57
|
-
return segment.replaceAll("~", "~0").replaceAll("/", "~1");
|
|
58
|
-
}
|
|
59
|
-
function decodeJsonPointerSegment(segment) {
|
|
60
|
-
return segment.replaceAll("~1", "/").replaceAll("~0", "~");
|
|
61
|
-
}
|
|
62
|
-
function mapJsonSchemaRefs(value, map, schemaLevel = true, path = []) {
|
|
63
|
-
if (!value || typeof value !== "object") {
|
|
64
|
-
return value;
|
|
65
|
-
}
|
|
66
|
-
if (Array.isArray(value)) {
|
|
67
|
-
return value.map((item, index) => mapJsonSchemaRefs(item, map, schemaLevel, [...path, index]));
|
|
68
|
-
}
|
|
69
|
-
const result = {};
|
|
70
|
-
for (const [key, val] of Object.entries(value)) {
|
|
71
|
-
if (key === "$ref" && typeof val === "string") {
|
|
72
|
-
result[key] = map(val, [...path, key]);
|
|
73
|
-
} else if (!schemaLevel) {
|
|
74
|
-
result[key] = mapJsonSchemaRefs(val, map, true, [...path, key]);
|
|
75
|
-
} else if (JSON_SCHEMA_LOGIC_KEYWORDS.has(key) || JSON_SCHEMA_RECORD_KEYWORDS.has(key)) {
|
|
76
|
-
result[key] = mapJsonSchemaRefs(val, map, !JSON_SCHEMA_RECORD_KEYWORDS.has(key), [...path, key]);
|
|
77
|
-
} else {
|
|
78
|
-
result[key] = val;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return result;
|
|
82
|
-
}
|
|
83
|
-
function hoistRecursiveRefToDef(schema) {
|
|
84
|
-
if (typeof schema !== "object") {
|
|
85
|
-
return schema;
|
|
86
|
-
}
|
|
87
|
-
let defName;
|
|
88
|
-
const rewritten = mapJsonSchemaRefs(schema, (ref) => {
|
|
89
|
-
if (ref === "#" || ref.startsWith("#/") && !ref.startsWith("#/$defs/") && get(schema, ref.slice(2).split("/").map(decodeJsonPointerSegment)) !== void 0) {
|
|
90
|
-
defName ??= findRecursiveJsonSchemaDefName(schema.$defs);
|
|
91
|
-
return `#/$defs/${encodeJsonPointerSegment(defName)}${ref.slice(1)}`;
|
|
92
|
-
}
|
|
93
|
-
return ref;
|
|
94
|
-
});
|
|
95
|
-
if (defName === void 0) {
|
|
96
|
-
return schema;
|
|
97
|
-
}
|
|
98
|
-
const { $defs, ...rest } = rewritten;
|
|
99
|
-
return {
|
|
100
|
-
$ref: `#/$defs/${encodeJsonPointerSegment(defName)}`,
|
|
101
|
-
$defs: {
|
|
102
|
-
...$defs,
|
|
103
|
-
[defName]: rest
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
function resolveJsonSchemaRootLocalRef(schema, $defs) {
|
|
108
|
-
if (typeof schema === "boolean") {
|
|
109
|
-
return schema;
|
|
110
|
-
}
|
|
111
|
-
if (arguments.length === 1) {
|
|
112
|
-
$defs = schema.$defs;
|
|
113
|
-
}
|
|
114
|
-
if (!$defs) {
|
|
115
|
-
return schema;
|
|
116
|
-
}
|
|
117
|
-
if (typeof schema.$ref !== "string" || !schema.$ref.startsWith("#/$defs/")) {
|
|
118
|
-
return schema;
|
|
119
|
-
}
|
|
120
|
-
const resolved = get($defs, schema.$ref.slice("#/$defs/".length).split("/").map(decodeJsonPointerSegment));
|
|
121
|
-
if (resolved === void 0) {
|
|
122
|
-
return schema;
|
|
123
|
-
}
|
|
124
|
-
if (typeof resolved !== "object") {
|
|
125
|
-
return resolved;
|
|
126
|
-
}
|
|
127
|
-
const { $ref: _ref, ...rest } = schema;
|
|
128
|
-
return resolveJsonSchemaRootLocalRef({
|
|
129
|
-
...rest,
|
|
130
|
-
...resolved
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
function findRecursiveJsonSchemaDefName(defs) {
|
|
134
|
-
let index = 0;
|
|
135
|
-
while (defs?.[`__schema${index}`] !== void 0) {
|
|
136
|
-
index++;
|
|
137
|
-
}
|
|
138
|
-
return `__schema${index}`;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
var JsonSchemaXNativeType = /* @__PURE__ */ ((JsonSchemaXNativeType2) => {
|
|
142
|
-
JsonSchemaXNativeType2["BigInt"] = "bigint";
|
|
143
|
-
JsonSchemaXNativeType2["RegExp"] = "regexp";
|
|
144
|
-
JsonSchemaXNativeType2["Date"] = "date";
|
|
145
|
-
JsonSchemaXNativeType2["Url"] = "url";
|
|
146
|
-
JsonSchemaXNativeType2["Set"] = "set";
|
|
147
|
-
JsonSchemaXNativeType2["Map"] = "map";
|
|
148
|
-
return JsonSchemaXNativeType2;
|
|
149
|
-
})(JsonSchemaXNativeType || {});
|
|
150
|
-
|
|
151
|
-
const FLEXIBLE_DATE_FORMAT_REGEX = /^[^-]+-[^-]+-[^-]+$/;
|
|
152
|
-
class JsonSchemaCoercer {
|
|
153
|
-
coerce([schema, optional], value) {
|
|
154
|
-
if (optional && value === void 0) {
|
|
155
|
-
return value;
|
|
156
|
-
}
|
|
157
|
-
const [, coerced] = this.coerceInternal(schema, schema, value);
|
|
158
|
-
return coerced;
|
|
159
|
-
}
|
|
160
|
-
coerceInternal(rootSchema, schema, value) {
|
|
161
|
-
if (typeof schema === "boolean") {
|
|
162
|
-
return [schema, value];
|
|
163
|
-
}
|
|
164
|
-
if (Array.isArray(schema.type)) {
|
|
165
|
-
return this.coerceInternal(
|
|
166
|
-
rootSchema,
|
|
167
|
-
{ anyOf: schema.type.map((type) => ({ ...schema, type })) },
|
|
168
|
-
value
|
|
169
|
-
);
|
|
170
|
-
}
|
|
171
|
-
let coerced = value;
|
|
172
|
-
let satisfied = true;
|
|
173
|
-
if (typeof schema.$ref === "string") {
|
|
174
|
-
const resolved = schema.$ref.startsWith("#/") ? get(rootSchema, schema.$ref.slice("#/".length).split("/").map(decodeJsonPointerSegment)) : schema.$ref === "#" ? rootSchema : void 0;
|
|
175
|
-
if (resolved !== void 0) {
|
|
176
|
-
const [subSatisfied, subCoerced] = this.coerceInternal(rootSchema, resolved, coerced);
|
|
177
|
-
coerced = subCoerced;
|
|
178
|
-
satisfied = subSatisfied;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
const enumValues = schema.const !== void 0 ? [schema.const] : schema.enum;
|
|
182
|
-
if (enumValues !== void 0 && !enumValues.includes(coerced)) {
|
|
183
|
-
if (typeof coerced === "string") {
|
|
184
|
-
const numberValue = stringToNumber(coerced);
|
|
185
|
-
if (enumValues.includes(numberValue)) {
|
|
186
|
-
coerced = numberValue;
|
|
187
|
-
} else {
|
|
188
|
-
const booleanValue = stringToBoolean(coerced);
|
|
189
|
-
if (enumValues.includes(booleanValue)) {
|
|
190
|
-
coerced = booleanValue;
|
|
191
|
-
} else {
|
|
192
|
-
satisfied = false;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
} else {
|
|
196
|
-
satisfied = false;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
if (schema.type) {
|
|
200
|
-
switch (schema.type) {
|
|
201
|
-
case "null": {
|
|
202
|
-
if (coerced !== null) {
|
|
203
|
-
satisfied = false;
|
|
204
|
-
}
|
|
205
|
-
break;
|
|
206
|
-
}
|
|
207
|
-
case "string": {
|
|
208
|
-
if (typeof coerced !== "string") {
|
|
209
|
-
satisfied = false;
|
|
210
|
-
}
|
|
211
|
-
break;
|
|
212
|
-
}
|
|
213
|
-
case "number": {
|
|
214
|
-
if (typeof coerced === "string") {
|
|
215
|
-
coerced = stringToNumber(coerced);
|
|
216
|
-
}
|
|
217
|
-
if (typeof coerced !== "number") {
|
|
218
|
-
satisfied = false;
|
|
219
|
-
}
|
|
220
|
-
break;
|
|
221
|
-
}
|
|
222
|
-
case "integer": {
|
|
223
|
-
if (typeof coerced === "string") {
|
|
224
|
-
coerced = stringToInteger(coerced);
|
|
225
|
-
}
|
|
226
|
-
if (typeof coerced !== "number" || !Number.isInteger(coerced)) {
|
|
227
|
-
satisfied = false;
|
|
228
|
-
}
|
|
229
|
-
break;
|
|
230
|
-
}
|
|
231
|
-
case "boolean": {
|
|
232
|
-
if (typeof coerced === "string") {
|
|
233
|
-
coerced = stringToBoolean(coerced);
|
|
234
|
-
}
|
|
235
|
-
if (typeof coerced !== "boolean") {
|
|
236
|
-
satisfied = false;
|
|
237
|
-
}
|
|
238
|
-
break;
|
|
239
|
-
}
|
|
240
|
-
case "array": {
|
|
241
|
-
if (Array.isArray(coerced)) {
|
|
242
|
-
const prefixItemSchemas = "prefixItems" in schema ? toArray(schema.prefixItems) : Array.isArray(schema.items) ? schema.items : [];
|
|
243
|
-
const itemSchema = Array.isArray(schema.items) ? schema.additionalItems : schema.items;
|
|
244
|
-
let shouldUseCoercedItems = false;
|
|
245
|
-
const coercedItems = coerced.map((item, i) => {
|
|
246
|
-
const subSchema = prefixItemSchemas[i] ?? itemSchema;
|
|
247
|
-
if (subSchema === void 0) {
|
|
248
|
-
satisfied = false;
|
|
249
|
-
return item;
|
|
250
|
-
}
|
|
251
|
-
const [subSatisfied, subCoerced] = this.coerceInternal(rootSchema, subSchema, item);
|
|
252
|
-
if (!subSatisfied) {
|
|
253
|
-
satisfied = false;
|
|
254
|
-
}
|
|
255
|
-
if (subCoerced !== item) {
|
|
256
|
-
shouldUseCoercedItems = true;
|
|
257
|
-
}
|
|
258
|
-
return subCoerced;
|
|
259
|
-
});
|
|
260
|
-
if (coercedItems.length < prefixItemSchemas.length) {
|
|
261
|
-
satisfied = false;
|
|
262
|
-
}
|
|
263
|
-
if (shouldUseCoercedItems) {
|
|
264
|
-
coerced = coercedItems;
|
|
265
|
-
}
|
|
266
|
-
} else {
|
|
267
|
-
satisfied = false;
|
|
268
|
-
}
|
|
269
|
-
break;
|
|
270
|
-
}
|
|
271
|
-
case "object": {
|
|
272
|
-
if (Array.isArray(coerced)) {
|
|
273
|
-
coerced = { ...coerced };
|
|
274
|
-
}
|
|
275
|
-
if (isPlainObject(coerced)) {
|
|
276
|
-
let shouldUseCoercedItems = false;
|
|
277
|
-
const coercedItems = {};
|
|
278
|
-
const patternProperties = Object.entries(schema.patternProperties ?? {}).map(([key, value2]) => [new RegExp(key), value2]);
|
|
279
|
-
for (const key in coerced) {
|
|
280
|
-
const value2 = coerced[key];
|
|
281
|
-
const subSchema = schema.properties?.[key] ?? patternProperties.find(([pattern]) => pattern.test(key))?.[1] ?? schema.additionalProperties;
|
|
282
|
-
if (value2 === void 0 && !schema.required?.includes(key)) {
|
|
283
|
-
coercedItems[key] = value2;
|
|
284
|
-
} else if (subSchema === void 0) {
|
|
285
|
-
coercedItems[key] = value2;
|
|
286
|
-
satisfied = false;
|
|
287
|
-
} else {
|
|
288
|
-
const [subSatisfied, subCoerced] = this.coerceInternal(rootSchema, subSchema, value2);
|
|
289
|
-
coercedItems[key] = subCoerced;
|
|
290
|
-
if (!subSatisfied) {
|
|
291
|
-
satisfied = false;
|
|
292
|
-
}
|
|
293
|
-
if (subCoerced !== value2) {
|
|
294
|
-
shouldUseCoercedItems = true;
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
if (schema.required?.some((key) => !Object.hasOwn(coercedItems, key))) {
|
|
299
|
-
satisfied = false;
|
|
300
|
-
}
|
|
301
|
-
if (shouldUseCoercedItems) {
|
|
302
|
-
coerced = coercedItems;
|
|
303
|
-
}
|
|
304
|
-
} else {
|
|
305
|
-
satisfied = false;
|
|
306
|
-
}
|
|
307
|
-
break;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
if ("x-native-type" in schema && typeof schema["x-native-type"] === "string") {
|
|
312
|
-
switch (schema["x-native-type"]) {
|
|
313
|
-
case JsonSchemaXNativeType.Date: {
|
|
314
|
-
if (typeof coerced === "string") {
|
|
315
|
-
coerced = stringToDate(coerced);
|
|
316
|
-
}
|
|
317
|
-
if (!(coerced instanceof Date)) {
|
|
318
|
-
satisfied = false;
|
|
319
|
-
}
|
|
320
|
-
break;
|
|
321
|
-
}
|
|
322
|
-
case JsonSchemaXNativeType.BigInt: {
|
|
323
|
-
switch (typeof coerced) {
|
|
324
|
-
case "string":
|
|
325
|
-
coerced = stringToBigInt(coerced);
|
|
326
|
-
break;
|
|
327
|
-
case "number":
|
|
328
|
-
coerced = numberToBigInt(coerced);
|
|
329
|
-
break;
|
|
330
|
-
}
|
|
331
|
-
if (typeof coerced !== "bigint") {
|
|
332
|
-
satisfied = false;
|
|
333
|
-
}
|
|
334
|
-
break;
|
|
335
|
-
}
|
|
336
|
-
case JsonSchemaXNativeType.RegExp: {
|
|
337
|
-
if (typeof coerced === "string") {
|
|
338
|
-
coerced = stringToRegExp(coerced);
|
|
339
|
-
}
|
|
340
|
-
if (!(coerced instanceof RegExp)) {
|
|
341
|
-
satisfied = false;
|
|
342
|
-
}
|
|
343
|
-
break;
|
|
344
|
-
}
|
|
345
|
-
case JsonSchemaXNativeType.Url: {
|
|
346
|
-
if (typeof coerced === "string") {
|
|
347
|
-
coerced = stringToURL(coerced);
|
|
348
|
-
}
|
|
349
|
-
if (!(coerced instanceof URL)) {
|
|
350
|
-
satisfied = false;
|
|
351
|
-
}
|
|
352
|
-
break;
|
|
353
|
-
}
|
|
354
|
-
case JsonSchemaXNativeType.Set: {
|
|
355
|
-
if (Array.isArray(coerced)) {
|
|
356
|
-
coerced = arrayToSet(coerced);
|
|
357
|
-
}
|
|
358
|
-
if (!(coerced instanceof Set)) {
|
|
359
|
-
satisfied = false;
|
|
360
|
-
}
|
|
361
|
-
break;
|
|
362
|
-
}
|
|
363
|
-
case JsonSchemaXNativeType.Map: {
|
|
364
|
-
if (Array.isArray(coerced)) {
|
|
365
|
-
coerced = arrayToMap(coerced);
|
|
366
|
-
}
|
|
367
|
-
if (!(coerced instanceof Map)) {
|
|
368
|
-
satisfied = false;
|
|
369
|
-
}
|
|
370
|
-
break;
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
if (schema.allOf) {
|
|
375
|
-
for (const subSchema of schema.allOf) {
|
|
376
|
-
const [subSatisfied, subCoerced] = this.coerceInternal(rootSchema, subSchema, coerced);
|
|
377
|
-
coerced = subCoerced;
|
|
378
|
-
if (!subSatisfied) {
|
|
379
|
-
satisfied = false;
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
for (const key of ["anyOf", "oneOf"]) {
|
|
384
|
-
if (schema[key]) {
|
|
385
|
-
let bestOptions;
|
|
386
|
-
for (const subSchema of schema[key]) {
|
|
387
|
-
const [subSatisfied, subCoerced] = this.coerceInternal(rootSchema, subSchema, coerced);
|
|
388
|
-
if (subSatisfied) {
|
|
389
|
-
if (!bestOptions || subCoerced === coerced) {
|
|
390
|
-
bestOptions = { coerced: subCoerced, satisfied: subSatisfied };
|
|
391
|
-
}
|
|
392
|
-
if (subCoerced === coerced) {
|
|
393
|
-
break;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
coerced = bestOptions ? bestOptions.coerced : coerced;
|
|
398
|
-
satisfied = bestOptions ? bestOptions.satisfied : false;
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
if (typeof schema.not !== "undefined") {
|
|
402
|
-
const [notSatisfied] = this.coerceInternal(rootSchema, schema.not, coerced);
|
|
403
|
-
if (notSatisfied) {
|
|
404
|
-
satisfied = false;
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
return [satisfied, coerced];
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
function stringToNumber(value) {
|
|
411
|
-
const num = Number.parseFloat(value);
|
|
412
|
-
if (Number.isNaN(num) || num !== Number(value)) {
|
|
413
|
-
return value;
|
|
414
|
-
}
|
|
415
|
-
return num;
|
|
416
|
-
}
|
|
417
|
-
function stringToInteger(value) {
|
|
418
|
-
const num = Number.parseInt(value);
|
|
419
|
-
if (Number.isNaN(num) || num !== Number(value)) {
|
|
420
|
-
return value;
|
|
421
|
-
}
|
|
422
|
-
return num;
|
|
423
|
-
}
|
|
424
|
-
function stringToBoolean(value) {
|
|
425
|
-
const lower = value.toLowerCase();
|
|
426
|
-
if (lower === "false" || lower === "off") {
|
|
427
|
-
return false;
|
|
428
|
-
}
|
|
429
|
-
if (lower === "true" || lower === "on") {
|
|
430
|
-
return true;
|
|
431
|
-
}
|
|
432
|
-
return value;
|
|
433
|
-
}
|
|
434
|
-
function stringToBigInt(value) {
|
|
435
|
-
return tryOrUndefined(() => BigInt(value)) ?? value;
|
|
436
|
-
}
|
|
437
|
-
function numberToBigInt(value) {
|
|
438
|
-
return tryOrUndefined(() => BigInt(value)) ?? value;
|
|
439
|
-
}
|
|
440
|
-
function stringToDate(value) {
|
|
441
|
-
const date = new Date(value);
|
|
442
|
-
if (Number.isNaN(date.getTime()) || !FLEXIBLE_DATE_FORMAT_REGEX.test(value)) {
|
|
443
|
-
return value;
|
|
444
|
-
}
|
|
445
|
-
return date;
|
|
446
|
-
}
|
|
447
|
-
function stringToRegExp(value) {
|
|
448
|
-
const match = value.match(/^\/(.*)\/([a-z]*)$/);
|
|
449
|
-
if (match) {
|
|
450
|
-
const [, pattern, flags] = match;
|
|
451
|
-
return tryOrUndefined(() => new RegExp(pattern, flags)) ?? value;
|
|
452
|
-
}
|
|
453
|
-
return value;
|
|
454
|
-
}
|
|
455
|
-
function stringToURL(value) {
|
|
456
|
-
return tryOrUndefined(() => new URL(value)) ?? value;
|
|
457
|
-
}
|
|
458
|
-
function arrayToSet(value) {
|
|
459
|
-
const set = new Set(value);
|
|
460
|
-
if (set.size !== value.length) {
|
|
461
|
-
return value;
|
|
462
|
-
}
|
|
463
|
-
return set;
|
|
464
|
-
}
|
|
465
|
-
function arrayToMap(value) {
|
|
466
|
-
if (value.some((item) => !Array.isArray(item) || item.length !== 2)) {
|
|
467
|
-
return value;
|
|
468
|
-
}
|
|
469
|
-
const result = new Map(value);
|
|
470
|
-
if (result.size !== value.length) {
|
|
471
|
-
return value;
|
|
472
|
-
}
|
|
473
|
-
return result;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
function isJsonPrimitiveSchema(schema) {
|
|
477
|
-
if (typeof schema === "boolean") {
|
|
478
|
-
return false;
|
|
479
|
-
}
|
|
480
|
-
if (typeof schema.type === "string" && JSON_SCHEMA_PRIMITIVE_TYPES.has(schema.type)) {
|
|
481
|
-
return true;
|
|
482
|
-
}
|
|
483
|
-
if (schema.const !== void 0) {
|
|
484
|
-
return true;
|
|
485
|
-
}
|
|
486
|
-
if (schema.enum !== void 0) {
|
|
487
|
-
return true;
|
|
488
|
-
}
|
|
489
|
-
return false;
|
|
490
|
-
}
|
|
491
|
-
function isJsonFileSchema(schema) {
|
|
492
|
-
return typeof schema !== "boolean" && schema.type === "string" && (typeof schema.contentMediaType === "string" || schema.format === "binary" || schema.contentEncoding === "binary");
|
|
493
|
-
}
|
|
494
|
-
function isJsonObjectSchema(schema) {
|
|
495
|
-
return typeof schema !== "boolean" && schema.type === "object";
|
|
496
|
-
}
|
|
497
|
-
function isJsonArraySchema(schema) {
|
|
498
|
-
return typeof schema !== "boolean" && schema.type === "array";
|
|
499
|
-
}
|
|
500
|
-
function isUnconstrainedSchema(schema) {
|
|
501
|
-
if (typeof schema === "boolean") {
|
|
502
|
-
return schema;
|
|
503
|
-
}
|
|
504
|
-
if (Object.keys(schema).every((k) => !JSON_SCHEMA_LOGIC_KEYWORDS.has(k))) {
|
|
505
|
-
return true;
|
|
506
|
-
}
|
|
507
|
-
return false;
|
|
508
|
-
}
|
|
509
|
-
function ensureJsonSchemaObject(schema) {
|
|
510
|
-
if (typeof schema === "boolean") {
|
|
511
|
-
return schema ? {} : { not: {} };
|
|
512
|
-
}
|
|
513
|
-
return schema;
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
function combineJsonSchemasWithComposition(keyword, schemas) {
|
|
517
|
-
if (schemas.length <= 1) {
|
|
518
|
-
return schemas[0] ?? true;
|
|
519
|
-
}
|
|
520
|
-
const mergedDefs = {};
|
|
521
|
-
const compositionBranches = [];
|
|
522
|
-
for (let i = 0; i < schemas.length; i++) {
|
|
523
|
-
const schema = schemas[i];
|
|
524
|
-
if (typeof schema === "boolean") {
|
|
525
|
-
compositionBranches.push(schema);
|
|
526
|
-
continue;
|
|
527
|
-
}
|
|
528
|
-
const { $defs, ...rest } = schema;
|
|
529
|
-
const renameMap = {};
|
|
530
|
-
const promotedNames = /* @__PURE__ */ new Set();
|
|
531
|
-
if ($defs) {
|
|
532
|
-
for (const [name, def] of Object.entries($defs)) {
|
|
533
|
-
if (def === void 0)
|
|
534
|
-
continue;
|
|
535
|
-
promotedNames.add(name);
|
|
536
|
-
if (name in mergedDefs) {
|
|
537
|
-
if (isDeepEqual(mergedDefs[name], def)) {
|
|
538
|
-
continue;
|
|
539
|
-
}
|
|
540
|
-
let counter = 2;
|
|
541
|
-
let newName = `${name}${counter}`;
|
|
542
|
-
while (newName in mergedDefs) {
|
|
543
|
-
counter++;
|
|
544
|
-
newName = `${name}${counter}`;
|
|
545
|
-
}
|
|
546
|
-
mergedDefs[newName] = def;
|
|
547
|
-
renameMap[name] = newName;
|
|
548
|
-
} else {
|
|
549
|
-
mergedDefs[name] = def;
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
compositionBranches.push(mapJsonSchemaRefs(
|
|
554
|
-
rest,
|
|
555
|
-
(ref) => {
|
|
556
|
-
if (ref === "#") {
|
|
557
|
-
return `#/${keyword}/${i}`;
|
|
558
|
-
}
|
|
559
|
-
if (ref.startsWith("#/$defs/")) {
|
|
560
|
-
const afterPrefix = ref.slice("#/$defs/".length);
|
|
561
|
-
const slashIdx = afterPrefix.indexOf("/");
|
|
562
|
-
const encodedSegment = slashIdx === -1 ? afterPrefix : afterPrefix.slice(0, slashIdx);
|
|
563
|
-
const rest2 = slashIdx === -1 ? "" : afterPrefix.slice(slashIdx);
|
|
564
|
-
const defName = decodeJsonPointerSegment(encodedSegment);
|
|
565
|
-
if (!promotedNames.has(defName)) {
|
|
566
|
-
return ref;
|
|
567
|
-
}
|
|
568
|
-
if (defName in renameMap) {
|
|
569
|
-
return `#/$defs/${encodeJsonPointerSegment(renameMap[defName])}${rest2}`;
|
|
570
|
-
}
|
|
571
|
-
return ref;
|
|
572
|
-
}
|
|
573
|
-
if (ref.startsWith("#/") && get(rest, ref.slice(2).split("/").map(decodeJsonPointerSegment)) !== void 0) {
|
|
574
|
-
return `#/${keyword}/${i}/${ref.slice(2)}`;
|
|
575
|
-
}
|
|
576
|
-
return ref;
|
|
577
|
-
}
|
|
578
|
-
));
|
|
579
|
-
}
|
|
580
|
-
const result = { [keyword]: compositionBranches };
|
|
581
|
-
if (Object.keys(mergedDefs).length > 0) {
|
|
582
|
-
result.$defs = mergedDefs;
|
|
583
|
-
}
|
|
584
|
-
return result;
|
|
585
|
-
}
|
|
586
|
-
function combineJsonObjectSchemaEntries(entries) {
|
|
587
|
-
const properties = {};
|
|
588
|
-
const required = [];
|
|
589
|
-
const mergedDefs = {};
|
|
590
|
-
for (const [name, propertySchema, optional] of entries) {
|
|
591
|
-
if (!optional) {
|
|
592
|
-
required.push(name);
|
|
593
|
-
}
|
|
594
|
-
if (typeof propertySchema === "boolean") {
|
|
595
|
-
properties[name] = propertySchema;
|
|
596
|
-
continue;
|
|
597
|
-
}
|
|
598
|
-
const { $defs, ...rest } = propertySchema;
|
|
599
|
-
const renameMap = {};
|
|
600
|
-
const promotedNames = /* @__PURE__ */ new Set();
|
|
601
|
-
if ($defs) {
|
|
602
|
-
for (const [defName, def] of Object.entries($defs)) {
|
|
603
|
-
if (def === void 0) {
|
|
604
|
-
continue;
|
|
605
|
-
}
|
|
606
|
-
promotedNames.add(defName);
|
|
607
|
-
if (defName in mergedDefs) {
|
|
608
|
-
if (isDeepEqual(mergedDefs[defName], def)) {
|
|
609
|
-
continue;
|
|
610
|
-
}
|
|
611
|
-
let counter = 2;
|
|
612
|
-
let newName = `${defName}${counter}`;
|
|
613
|
-
while (newName in mergedDefs) {
|
|
614
|
-
counter++;
|
|
615
|
-
newName = `${defName}${counter}`;
|
|
616
|
-
}
|
|
617
|
-
mergedDefs[newName] = def;
|
|
618
|
-
renameMap[defName] = newName;
|
|
619
|
-
} else {
|
|
620
|
-
mergedDefs[defName] = def;
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
const propertyPathPrefix = `#/properties/${encodeJsonPointerSegment(name)}`;
|
|
625
|
-
properties[name] = mapJsonSchemaRefs(rest, (ref) => {
|
|
626
|
-
if (ref.startsWith("#/$defs/")) {
|
|
627
|
-
const afterPrefix = ref.slice("#/$defs/".length);
|
|
628
|
-
const slashIdx = afterPrefix.indexOf("/");
|
|
629
|
-
const encodedSegment = slashIdx === -1 ? afterPrefix : afterPrefix.slice(0, slashIdx);
|
|
630
|
-
const refRest = slashIdx === -1 ? "" : afterPrefix.slice(slashIdx);
|
|
631
|
-
const defName = decodeJsonPointerSegment(encodedSegment);
|
|
632
|
-
if (!promotedNames.has(defName)) {
|
|
633
|
-
return ref;
|
|
634
|
-
}
|
|
635
|
-
if (defName in renameMap) {
|
|
636
|
-
return `#/$defs/${encodeJsonPointerSegment(renameMap[defName])}${refRest}`;
|
|
637
|
-
}
|
|
638
|
-
return ref;
|
|
639
|
-
}
|
|
640
|
-
if (ref === "#") {
|
|
641
|
-
return propertyPathPrefix;
|
|
642
|
-
}
|
|
643
|
-
if (ref.startsWith("#/") && get(rest, ref.slice(2).split("/").map(decodeJsonPointerSegment)) !== void 0) {
|
|
644
|
-
return `${propertyPathPrefix}/${ref.slice(2)}`;
|
|
645
|
-
}
|
|
646
|
-
return ref;
|
|
647
|
-
});
|
|
648
|
-
}
|
|
649
|
-
const schema = {
|
|
650
|
-
type: "object",
|
|
651
|
-
properties
|
|
652
|
-
};
|
|
653
|
-
if (required.length > 0) {
|
|
654
|
-
schema.required = required;
|
|
655
|
-
}
|
|
656
|
-
if (Object.keys(mergedDefs).length > 0) {
|
|
657
|
-
schema.$defs = mergedDefs;
|
|
658
|
-
}
|
|
659
|
-
return schema;
|
|
660
|
-
}
|
|
661
|
-
function extractJsonObjectSchemaEntries(schema) {
|
|
662
|
-
schema = hoistRecursiveRefToDef(schema);
|
|
663
|
-
if (typeof schema !== "object") {
|
|
664
|
-
return void 0;
|
|
665
|
-
}
|
|
666
|
-
const result = extractJsonObjectSchemaEntriesInternal(omit(schema, ["$defs"]), schema.$defs, /* @__PURE__ */ new Set());
|
|
667
|
-
if (!result.objectLike) {
|
|
668
|
-
return void 0;
|
|
669
|
-
}
|
|
670
|
-
return result.entries.map(([n, s, ...r]) => [n, withRootDefs(s, schema.$defs), ...r]);
|
|
671
|
-
}
|
|
672
|
-
function extractJsonObjectSchemaEntriesInternal(schema, $defs, resolvingRefs) {
|
|
673
|
-
if (typeof schema !== "object") {
|
|
674
|
-
return { entries: [], objectLike: false };
|
|
675
|
-
}
|
|
676
|
-
if (typeof schema.$ref === "string") {
|
|
677
|
-
if (resolvingRefs.has(schema.$ref)) {
|
|
678
|
-
return { entries: [], objectLike: true };
|
|
679
|
-
}
|
|
680
|
-
const resolved = resolveJsonSchemaRootLocalRef(schema, $defs);
|
|
681
|
-
if (resolved !== schema) {
|
|
682
|
-
return extractJsonObjectSchemaEntriesInternal(resolved, $defs, new Set(resolvingRefs).add(schema.$ref));
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
const sources = [];
|
|
686
|
-
if (schema.properties) {
|
|
687
|
-
sources.push({
|
|
688
|
-
entries: Object.entries(schema.properties).map(([name, propertySchema]) => {
|
|
689
|
-
return [
|
|
690
|
-
name,
|
|
691
|
-
propertySchema,
|
|
692
|
-
!schema.required?.includes(name)
|
|
693
|
-
];
|
|
694
|
-
}),
|
|
695
|
-
source: "direct"
|
|
696
|
-
});
|
|
697
|
-
}
|
|
698
|
-
let objectLike = schema.type === "object" || schema.properties !== void 0 || schema.required !== void 0 || schema.additionalProperties !== void 0;
|
|
699
|
-
for (const keyword of ["anyOf", "oneOf", "allOf"]) {
|
|
700
|
-
const branches = schema[keyword];
|
|
701
|
-
if (branches === void 0) {
|
|
702
|
-
continue;
|
|
703
|
-
}
|
|
704
|
-
const branchResults = branches.map((branch) => extractJsonObjectSchemaEntriesInternal(branch, $defs, resolvingRefs));
|
|
705
|
-
if (branchResults.some((result) => !result.objectLike)) {
|
|
706
|
-
return { entries: [], objectLike: false };
|
|
707
|
-
}
|
|
708
|
-
const entriesByName = /* @__PURE__ */ new Map();
|
|
709
|
-
for (const result of branchResults) {
|
|
710
|
-
for (const entry of result.entries) {
|
|
711
|
-
const entries = entriesByName.get(entry[0]);
|
|
712
|
-
if (entries) {
|
|
713
|
-
entries.push(entry);
|
|
714
|
-
} else {
|
|
715
|
-
entriesByName.set(entry[0], [entry]);
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
objectLike = true;
|
|
720
|
-
sources.push({
|
|
721
|
-
entries: Array.from(entriesByName.entries()).map(([name, entries]) => {
|
|
722
|
-
const schemas = deduplicateJsonSchemas(entries.map((entry) => entry[1]));
|
|
723
|
-
const required = keyword === "allOf" ? entries.some((entry) => !entry[2]) : branchResults.every((result) => {
|
|
724
|
-
const entry = result.entries.find((item) => item[0] === name);
|
|
725
|
-
return entry !== void 0 && !entry[2];
|
|
726
|
-
});
|
|
727
|
-
return [
|
|
728
|
-
name,
|
|
729
|
-
schemas.length === 1 ? schemas[0] : keyword === "allOf" ? { allOf: schemas } : { anyOf: schemas },
|
|
730
|
-
!required
|
|
731
|
-
];
|
|
732
|
-
}),
|
|
733
|
-
source: keyword
|
|
734
|
-
});
|
|
735
|
-
}
|
|
736
|
-
const sourceEntries = /* @__PURE__ */ new Map();
|
|
737
|
-
for (const { entries, source } of sources) {
|
|
738
|
-
for (const entry of entries) {
|
|
739
|
-
const existing = sourceEntries.get(entry[0]);
|
|
740
|
-
if (existing) {
|
|
741
|
-
existing[source] = entry;
|
|
742
|
-
} else {
|
|
743
|
-
sourceEntries.set(entry[0], { [source]: entry });
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
return {
|
|
748
|
-
entries: Array.from(sourceEntries.entries()).map(([name, entries]) => {
|
|
749
|
-
const schemas = deduplicateJsonSchemas([
|
|
750
|
-
entries.direct?.[1],
|
|
751
|
-
entries.allOf?.[1],
|
|
752
|
-
entries.anyOf?.[1],
|
|
753
|
-
entries.oneOf?.[1]
|
|
754
|
-
].filter((schema2) => schema2 !== void 0));
|
|
755
|
-
return [
|
|
756
|
-
name,
|
|
757
|
-
schemas.length === 1 ? schemas[0] : { allOf: schemas },
|
|
758
|
-
[entries.direct, entries.allOf, entries.anyOf, entries.oneOf].every((entry) => entry === void 0 || entry[2])
|
|
759
|
-
];
|
|
760
|
-
}),
|
|
761
|
-
objectLike
|
|
762
|
-
};
|
|
763
|
-
}
|
|
764
|
-
function flattenJsonUnionSchema(schema) {
|
|
765
|
-
return deduplicateJsonSchemas(flattenJsonUnionSchemaInternal(schema, /* @__PURE__ */ new Set()));
|
|
766
|
-
}
|
|
767
|
-
function flattenJsonUnionSchemaInternal(schema, resolvingRefs) {
|
|
768
|
-
if (typeof schema !== "object") {
|
|
769
|
-
return [schema];
|
|
770
|
-
}
|
|
771
|
-
if (typeof schema.$ref === "string") {
|
|
772
|
-
if (resolvingRefs.has(schema.$ref)) {
|
|
773
|
-
return [];
|
|
774
|
-
}
|
|
775
|
-
const resolved = resolveJsonSchemaRootLocalRef(schema);
|
|
776
|
-
if (resolved !== schema) {
|
|
777
|
-
const result = flattenJsonUnionSchemaInternal(resolved, resolvingRefs.add(schema.$ref));
|
|
778
|
-
if (result.length > 1) {
|
|
779
|
-
return result;
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
const { anyOf: _anyOf, oneOf: _oneOf, ...rest } = schema;
|
|
784
|
-
const entries = Object.entries(rest).filter(([, val]) => val !== void 0);
|
|
785
|
-
for (const keyword of ["anyOf", "oneOf"]) {
|
|
786
|
-
if (schema[keyword]) {
|
|
787
|
-
return schema[keyword].flatMap((s) => {
|
|
788
|
-
s = ensureJsonSchemaObject(s);
|
|
789
|
-
const mergedSchema = {
|
|
790
|
-
...s,
|
|
791
|
-
...Object.fromEntries(entries.filter(([key]) => s[key] === void 0)),
|
|
792
|
-
$defs: schema.$defs
|
|
793
|
-
};
|
|
794
|
-
const conflicts = entries.filter(([key]) => s[key] !== void 0);
|
|
795
|
-
if (conflicts.length) {
|
|
796
|
-
mergedSchema.allOf = [...toArray(mergedSchema.allOf), Object.fromEntries(conflicts)];
|
|
797
|
-
}
|
|
798
|
-
return flattenJsonUnionSchemaInternal(mergedSchema, resolvingRefs);
|
|
799
|
-
});
|
|
800
|
-
}
|
|
801
|
-
}
|
|
802
|
-
return [schema];
|
|
803
|
-
}
|
|
804
|
-
function matchArrayableJsonSchema(schema) {
|
|
805
|
-
const schemas = flattenJsonUnionSchema(schema);
|
|
806
|
-
if (schemas.length !== 2) {
|
|
807
|
-
return void 0;
|
|
808
|
-
}
|
|
809
|
-
const arraySchema = schemas.find(isJsonArraySchema);
|
|
810
|
-
if (arraySchema === void 0) {
|
|
811
|
-
return void 0;
|
|
812
|
-
}
|
|
813
|
-
const items1 = arraySchema.items ?? true;
|
|
814
|
-
const items2 = schemas.find((s) => s !== arraySchema);
|
|
815
|
-
const logicItem1 = Object.fromEntries(
|
|
816
|
-
Object.entries(ensureJsonSchemaObject(items1)).filter(([key]) => JSON_SCHEMA_LOGIC_KEYWORDS.has(key))
|
|
817
|
-
);
|
|
818
|
-
const logicItem2 = Object.fromEntries(
|
|
819
|
-
Object.entries(ensureJsonSchemaObject(items2)).filter(([key]) => JSON_SCHEMA_LOGIC_KEYWORDS.has(key))
|
|
820
|
-
);
|
|
821
|
-
if (!isDeepEqual(logicItem1, logicItem2)) {
|
|
822
|
-
return void 0;
|
|
823
|
-
}
|
|
824
|
-
return [items2, arraySchema];
|
|
825
|
-
}
|
|
826
|
-
function deduplicateJsonSchemas(schemas) {
|
|
827
|
-
const result = [];
|
|
828
|
-
for (const schema of schemas) {
|
|
829
|
-
if (result.some((i) => isDeepEqual(i, schema))) {
|
|
830
|
-
continue;
|
|
831
|
-
}
|
|
832
|
-
result.push(schema);
|
|
833
|
-
}
|
|
834
|
-
return result;
|
|
835
|
-
}
|
|
836
|
-
function withRootDefs(schema, $defs) {
|
|
837
|
-
if (typeof schema === "boolean" || !$defs) {
|
|
838
|
-
return schema;
|
|
839
|
-
}
|
|
840
|
-
return { ...schema, $defs };
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
class DelegatingJsonSchemaConverter {
|
|
844
|
-
constructor(converters = []) {
|
|
845
|
-
this.converters = converters;
|
|
846
|
-
}
|
|
847
|
-
async convert(schema, direction) {
|
|
848
|
-
for (const converter of this.converters) {
|
|
849
|
-
if (await converter.condition(schema, direction)) {
|
|
850
|
-
return converter.convert(schema, direction);
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
const optional = !(await schema?.["~standard"].validate(void 0))?.issues?.length;
|
|
854
|
-
if (schema && "jsonSchema" in schema["~standard"] && schema["~standard"].jsonSchema) {
|
|
855
|
-
try {
|
|
856
|
-
return [
|
|
857
|
-
schema["~standard"].jsonSchema[direction](),
|
|
858
|
-
optional
|
|
859
|
-
];
|
|
860
|
-
} catch {
|
|
861
|
-
}
|
|
862
|
-
}
|
|
863
|
-
return [{}, optional];
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
|
|
867
|
-
class StandardJsonSchemaConverter {
|
|
868
|
-
condition(schema, _direction) {
|
|
869
|
-
return Boolean(
|
|
870
|
-
schema && "jsonSchema" in schema["~standard"] && isTypescriptObject(schema["~standard"].jsonSchema) && "input" in schema["~standard"].jsonSchema && typeof schema["~standard"].jsonSchema.input === "function" && "output" in schema["~standard"].jsonSchema && typeof schema["~standard"].jsonSchema.output === "function"
|
|
871
|
-
);
|
|
872
|
-
}
|
|
873
|
-
convert(schema, direction) {
|
|
874
|
-
return this.convertInternal(schema, direction);
|
|
875
|
-
}
|
|
876
|
-
convertInternal(schema, direction) {
|
|
877
|
-
try {
|
|
878
|
-
const jsonSchema = direction === "input" ? schema["~standard"].jsonSchema.input({ target: "draft-2020-12" }) : schema["~standard"].jsonSchema.output({ target: "draft-2020-12" });
|
|
879
|
-
let optional = false;
|
|
880
|
-
try {
|
|
881
|
-
const result = schema["~standard"].validate(void 0);
|
|
882
|
-
if (!(result instanceof Promise) && !result.issues) {
|
|
883
|
-
optional = direction === "input" ? true : result.value === void 0;
|
|
884
|
-
}
|
|
885
|
-
} catch {
|
|
886
|
-
}
|
|
887
|
-
return [jsonSchema, optional];
|
|
888
|
-
} catch {
|
|
889
|
-
return [{}, true];
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
}
|
|
893
|
-
|
|
894
|
-
class SmartCoercionHandlerPlugin {
|
|
895
|
-
name = "~smart-coercion";
|
|
896
|
-
converter;
|
|
897
|
-
coercer;
|
|
898
|
-
cache = /* @__PURE__ */ new WeakMap();
|
|
899
|
-
constructor(options = {}) {
|
|
900
|
-
this.converter = new DelegatingJsonSchemaConverter([
|
|
901
|
-
...toArray(options.converters),
|
|
902
|
-
new StandardJsonSchemaConverter()
|
|
903
|
-
]);
|
|
904
|
-
this.coercer = new JsonSchemaCoercer();
|
|
905
|
-
}
|
|
906
|
-
init(options) {
|
|
907
|
-
return {
|
|
908
|
-
...options,
|
|
909
|
-
clientInterceptors: [
|
|
910
|
-
async ({ next, input, ...interceptorOptions }) => {
|
|
911
|
-
const inputSchemas = interceptorOptions.procedure["~orpc"].inputSchemas;
|
|
912
|
-
if (!inputSchemas) {
|
|
913
|
-
return next();
|
|
914
|
-
}
|
|
915
|
-
const coercedInput = await this.coerceValue(inputSchemas, input);
|
|
916
|
-
return next({ ...interceptorOptions, input: coercedInput });
|
|
917
|
-
},
|
|
918
|
-
...toArray(options.clientInterceptors)
|
|
919
|
-
]
|
|
920
|
-
};
|
|
921
|
-
}
|
|
922
|
-
async coerceValue(schemas, value) {
|
|
923
|
-
for (const schema of schemas) {
|
|
924
|
-
let converted = this.cache.get(schema);
|
|
925
|
-
if (!converted) {
|
|
926
|
-
converted = await this.converter.convert(schema, "input");
|
|
927
|
-
this.cache.set(schema, converted);
|
|
928
|
-
}
|
|
929
|
-
value = this.coercer.coerce(converted, value);
|
|
930
|
-
}
|
|
931
|
-
return value;
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
class SmartCoercionLinkPlugin {
|
|
936
|
-
constructor(contract, options = {}) {
|
|
937
|
-
this.contract = contract;
|
|
938
|
-
this.converter = new DelegatingJsonSchemaConverter([
|
|
939
|
-
...toArray(options.converters),
|
|
940
|
-
new StandardJsonSchemaConverter()
|
|
941
|
-
]);
|
|
942
|
-
this.coercer = new JsonSchemaCoercer();
|
|
943
|
-
}
|
|
944
|
-
name = "~smart-coercion";
|
|
945
|
-
/**
|
|
946
|
-
* Output and error values should be coerced before validation.
|
|
947
|
-
*/
|
|
948
|
-
after = ["~response-validation"];
|
|
949
|
-
converter;
|
|
950
|
-
coercer;
|
|
951
|
-
cache = /* @__PURE__ */ new WeakMap();
|
|
952
|
-
init(options) {
|
|
953
|
-
return {
|
|
954
|
-
...options,
|
|
955
|
-
interceptors: [
|
|
956
|
-
...toArray(options.interceptors),
|
|
957
|
-
async ({ next, path }) => {
|
|
958
|
-
const procedure = getProcedureContractOrThrow(this.contract, path);
|
|
959
|
-
try {
|
|
960
|
-
const output = await next();
|
|
961
|
-
const outputSchemas = procedure["~orpc"].outputSchemas;
|
|
962
|
-
if (!outputSchemas) {
|
|
963
|
-
return output;
|
|
964
|
-
}
|
|
965
|
-
const coercedOutput = await this.coerceValue(outputSchemas, output);
|
|
966
|
-
return coercedOutput;
|
|
967
|
-
} catch (error) {
|
|
968
|
-
if (!(error instanceof ORPCError) || !error.defined) {
|
|
969
|
-
throw error;
|
|
970
|
-
}
|
|
971
|
-
const errorMap = procedure["~orpc"].errorMap;
|
|
972
|
-
const dataSchema = errorMap[error.code]?.data;
|
|
973
|
-
if (!dataSchema) {
|
|
974
|
-
throw error;
|
|
975
|
-
}
|
|
976
|
-
const cloned = cloneORPCError(error);
|
|
977
|
-
cloned.data = await this.coerceValue([dataSchema], cloned.data);
|
|
978
|
-
throw cloned;
|
|
979
|
-
}
|
|
980
|
-
}
|
|
981
|
-
]
|
|
982
|
-
};
|
|
983
|
-
}
|
|
984
|
-
async coerceValue(schemas, value) {
|
|
985
|
-
for (const schema of schemas) {
|
|
986
|
-
let converted = this.cache.get(schema);
|
|
987
|
-
if (!converted) {
|
|
988
|
-
converted = await this.converter.convert(schema, "output");
|
|
989
|
-
this.cache.set(schema, converted);
|
|
990
|
-
}
|
|
991
|
-
value = this.coercer.coerce(converted, value);
|
|
992
|
-
}
|
|
993
|
-
return value;
|
|
994
|
-
}
|
|
995
|
-
}
|
|
996
|
-
|
|
997
|
-
export { DelegatingJsonSchemaConverter, JsonSchemaCoercer, JsonSchemaXNativeType, SmartCoercionHandlerPlugin, SmartCoercionLinkPlugin, StandardJsonSchemaConverter, combineJsonObjectSchemaEntries, combineJsonSchemasWithComposition, decodeJsonPointerSegment, deduplicateJsonSchemas, encodeJsonPointerSegment, ensureJsonSchemaObject, extractJsonObjectSchemaEntries, flattenJsonUnionSchema, hoistRecursiveRefToDef, isJsonArraySchema, isJsonFileSchema, isJsonObjectSchema, isJsonPrimitiveSchema, isUnconstrainedSchema, mapJsonSchemaRefs, matchArrayableJsonSchema, resolveJsonSchemaRootLocalRef };
|