@arrirpc/schema 0.73.0 → 0.74.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -22
- package/dist/adapters.cjs +9 -0
- package/dist/adapters.d.cts +11 -0
- package/dist/adapters.d.mts +11 -0
- package/dist/adapters.d.ts +11 -0
- package/dist/adapters.mjs +2 -0
- package/dist/index.cjs +2610 -500
- package/dist/index.d.cts +100 -63
- package/dist/index.d.mts +100 -63
- package/dist/index.d.ts +100 -63
- package/dist/index.mjs +2512 -402
- package/dist/shared/{schema.DLX0nTwB.d.cts → schema.BYXPajpl.d.cts} +54 -51
- package/dist/shared/{schema.DLX0nTwB.d.mts → schema.BYXPajpl.d.mts} +54 -51
- package/dist/shared/{schema.DLX0nTwB.d.ts → schema.BYXPajpl.d.ts} +54 -51
- package/dist/shared/schema.CZrExOkx.mjs +96 -0
- package/dist/shared/schema.Cwqn0t9l.cjs +111 -0
- package/package.json +3 -3
- package/dist/shared/schema.COiEWm_0.cjs +0 -1935
- package/dist/shared/schema.PNAcAdz3.mjs +0 -1860
- package/dist/testSuites.cjs +0 -1310
- package/dist/testSuites.d.cts +0 -39
- package/dist/testSuites.d.mts +0 -39
- package/dist/testSuites.d.ts +0 -39
- package/dist/testSuites.mjs +0 -1305
|
@@ -1,1935 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const typeDefs = require('@arrirpc/type-defs');
|
|
4
|
-
require('scule');
|
|
5
|
-
|
|
6
|
-
const int8Min = -128;
|
|
7
|
-
const int8Max = 127;
|
|
8
|
-
const uint8Min = 0;
|
|
9
|
-
const uint8Max = 255;
|
|
10
|
-
const int16Min = -32768;
|
|
11
|
-
const int16Max = 32767;
|
|
12
|
-
const uint16Min = 0;
|
|
13
|
-
const uint16Max = 65535;
|
|
14
|
-
const int32Min = -2147483648;
|
|
15
|
-
const int32Max = 2147483647;
|
|
16
|
-
const uint32Min = 0;
|
|
17
|
-
const uint32Max = 4294967295;
|
|
18
|
-
const int64Min = BigInt("-9223372036854775808");
|
|
19
|
-
const int64Max = BigInt("9223372036854775807");
|
|
20
|
-
const uint64Min = BigInt("0");
|
|
21
|
-
const uint64Max = BigInt("18446744073709551615");
|
|
22
|
-
|
|
23
|
-
const SCHEMA_METADATA = Symbol.for("arri.schema_metadata");
|
|
24
|
-
function isASchema(input) {
|
|
25
|
-
if (typeof input !== "object") {
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
if (!input || !("metadata" in input)) {
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
if (!input.metadata || typeof input.metadata !== "object" || !(SCHEMA_METADATA in input.metadata) || !input.metadata[SCHEMA_METADATA] || typeof input.metadata[SCHEMA_METADATA] !== "object") {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
return "parse" in input.metadata[SCHEMA_METADATA] && typeof input.metadata[SCHEMA_METADATA] === "object";
|
|
35
|
-
}
|
|
36
|
-
function isAScalarSchema(input) {
|
|
37
|
-
return isASchema(input) && "type" in input && typeof input.type === "string" && (typeDefs.TypeValues.includes(input.type) || NumberTypeValues.includes(input.type));
|
|
38
|
-
}
|
|
39
|
-
const NumberTypeValues = [
|
|
40
|
-
"float32",
|
|
41
|
-
"float64",
|
|
42
|
-
"int8",
|
|
43
|
-
"int16",
|
|
44
|
-
"int32",
|
|
45
|
-
"int64",
|
|
46
|
-
"uint8",
|
|
47
|
-
"uint16",
|
|
48
|
-
"uint32",
|
|
49
|
-
"uint64"
|
|
50
|
-
];
|
|
51
|
-
function isAAraySchema(input) {
|
|
52
|
-
return isASchema(input) && typeDefs.isSchemaFormElements(input);
|
|
53
|
-
}
|
|
54
|
-
function isAStringEnumSchema(input) {
|
|
55
|
-
return isASchema(input) && typeDefs.isSchemaFormEnum(input);
|
|
56
|
-
}
|
|
57
|
-
function isADiscriminatorSchema(input) {
|
|
58
|
-
return isASchema(input) && typeDefs.isSchemaFormDiscriminator(input);
|
|
59
|
-
}
|
|
60
|
-
function isARecordSchema(input) {
|
|
61
|
-
return isASchema(input) && typeDefs.isSchemaFormValues(input);
|
|
62
|
-
}
|
|
63
|
-
function isAObjectSchema(input) {
|
|
64
|
-
return isASchema(input) && typeDefs.isSchemaFormProperties(input);
|
|
65
|
-
}
|
|
66
|
-
function isObject(input) {
|
|
67
|
-
return typeof input === "object" && input !== null;
|
|
68
|
-
}
|
|
69
|
-
function isARefSchema(input) {
|
|
70
|
-
return isASchema(input) && typeDefs.isSchemaFormRef(input);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function createStandardSchemaProperty(validate, parse) {
|
|
74
|
-
return {
|
|
75
|
-
version: 1,
|
|
76
|
-
vendor: "arri",
|
|
77
|
-
validate(input) {
|
|
78
|
-
if (validate(input)) {
|
|
79
|
-
return {
|
|
80
|
-
value: input
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
const ctx = {
|
|
84
|
-
instancePath: "",
|
|
85
|
-
schemaPath: "",
|
|
86
|
-
errors: []
|
|
87
|
-
};
|
|
88
|
-
try {
|
|
89
|
-
const result = parse(input, ctx);
|
|
90
|
-
if (ctx.errors.length) {
|
|
91
|
-
return {
|
|
92
|
-
issues: ctx.errors.map((err) => ({
|
|
93
|
-
message: err.message ?? "Unknown error",
|
|
94
|
-
path: err.instancePath.split("/").filter((val) => val.length > 0)
|
|
95
|
-
}))
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
return {
|
|
99
|
-
value: result
|
|
100
|
-
};
|
|
101
|
-
} catch (err) {
|
|
102
|
-
if (err instanceof ValidationError) {
|
|
103
|
-
return {
|
|
104
|
-
issues: err.errors.map((err2) => ({
|
|
105
|
-
message: err2.message ?? "Unknown error",
|
|
106
|
-
path: err2.instancePath.split("/").filter((item) => item.length > 0)
|
|
107
|
-
}))
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
return {
|
|
111
|
-
issues: [
|
|
112
|
-
{
|
|
113
|
-
message: err instanceof Error ? err.message : `${err}`
|
|
114
|
-
}
|
|
115
|
-
]
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
function hideInvalidProperties(schema) {
|
|
122
|
-
Object.defineProperty(schema, "~standard", { enumerable: false });
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function any(options = {}) {
|
|
126
|
-
const validate = (input) => true;
|
|
127
|
-
const parse = (input, context) => {
|
|
128
|
-
if (context.instancePath.length === 0 && typeof input === "string") {
|
|
129
|
-
try {
|
|
130
|
-
return JSON.parse(input);
|
|
131
|
-
} catch {
|
|
132
|
-
return input;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return input;
|
|
136
|
-
};
|
|
137
|
-
const result = {
|
|
138
|
-
metadata: {
|
|
139
|
-
id: options.id,
|
|
140
|
-
description: options.description,
|
|
141
|
-
isDeprecated: options.isDeprecated,
|
|
142
|
-
[SCHEMA_METADATA]: {
|
|
143
|
-
output: undefined,
|
|
144
|
-
parse,
|
|
145
|
-
coerce: (input, context) => {
|
|
146
|
-
if (context.instancePath.length === 0 && typeof input === "string") {
|
|
147
|
-
try {
|
|
148
|
-
return JSON.parse(input);
|
|
149
|
-
} catch {
|
|
150
|
-
return input;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return input;
|
|
154
|
-
},
|
|
155
|
-
validate,
|
|
156
|
-
serialize(input) {
|
|
157
|
-
return JSON.stringify(input);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
},
|
|
161
|
-
"~standard": createStandardSchemaProperty(validate, parse)
|
|
162
|
-
};
|
|
163
|
-
hideInvalidProperties(result);
|
|
164
|
-
return result;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function array(schema, opts = {}) {
|
|
168
|
-
const validateType = (input) => {
|
|
169
|
-
return validate$5(schema, input);
|
|
170
|
-
};
|
|
171
|
-
const parseType = (input, context) => {
|
|
172
|
-
return parse$6(schema, input, context, false);
|
|
173
|
-
};
|
|
174
|
-
const result = {
|
|
175
|
-
elements: schema,
|
|
176
|
-
metadata: {
|
|
177
|
-
id: opts.id,
|
|
178
|
-
description: opts.description,
|
|
179
|
-
isDeprecated: opts.isDeprecated,
|
|
180
|
-
[SCHEMA_METADATA]: {
|
|
181
|
-
output: [],
|
|
182
|
-
parse: parseType,
|
|
183
|
-
coerce(input, context) {
|
|
184
|
-
return parse$6(schema, input, context, true);
|
|
185
|
-
},
|
|
186
|
-
validate: validateType,
|
|
187
|
-
serialize(input, context) {
|
|
188
|
-
const strParts = [];
|
|
189
|
-
for (let i = 0; i < input.length; i++) {
|
|
190
|
-
const item = input[i];
|
|
191
|
-
strParts.push(
|
|
192
|
-
schema.metadata[SCHEMA_METADATA].serialize(item, {
|
|
193
|
-
instancePath: `${context.instancePath}/${i}`,
|
|
194
|
-
schemaPath: `${context.schemaPath}/elements`,
|
|
195
|
-
errors: context.errors
|
|
196
|
-
})
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
return `[${strParts.join(",")}]`;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
},
|
|
203
|
-
"~standard": createStandardSchemaProperty(validateType, parseType)
|
|
204
|
-
};
|
|
205
|
-
hideInvalidProperties(result);
|
|
206
|
-
return result;
|
|
207
|
-
}
|
|
208
|
-
function validate$5(innerSchema, input) {
|
|
209
|
-
if (!Array.isArray(input)) {
|
|
210
|
-
return false;
|
|
211
|
-
}
|
|
212
|
-
for (const item of input) {
|
|
213
|
-
const isValid = innerSchema.metadata[SCHEMA_METADATA].validate(item);
|
|
214
|
-
if (!isValid) {
|
|
215
|
-
return false;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
return true;
|
|
219
|
-
}
|
|
220
|
-
function parse$6(innerSchema, input, data, coerce = false) {
|
|
221
|
-
let parsedInput = input;
|
|
222
|
-
if (data.instancePath.length === 0 && typeof input === "string") {
|
|
223
|
-
try {
|
|
224
|
-
parsedInput = JSON.parse(input);
|
|
225
|
-
} catch (_) {
|
|
226
|
-
data.errors.push({
|
|
227
|
-
instancePath: data.instancePath,
|
|
228
|
-
schemaPath: `${data.schemaPath}/elements`,
|
|
229
|
-
message: `Error at ${data.instancePath}. Invalid JSON.`
|
|
230
|
-
});
|
|
231
|
-
return undefined;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
if (!Array.isArray(parsedInput)) {
|
|
235
|
-
data.errors.push({
|
|
236
|
-
instancePath: data.instancePath,
|
|
237
|
-
schemaPath: `${data.schemaPath}/elements`,
|
|
238
|
-
message: `Error at ${data.instancePath}. Expected array. Got ${typeof input}.`
|
|
239
|
-
});
|
|
240
|
-
return undefined;
|
|
241
|
-
}
|
|
242
|
-
const result = [];
|
|
243
|
-
for (let i = 0; i < parsedInput.length; i++) {
|
|
244
|
-
const item = parsedInput[i];
|
|
245
|
-
if (coerce) {
|
|
246
|
-
const parsedItem = innerSchema.metadata[SCHEMA_METADATA].coerce(
|
|
247
|
-
item,
|
|
248
|
-
{
|
|
249
|
-
instancePath: `${data.instancePath}/${i}`,
|
|
250
|
-
schemaPath: `${data.schemaPath}/elements`,
|
|
251
|
-
errors: data.errors
|
|
252
|
-
}
|
|
253
|
-
);
|
|
254
|
-
result.push(parsedItem);
|
|
255
|
-
} else {
|
|
256
|
-
const parsedItem = innerSchema.metadata[SCHEMA_METADATA].parse(
|
|
257
|
-
item,
|
|
258
|
-
{
|
|
259
|
-
instancePath: `${data.instancePath}/${i}`,
|
|
260
|
-
schemaPath: `${data.schemaPath}/elements`,
|
|
261
|
-
errors: data.errors
|
|
262
|
-
}
|
|
263
|
-
);
|
|
264
|
-
result.push(parsedItem);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
if (data.errors.length) {
|
|
268
|
-
return undefined;
|
|
269
|
-
}
|
|
270
|
-
return result;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
function boolean(opts = {}) {
|
|
274
|
-
const result = {
|
|
275
|
-
type: "boolean",
|
|
276
|
-
metadata: {
|
|
277
|
-
id: opts.id,
|
|
278
|
-
description: opts.description,
|
|
279
|
-
isDeprecated: opts.isDeprecated,
|
|
280
|
-
[SCHEMA_METADATA]: {
|
|
281
|
-
output: false,
|
|
282
|
-
parse: parse$5,
|
|
283
|
-
validate: validate$4,
|
|
284
|
-
serialize: serialize$2,
|
|
285
|
-
coerce(input, context) {
|
|
286
|
-
if (validate$4(input)) {
|
|
287
|
-
return input;
|
|
288
|
-
}
|
|
289
|
-
switch (input) {
|
|
290
|
-
case "true":
|
|
291
|
-
case "TRUE":
|
|
292
|
-
case "1":
|
|
293
|
-
case 1:
|
|
294
|
-
return true;
|
|
295
|
-
case "false":
|
|
296
|
-
case "FALSE":
|
|
297
|
-
case "0":
|
|
298
|
-
case 0:
|
|
299
|
-
return false;
|
|
300
|
-
}
|
|
301
|
-
context.errors.push({
|
|
302
|
-
instancePath: context.instancePath,
|
|
303
|
-
schemaPath: context.schemaPath,
|
|
304
|
-
message: `Error at ${context.instancePath}. Unable to coerce ${input} to boolean.`
|
|
305
|
-
});
|
|
306
|
-
return undefined;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
},
|
|
310
|
-
"~standard": createStandardSchemaProperty(validate$4, parse$5)
|
|
311
|
-
};
|
|
312
|
-
hideInvalidProperties(result);
|
|
313
|
-
return result;
|
|
314
|
-
}
|
|
315
|
-
function validate$4(input) {
|
|
316
|
-
return typeof input === "boolean";
|
|
317
|
-
}
|
|
318
|
-
function parse$5(input, data) {
|
|
319
|
-
if (validate$4(input)) {
|
|
320
|
-
return input;
|
|
321
|
-
}
|
|
322
|
-
if (data.instancePath.length === 0 && typeof input === "string") {
|
|
323
|
-
if (input === "true") {
|
|
324
|
-
return true;
|
|
325
|
-
}
|
|
326
|
-
if (input === "false") {
|
|
327
|
-
return false;
|
|
328
|
-
}
|
|
329
|
-
data.errors.push({
|
|
330
|
-
instancePath: data.instancePath,
|
|
331
|
-
schemaPath: `${data.schemaPath}/type`,
|
|
332
|
-
message: `Error at ${data.instancePath}. Expected boolean. Got ${typeof input}.`
|
|
333
|
-
});
|
|
334
|
-
return undefined;
|
|
335
|
-
}
|
|
336
|
-
data.errors.push({
|
|
337
|
-
instancePath: data.instancePath,
|
|
338
|
-
schemaPath: `${data.schemaPath}/type`,
|
|
339
|
-
message: `Error at ${data.instancePath}. Expected boolean. Got ${typeof input}.`
|
|
340
|
-
});
|
|
341
|
-
return undefined;
|
|
342
|
-
}
|
|
343
|
-
function serialize$2(input) {
|
|
344
|
-
return `${input}`;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
function sanitizeJson(json) {
|
|
348
|
-
return json.replace(/[\b]/g, "\\b").replace(/[\f]/g, "\\f").replace(/[\n]/g, "\\n").replace(/[\r]/g, "\\r").replace(/[\t]/g, "\\t");
|
|
349
|
-
}
|
|
350
|
-
function validate$3(schema, input) {
|
|
351
|
-
return schema.metadata[SCHEMA_METADATA].validate(input);
|
|
352
|
-
}
|
|
353
|
-
function parse$4(schema, input) {
|
|
354
|
-
const errors2 = [];
|
|
355
|
-
const result = schema.metadata[SCHEMA_METADATA].parse(input, {
|
|
356
|
-
schemaPath: "",
|
|
357
|
-
instancePath: "",
|
|
358
|
-
errors: errors2
|
|
359
|
-
});
|
|
360
|
-
if (errors2.length) {
|
|
361
|
-
throw new ValidationError({
|
|
362
|
-
message: `Unable to parse input. ${errors2[0].message}`,
|
|
363
|
-
errors: errors2
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
return result;
|
|
367
|
-
}
|
|
368
|
-
function safeParse(schema, input) {
|
|
369
|
-
try {
|
|
370
|
-
const result = parse$4(schema, input);
|
|
371
|
-
return {
|
|
372
|
-
success: true,
|
|
373
|
-
value: result
|
|
374
|
-
};
|
|
375
|
-
} catch (err) {
|
|
376
|
-
if (isValidationError(err)) {
|
|
377
|
-
return {
|
|
378
|
-
success: false,
|
|
379
|
-
error: err
|
|
380
|
-
};
|
|
381
|
-
}
|
|
382
|
-
return {
|
|
383
|
-
success: false,
|
|
384
|
-
error: new ValidationError({
|
|
385
|
-
message: "Unable to parse input",
|
|
386
|
-
errors: []
|
|
387
|
-
})
|
|
388
|
-
};
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
function coerce$2(schema, input) {
|
|
392
|
-
const errors2 = [];
|
|
393
|
-
const result = schema.metadata[SCHEMA_METADATA].coerce(input, {
|
|
394
|
-
schemaPath: "",
|
|
395
|
-
instancePath: "",
|
|
396
|
-
errors: errors2
|
|
397
|
-
});
|
|
398
|
-
if (errors2.length) {
|
|
399
|
-
throw new ValidationError({
|
|
400
|
-
message: `Unable to coerce input. ${errors2[0]?.message}`,
|
|
401
|
-
errors: errors2
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
|
-
return result;
|
|
405
|
-
}
|
|
406
|
-
function safeCoerce(schema, input) {
|
|
407
|
-
try {
|
|
408
|
-
const result = coerce$2(schema, input);
|
|
409
|
-
return {
|
|
410
|
-
success: true,
|
|
411
|
-
value: result
|
|
412
|
-
};
|
|
413
|
-
} catch (err) {
|
|
414
|
-
if (isValidationError(err)) {
|
|
415
|
-
return {
|
|
416
|
-
success: false,
|
|
417
|
-
error: err
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
return {
|
|
421
|
-
success: false,
|
|
422
|
-
error: new ValidationError({
|
|
423
|
-
message: "Unable to coerce input",
|
|
424
|
-
errors: []
|
|
425
|
-
})
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
function serialize$1(schema, input) {
|
|
430
|
-
const context = {
|
|
431
|
-
instancePath: "",
|
|
432
|
-
schemaPath: "",
|
|
433
|
-
errors: []
|
|
434
|
-
};
|
|
435
|
-
return schema.metadata[SCHEMA_METADATA].serialize(input, context);
|
|
436
|
-
}
|
|
437
|
-
function errors(schema, input) {
|
|
438
|
-
const errorList = [];
|
|
439
|
-
try {
|
|
440
|
-
schema.metadata[SCHEMA_METADATA].parse(input, {
|
|
441
|
-
errors: errorList,
|
|
442
|
-
instancePath: "",
|
|
443
|
-
schemaPath: ""
|
|
444
|
-
});
|
|
445
|
-
} catch (err) {
|
|
446
|
-
errorList.push({ instancePath: "", schemaPath: "", message: `${err}` });
|
|
447
|
-
}
|
|
448
|
-
return errorList;
|
|
449
|
-
}
|
|
450
|
-
class ValidationError extends Error {
|
|
451
|
-
errors;
|
|
452
|
-
constructor(options) {
|
|
453
|
-
super(options.message);
|
|
454
|
-
this.errors = options.errors;
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
function isValidationError(input) {
|
|
458
|
-
if (typeof input !== "object" || !input) {
|
|
459
|
-
return false;
|
|
460
|
-
}
|
|
461
|
-
return "message" in input && typeof input.message === "string" && "errors" in input && Array.isArray(input.errors);
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
function discriminator(propA, propB, propC) {
|
|
465
|
-
const isIdShorthand = typeof propB === "string";
|
|
466
|
-
const discriminator2 = isIdShorthand ? propB : propA;
|
|
467
|
-
const mapping = isIdShorthand ? propC : propB;
|
|
468
|
-
const opts = isIdShorthand ? { id: propA } : propC ?? {};
|
|
469
|
-
if (isIdShorthand) {
|
|
470
|
-
opts.id = propA;
|
|
471
|
-
}
|
|
472
|
-
const isType = (input) => {
|
|
473
|
-
return validate$2(discriminator2, mapping, input);
|
|
474
|
-
};
|
|
475
|
-
const parseType = (input, context) => {
|
|
476
|
-
return parse$3(discriminator2, mapping, input, context, false);
|
|
477
|
-
};
|
|
478
|
-
const result = {
|
|
479
|
-
discriminator: discriminator2,
|
|
480
|
-
mapping,
|
|
481
|
-
metadata: {
|
|
482
|
-
id: opts.id,
|
|
483
|
-
description: opts.description,
|
|
484
|
-
isDeprecated: opts.isDeprecated,
|
|
485
|
-
[SCHEMA_METADATA]: {
|
|
486
|
-
output: {},
|
|
487
|
-
validate: isType,
|
|
488
|
-
parse: parseType,
|
|
489
|
-
coerce: (input, context) => {
|
|
490
|
-
return parse$3(discriminator2, mapping, input, context, true);
|
|
491
|
-
},
|
|
492
|
-
serialize(input, context) {
|
|
493
|
-
const discriminatorVal = input[discriminator2] ?? "";
|
|
494
|
-
const targetSchema = mapping[discriminatorVal];
|
|
495
|
-
if (!targetSchema) {
|
|
496
|
-
throw discriminatorMappingError(
|
|
497
|
-
discriminatorVal,
|
|
498
|
-
context
|
|
499
|
-
);
|
|
500
|
-
}
|
|
501
|
-
const result2 = targetSchema.metadata[SCHEMA_METADATA].serialize(input, {
|
|
502
|
-
instancePath: context.instancePath,
|
|
503
|
-
schemaPath: `${context.schemaPath}/mapping/${discriminatorVal}`,
|
|
504
|
-
errors: context.errors,
|
|
505
|
-
discriminatorKey: discriminator2,
|
|
506
|
-
discriminatorValue: discriminatorVal
|
|
507
|
-
});
|
|
508
|
-
return result2;
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
},
|
|
512
|
-
"~standard": createStandardSchemaProperty(isType, parseType)
|
|
513
|
-
};
|
|
514
|
-
hideInvalidProperties(result);
|
|
515
|
-
return result;
|
|
516
|
-
}
|
|
517
|
-
function validate$2(discriminator2, mapping, input) {
|
|
518
|
-
if (!isObject(input)) {
|
|
519
|
-
return false;
|
|
520
|
-
}
|
|
521
|
-
if (!(discriminator2 in input)) {
|
|
522
|
-
return false;
|
|
523
|
-
}
|
|
524
|
-
const discriminatorOptions = Object.keys(mapping);
|
|
525
|
-
if (!discriminatorOptions.includes(input[discriminator2])) {
|
|
526
|
-
return false;
|
|
527
|
-
}
|
|
528
|
-
const targetSchema = mapping[input[discriminator2]];
|
|
529
|
-
if (!targetSchema) {
|
|
530
|
-
return false;
|
|
531
|
-
}
|
|
532
|
-
return targetSchema.metadata[SCHEMA_METADATA].validate(input);
|
|
533
|
-
}
|
|
534
|
-
function parse$3(discriminator2, mapping, input, data, coerce = false) {
|
|
535
|
-
let parsedInput = input;
|
|
536
|
-
if (typeof input === "string" && input.length && data.instancePath.length === 0) {
|
|
537
|
-
parsedInput = JSON.parse(input);
|
|
538
|
-
}
|
|
539
|
-
if (!isObject(parsedInput)) {
|
|
540
|
-
data.errors.push({
|
|
541
|
-
instancePath: data.instancePath,
|
|
542
|
-
schemaPath: `${data.schemaPath}/discriminator`,
|
|
543
|
-
message: `Error at ${data.instancePath}. Expected object. Got ${typeof parsedInput}.`
|
|
544
|
-
});
|
|
545
|
-
return undefined;
|
|
546
|
-
}
|
|
547
|
-
if (!(discriminator2 in parsedInput)) {
|
|
548
|
-
data.errors.push({
|
|
549
|
-
instancePath: `${data.instancePath}/${discriminator2}`,
|
|
550
|
-
schemaPath: `${data.schemaPath}/discriminator`,
|
|
551
|
-
message: `Error at ${data.instancePath}/${discriminator2}. Discriminator field "${discriminator2}" cannot be undefined`
|
|
552
|
-
});
|
|
553
|
-
return undefined;
|
|
554
|
-
}
|
|
555
|
-
const acceptedDiscriminatorVals = Object.keys(mapping);
|
|
556
|
-
const discriminatorVal = parsedInput[discriminator2];
|
|
557
|
-
if (!acceptedDiscriminatorVals.includes(discriminatorVal)) {
|
|
558
|
-
data.errors.push({
|
|
559
|
-
instancePath: `${data.instancePath}/${discriminator2}`,
|
|
560
|
-
schemaPath: `${data.schemaPath}/discriminator`,
|
|
561
|
-
message: `Error at ${data.instancePath}/${discriminator2}. "${parsedInput[discriminator2]}" is not one of the accepted discriminator values: [${acceptedDiscriminatorVals.join(
|
|
562
|
-
", "
|
|
563
|
-
)}]`
|
|
564
|
-
});
|
|
565
|
-
return undefined;
|
|
566
|
-
}
|
|
567
|
-
const targetSchema = mapping[parsedInput[discriminator2]];
|
|
568
|
-
if (!targetSchema) {
|
|
569
|
-
throw discriminatorMappingError(discriminatorVal, data);
|
|
570
|
-
}
|
|
571
|
-
if (coerce) {
|
|
572
|
-
const result2 = targetSchema.metadata[SCHEMA_METADATA].coerce(
|
|
573
|
-
parsedInput,
|
|
574
|
-
{
|
|
575
|
-
instancePath: data.instancePath,
|
|
576
|
-
schemaPath: `${data.schemaPath}/mapping/${discriminatorVal}`,
|
|
577
|
-
errors: data.errors,
|
|
578
|
-
discriminatorKey: discriminator2,
|
|
579
|
-
discriminatorValue: discriminatorVal
|
|
580
|
-
}
|
|
581
|
-
);
|
|
582
|
-
return result2;
|
|
583
|
-
}
|
|
584
|
-
const result = targetSchema.metadata[SCHEMA_METADATA].parse(parsedInput, {
|
|
585
|
-
instancePath: data.instancePath,
|
|
586
|
-
schemaPath: `${data.schemaPath}/mapping/${discriminatorVal}`,
|
|
587
|
-
errors: data.errors,
|
|
588
|
-
discriminatorKey: discriminator2,
|
|
589
|
-
discriminatorValue: discriminatorVal
|
|
590
|
-
});
|
|
591
|
-
return result;
|
|
592
|
-
}
|
|
593
|
-
function discriminatorMappingError(discriminatorVal, data) {
|
|
594
|
-
return new ValidationError({
|
|
595
|
-
message: `Error fetching discriminator schema for "${discriminatorVal}"`,
|
|
596
|
-
errors: [
|
|
597
|
-
{
|
|
598
|
-
message: "Error fetching discriminator schema",
|
|
599
|
-
instancePath: data.instancePath,
|
|
600
|
-
schemaPath: data.schemaPath
|
|
601
|
-
}
|
|
602
|
-
]
|
|
603
|
-
});
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
const stringEnum = enumerator;
|
|
607
|
-
function enumerator(paramA, paramB) {
|
|
608
|
-
const isIdShorthand = typeof paramA === "string";
|
|
609
|
-
const enumVal = isIdShorthand ? paramB : paramA;
|
|
610
|
-
const meta = isIdShorthand ? { id: paramA } : paramB;
|
|
611
|
-
const isType = (input) => {
|
|
612
|
-
if (typeof input !== "string") {
|
|
613
|
-
return false;
|
|
614
|
-
}
|
|
615
|
-
for (const val of enumVal) {
|
|
616
|
-
if (val === input) {
|
|
617
|
-
return true;
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
return false;
|
|
621
|
-
};
|
|
622
|
-
const parse = (input, context) => {
|
|
623
|
-
if (isType(input)) {
|
|
624
|
-
return input;
|
|
625
|
-
}
|
|
626
|
-
context.errors.push({
|
|
627
|
-
instancePath: context.instancePath,
|
|
628
|
-
schemaPath: `${context.schemaPath}/enum`,
|
|
629
|
-
message: `Error at ${context.instancePath}. Invalid enum value. Expected one of the following values: [${enumVal.join(
|
|
630
|
-
", "
|
|
631
|
-
)}]`
|
|
632
|
-
});
|
|
633
|
-
return undefined;
|
|
634
|
-
};
|
|
635
|
-
const result = {
|
|
636
|
-
enum: enumVal,
|
|
637
|
-
metadata: {
|
|
638
|
-
id: meta?.id,
|
|
639
|
-
description: meta?.description,
|
|
640
|
-
isDeprecated: meta?.isDeprecated,
|
|
641
|
-
[SCHEMA_METADATA]: {
|
|
642
|
-
output: paramA[0] ?? "",
|
|
643
|
-
parse,
|
|
644
|
-
coerce: (input, context) => {
|
|
645
|
-
if (isType(input)) {
|
|
646
|
-
return input;
|
|
647
|
-
}
|
|
648
|
-
context.errors.push({
|
|
649
|
-
instancePath: context.instancePath,
|
|
650
|
-
schemaPath: `${context.schemaPath}/enum`,
|
|
651
|
-
message: `Error at ${context.instancePath}. Invalid enum value. Expected one of the following values: [${enumVal.join(
|
|
652
|
-
", "
|
|
653
|
-
)}]`
|
|
654
|
-
});
|
|
655
|
-
return undefined;
|
|
656
|
-
},
|
|
657
|
-
validate: isType,
|
|
658
|
-
serialize(input, context) {
|
|
659
|
-
if (context.instancePath.length === 0) {
|
|
660
|
-
return input;
|
|
661
|
-
}
|
|
662
|
-
return `"${input}"`;
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
},
|
|
666
|
-
"~standard": createStandardSchemaProperty(isType, parse)
|
|
667
|
-
};
|
|
668
|
-
hideInvalidProperties(result);
|
|
669
|
-
return result;
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
function nullable(schema, opts = {}) {
|
|
673
|
-
const isType = (val) => {
|
|
674
|
-
if (val === null) {
|
|
675
|
-
return true;
|
|
676
|
-
}
|
|
677
|
-
return schema.metadata[SCHEMA_METADATA].validate(val);
|
|
678
|
-
};
|
|
679
|
-
const parse = (val, data) => {
|
|
680
|
-
if (data.instancePath.length === 0 && typeof val === "string" && val === "null") {
|
|
681
|
-
return null;
|
|
682
|
-
}
|
|
683
|
-
if (val === null) {
|
|
684
|
-
return null;
|
|
685
|
-
}
|
|
686
|
-
return schema.metadata[SCHEMA_METADATA].parse(val, data);
|
|
687
|
-
};
|
|
688
|
-
const result = {
|
|
689
|
-
...schema,
|
|
690
|
-
nullable: true,
|
|
691
|
-
metadata: {
|
|
692
|
-
id: opts.id ?? schema.metadata.id,
|
|
693
|
-
description: opts.description ?? schema.metadata.description,
|
|
694
|
-
isDeprecated: opts.isDeprecated ?? schema.metadata.isDeprecated,
|
|
695
|
-
[SCHEMA_METADATA]: {
|
|
696
|
-
output: null,
|
|
697
|
-
optional: schema.metadata[SCHEMA_METADATA].optional,
|
|
698
|
-
validate: isType,
|
|
699
|
-
parse,
|
|
700
|
-
coerce(val, data) {
|
|
701
|
-
if (val === null) {
|
|
702
|
-
return null;
|
|
703
|
-
}
|
|
704
|
-
if (val === "null") {
|
|
705
|
-
return null;
|
|
706
|
-
}
|
|
707
|
-
return schema.metadata[SCHEMA_METADATA].coerce(val, data);
|
|
708
|
-
},
|
|
709
|
-
serialize(val, data) {
|
|
710
|
-
if (val === null) {
|
|
711
|
-
return "null";
|
|
712
|
-
}
|
|
713
|
-
return schema.metadata[SCHEMA_METADATA].serialize(
|
|
714
|
-
val,
|
|
715
|
-
data
|
|
716
|
-
);
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
};
|
|
721
|
-
hideInvalidProperties(result);
|
|
722
|
-
return result;
|
|
723
|
-
}
|
|
724
|
-
function optional(input, opts = {}) {
|
|
725
|
-
const isType = (val) => {
|
|
726
|
-
if (val === undefined) {
|
|
727
|
-
return true;
|
|
728
|
-
}
|
|
729
|
-
return input.metadata[SCHEMA_METADATA].validate(val);
|
|
730
|
-
};
|
|
731
|
-
const parse = (val, context) => {
|
|
732
|
-
if (typeof val === "undefined") {
|
|
733
|
-
return undefined;
|
|
734
|
-
}
|
|
735
|
-
if (context.instancePath.length === 0 && val === "undefined") {
|
|
736
|
-
return undefined;
|
|
737
|
-
}
|
|
738
|
-
return input.metadata[SCHEMA_METADATA].parse(val, context);
|
|
739
|
-
};
|
|
740
|
-
const result = {
|
|
741
|
-
...input,
|
|
742
|
-
metadata: {
|
|
743
|
-
id: opts.id ?? input.metadata.id,
|
|
744
|
-
description: opts.description ?? input.metadata.description,
|
|
745
|
-
isDeprecated: opts.isDeprecated ?? input.metadata.isDeprecated,
|
|
746
|
-
[SCHEMA_METADATA]: {
|
|
747
|
-
output: undefined,
|
|
748
|
-
optional: true,
|
|
749
|
-
validate: isType,
|
|
750
|
-
parse,
|
|
751
|
-
coerce: (val, data) => {
|
|
752
|
-
if (typeof val === "undefined") {
|
|
753
|
-
return undefined;
|
|
754
|
-
}
|
|
755
|
-
if (val === "undefined") {
|
|
756
|
-
return undefined;
|
|
757
|
-
}
|
|
758
|
-
return input.metadata[SCHEMA_METADATA].coerce(val, data);
|
|
759
|
-
},
|
|
760
|
-
serialize: (val, data) => {
|
|
761
|
-
if (typeof val === "undefined") {
|
|
762
|
-
return "undefined";
|
|
763
|
-
}
|
|
764
|
-
return input.metadata[SCHEMA_METADATA].serialize(val, data);
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
},
|
|
768
|
-
"~standard": createStandardSchemaProperty(isType, parse)
|
|
769
|
-
};
|
|
770
|
-
hideInvalidProperties(result);
|
|
771
|
-
return result;
|
|
772
|
-
}
|
|
773
|
-
function clone(input, opts = {}) {
|
|
774
|
-
const schema = {
|
|
775
|
-
...input,
|
|
776
|
-
metadata: {
|
|
777
|
-
id: opts.id,
|
|
778
|
-
description: opts.description,
|
|
779
|
-
isDeprecated: opts.isDeprecated,
|
|
780
|
-
[SCHEMA_METADATA]: {
|
|
781
|
-
...input.metadata[SCHEMA_METADATA]
|
|
782
|
-
}
|
|
783
|
-
},
|
|
784
|
-
"~standard": input["~standard"]
|
|
785
|
-
};
|
|
786
|
-
hideInvalidProperties(schema);
|
|
787
|
-
return schema;
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
function number(opts = {}) {
|
|
791
|
-
return float64(opts);
|
|
792
|
-
}
|
|
793
|
-
function coerceNumber(input, options) {
|
|
794
|
-
if (typeof input === "string") {
|
|
795
|
-
const parsedInput = Number(input);
|
|
796
|
-
if (Number.isNaN(parsedInput)) {
|
|
797
|
-
options.errors.push({
|
|
798
|
-
instancePath: `${options.instancePath}`,
|
|
799
|
-
schemaPath: `${options.schemaPath}/type`,
|
|
800
|
-
message: `Error at ${options.instancePath}. Unable to coerce ${typeof input} into a number`
|
|
801
|
-
});
|
|
802
|
-
return undefined;
|
|
803
|
-
}
|
|
804
|
-
return parseNumber(parsedInput, options);
|
|
805
|
-
}
|
|
806
|
-
return parseNumber(input, options);
|
|
807
|
-
}
|
|
808
|
-
function float32(opts = {}) {
|
|
809
|
-
return numberScalarType("float32", opts, () => {
|
|
810
|
-
return {
|
|
811
|
-
success: true
|
|
812
|
-
};
|
|
813
|
-
});
|
|
814
|
-
}
|
|
815
|
-
function float64(opts = {}) {
|
|
816
|
-
return numberScalarType("float64", opts, () => {
|
|
817
|
-
return {
|
|
818
|
-
success: true
|
|
819
|
-
};
|
|
820
|
-
});
|
|
821
|
-
}
|
|
822
|
-
function int8(opts = {}) {
|
|
823
|
-
return numberScalarType("int8", opts, (input) => {
|
|
824
|
-
const isValid = validateInt(input, int8Min, int8Max);
|
|
825
|
-
if (isValid) {
|
|
826
|
-
return {
|
|
827
|
-
success: true
|
|
828
|
-
};
|
|
829
|
-
}
|
|
830
|
-
return {
|
|
831
|
-
success: false,
|
|
832
|
-
message: `Must be an valid integer between ${int8Min} and ${int8Max}`
|
|
833
|
-
};
|
|
834
|
-
});
|
|
835
|
-
}
|
|
836
|
-
function uint8(opts = {}) {
|
|
837
|
-
return numberScalarType("uint8", opts, (input) => {
|
|
838
|
-
const isValid = validateInt(input, uint8Min, uint8Max);
|
|
839
|
-
if (isValid) {
|
|
840
|
-
return {
|
|
841
|
-
success: true
|
|
842
|
-
};
|
|
843
|
-
}
|
|
844
|
-
return {
|
|
845
|
-
success: false,
|
|
846
|
-
message: `Must be a valid integer between ${uint8Min} and ${uint8Max}`
|
|
847
|
-
};
|
|
848
|
-
});
|
|
849
|
-
}
|
|
850
|
-
function int16(opts = {}) {
|
|
851
|
-
return numberScalarType("int16", opts, (input) => {
|
|
852
|
-
const isValid = validateInt(input, int16Min, int16Max);
|
|
853
|
-
if (isValid) {
|
|
854
|
-
return {
|
|
855
|
-
success: true
|
|
856
|
-
};
|
|
857
|
-
}
|
|
858
|
-
return {
|
|
859
|
-
success: false,
|
|
860
|
-
message: `Must be a valid integer between ${int16Min} and ${int16Max}`
|
|
861
|
-
};
|
|
862
|
-
});
|
|
863
|
-
}
|
|
864
|
-
function uint16(opts = {}) {
|
|
865
|
-
return numberScalarType("uint16", opts, (input) => {
|
|
866
|
-
const isValid = validateInt(input, uint16Min, uint16Max);
|
|
867
|
-
if (isValid) {
|
|
868
|
-
return {
|
|
869
|
-
success: true
|
|
870
|
-
};
|
|
871
|
-
}
|
|
872
|
-
return {
|
|
873
|
-
success: false,
|
|
874
|
-
message: `Must be a valid integer between ${uint16Min} and ${uint16Max}`
|
|
875
|
-
};
|
|
876
|
-
});
|
|
877
|
-
}
|
|
878
|
-
function int32(opts = {}) {
|
|
879
|
-
return numberScalarType("int32", opts, (input) => {
|
|
880
|
-
const isValid = validateInt(input, int32Min, int32Max);
|
|
881
|
-
if (isValid) {
|
|
882
|
-
return {
|
|
883
|
-
success: true
|
|
884
|
-
};
|
|
885
|
-
}
|
|
886
|
-
return {
|
|
887
|
-
success: false,
|
|
888
|
-
message: `Must be a valid integer between ${int32Min} and ${int32Max}`
|
|
889
|
-
};
|
|
890
|
-
});
|
|
891
|
-
}
|
|
892
|
-
function uint32(opts = {}) {
|
|
893
|
-
return numberScalarType("uint32", opts, (input) => {
|
|
894
|
-
const isValid = validateInt(input, uint32Min, uint32Max);
|
|
895
|
-
if (isValid) {
|
|
896
|
-
return { success: true };
|
|
897
|
-
}
|
|
898
|
-
return {
|
|
899
|
-
success: false,
|
|
900
|
-
message: `Must be a valid integer between ${uint32Min} and ${uint32Max}`
|
|
901
|
-
};
|
|
902
|
-
});
|
|
903
|
-
}
|
|
904
|
-
function int64(opts = {}) {
|
|
905
|
-
function isType(input) {
|
|
906
|
-
return typeof input === "bigint" && input >= int64Min && input <= int64Max;
|
|
907
|
-
}
|
|
908
|
-
function parse(input, data) {
|
|
909
|
-
if (typeof input === "string" || typeof input === "number") {
|
|
910
|
-
try {
|
|
911
|
-
const val = BigInt(input);
|
|
912
|
-
if (isType(val)) {
|
|
913
|
-
return val;
|
|
914
|
-
}
|
|
915
|
-
data.errors.push({
|
|
916
|
-
message: `Error at ${data.instancePath}. Invalid int64.`,
|
|
917
|
-
schemaPath: `${data.schemaPath}/type`,
|
|
918
|
-
instancePath: data.instancePath
|
|
919
|
-
});
|
|
920
|
-
return void 0;
|
|
921
|
-
} catch (_) {
|
|
922
|
-
data.errors.push({
|
|
923
|
-
message: `Error at ${data.instancePath}. Unable to transform ${input} to BigInt`,
|
|
924
|
-
schemaPath: `${data.schemaPath}/type`,
|
|
925
|
-
instancePath: data.instancePath
|
|
926
|
-
});
|
|
927
|
-
return undefined;
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
if (isType(input)) {
|
|
931
|
-
return input;
|
|
932
|
-
}
|
|
933
|
-
data.errors.push({
|
|
934
|
-
message: `Error at ${data.instancePath}. Expected BigInt or integer string`,
|
|
935
|
-
schemaPath: `${data.schemaPath}/type`,
|
|
936
|
-
instancePath: data.instancePath
|
|
937
|
-
});
|
|
938
|
-
return undefined;
|
|
939
|
-
}
|
|
940
|
-
const result = {
|
|
941
|
-
type: "int64",
|
|
942
|
-
metadata: {
|
|
943
|
-
id: opts.id,
|
|
944
|
-
description: opts.description,
|
|
945
|
-
isDeprecated: opts.isDeprecated,
|
|
946
|
-
[SCHEMA_METADATA]: {
|
|
947
|
-
output: BigInt("0"),
|
|
948
|
-
validate: isType,
|
|
949
|
-
parse,
|
|
950
|
-
coerce: parse,
|
|
951
|
-
serialize(input, context) {
|
|
952
|
-
if (context.instancePath.length === 0) {
|
|
953
|
-
return input.toString();
|
|
954
|
-
}
|
|
955
|
-
return `"${input.toString()}"`;
|
|
956
|
-
}
|
|
957
|
-
}
|
|
958
|
-
},
|
|
959
|
-
"~standard": createStandardSchemaProperty(isType, parse)
|
|
960
|
-
};
|
|
961
|
-
hideInvalidProperties(result);
|
|
962
|
-
return result;
|
|
963
|
-
}
|
|
964
|
-
function uint64(opts = {}) {
|
|
965
|
-
function isType(input) {
|
|
966
|
-
return typeof input === "bigint" && input >= uint64Min && input <= uint64Max;
|
|
967
|
-
}
|
|
968
|
-
function parse(input, data) {
|
|
969
|
-
if (typeof input === "string" || typeof input === "number") {
|
|
970
|
-
try {
|
|
971
|
-
const val = BigInt(input);
|
|
972
|
-
if (isType(val)) {
|
|
973
|
-
return val;
|
|
974
|
-
}
|
|
975
|
-
data.errors.push({
|
|
976
|
-
message: `Error at ${data.instancePath}. Invalid uint64.`,
|
|
977
|
-
schemaPath: `${data.schemaPath}/type`,
|
|
978
|
-
instancePath: data.instancePath
|
|
979
|
-
});
|
|
980
|
-
return void 0;
|
|
981
|
-
} catch (_) {
|
|
982
|
-
data.errors.push({
|
|
983
|
-
message: `Error at ${data.instancePath}. Unable to transform ${input} to BigInt.`,
|
|
984
|
-
schemaPath: `${data.schemaPath}/type`,
|
|
985
|
-
instancePath: data.instancePath
|
|
986
|
-
});
|
|
987
|
-
return undefined;
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
if (isType(input)) {
|
|
991
|
-
return input;
|
|
992
|
-
}
|
|
993
|
-
data.errors.push({
|
|
994
|
-
message: `Error at ${data.instancePath}. Expected BigInt or integer string.`,
|
|
995
|
-
schemaPath: `${data.schemaPath}/type`,
|
|
996
|
-
instancePath: data.instancePath
|
|
997
|
-
});
|
|
998
|
-
return undefined;
|
|
999
|
-
}
|
|
1000
|
-
const result = {
|
|
1001
|
-
type: "uint64",
|
|
1002
|
-
metadata: {
|
|
1003
|
-
id: opts.id,
|
|
1004
|
-
description: opts.description,
|
|
1005
|
-
isDeprecated: opts.isDeprecated,
|
|
1006
|
-
[SCHEMA_METADATA]: {
|
|
1007
|
-
output: BigInt("0"),
|
|
1008
|
-
validate: isType,
|
|
1009
|
-
parse,
|
|
1010
|
-
coerce: parse,
|
|
1011
|
-
serialize(input, context) {
|
|
1012
|
-
if (context.instancePath.length === 0) {
|
|
1013
|
-
return input.toString();
|
|
1014
|
-
}
|
|
1015
|
-
return `"${input.toString()}"`;
|
|
1016
|
-
}
|
|
1017
|
-
}
|
|
1018
|
-
},
|
|
1019
|
-
"~standard": createStandardSchemaProperty(isType, parse)
|
|
1020
|
-
};
|
|
1021
|
-
hideInvalidProperties(result);
|
|
1022
|
-
return result;
|
|
1023
|
-
}
|
|
1024
|
-
function validateNumber(input) {
|
|
1025
|
-
return typeof input === "number";
|
|
1026
|
-
}
|
|
1027
|
-
function validateInt(input, minVal, maxValue) {
|
|
1028
|
-
return Number.isInteger(input) && input >= minVal && input <= maxValue;
|
|
1029
|
-
}
|
|
1030
|
-
function serializeNumber(input, _data) {
|
|
1031
|
-
return input.toString();
|
|
1032
|
-
}
|
|
1033
|
-
function parseNumber(input, options) {
|
|
1034
|
-
if (options.instancePath.length === 0 && typeof input === "string") {
|
|
1035
|
-
const result = Number(input);
|
|
1036
|
-
if (Number.isNaN(result)) {
|
|
1037
|
-
options?.errors.push({
|
|
1038
|
-
instancePath: options.instancePath,
|
|
1039
|
-
schemaPath: `${options.schemaPath}/type`,
|
|
1040
|
-
message: `Error at ${options.instancePath}. Invalid number.`
|
|
1041
|
-
});
|
|
1042
|
-
return undefined;
|
|
1043
|
-
}
|
|
1044
|
-
return result;
|
|
1045
|
-
}
|
|
1046
|
-
if (typeof input === "number" && !Number.isNaN(input)) {
|
|
1047
|
-
return input;
|
|
1048
|
-
}
|
|
1049
|
-
options?.errors.push({
|
|
1050
|
-
instancePath: options.instancePath,
|
|
1051
|
-
schemaPath: `${options.schemaPath}/type`,
|
|
1052
|
-
message: `Error at ${options.instancePath}. Invalid number.`
|
|
1053
|
-
});
|
|
1054
|
-
return undefined;
|
|
1055
|
-
}
|
|
1056
|
-
function numberScalarType(type, opts, numTypeMatcher) {
|
|
1057
|
-
const validate = (input) => {
|
|
1058
|
-
const isNum = validateNumber(input);
|
|
1059
|
-
if (!isNum) {
|
|
1060
|
-
return false;
|
|
1061
|
-
}
|
|
1062
|
-
return numTypeMatcher(input).success;
|
|
1063
|
-
};
|
|
1064
|
-
const parse = (input, context) => {
|
|
1065
|
-
const result2 = parseNumber(input, context);
|
|
1066
|
-
if (context.errors.length) {
|
|
1067
|
-
return undefined;
|
|
1068
|
-
}
|
|
1069
|
-
if (typeof result2 !== "number") {
|
|
1070
|
-
context?.errors.push({
|
|
1071
|
-
instancePath: context.instancePath,
|
|
1072
|
-
schemaPath: `${context.schemaPath}/type`,
|
|
1073
|
-
message: `Error at ${context.instancePath}. Invalid number.`
|
|
1074
|
-
});
|
|
1075
|
-
return undefined;
|
|
1076
|
-
}
|
|
1077
|
-
const matchResult = numTypeMatcher(result2);
|
|
1078
|
-
if (matchResult.success) {
|
|
1079
|
-
return result2;
|
|
1080
|
-
}
|
|
1081
|
-
context?.errors.push({
|
|
1082
|
-
instancePath: context.instancePath,
|
|
1083
|
-
schemaPath: `${context.schemaPath}/type`,
|
|
1084
|
-
message: `Error at ${context.instancePath}. ${matchResult.message}`
|
|
1085
|
-
});
|
|
1086
|
-
return undefined;
|
|
1087
|
-
};
|
|
1088
|
-
const result = {
|
|
1089
|
-
type,
|
|
1090
|
-
metadata: {
|
|
1091
|
-
id: opts.id,
|
|
1092
|
-
description: opts.description,
|
|
1093
|
-
isDeprecated: opts.isDeprecated,
|
|
1094
|
-
[SCHEMA_METADATA]: {
|
|
1095
|
-
output: 0,
|
|
1096
|
-
validate,
|
|
1097
|
-
parse,
|
|
1098
|
-
serialize: serializeNumber,
|
|
1099
|
-
coerce(input, options) {
|
|
1100
|
-
const result2 = coerceNumber(input, options);
|
|
1101
|
-
if (typeof result2 !== "number") {
|
|
1102
|
-
options.errors.push({
|
|
1103
|
-
instancePath: options.instancePath,
|
|
1104
|
-
schemaPath: `${options.schemaPath}/type`,
|
|
1105
|
-
message: `Error at ${options.instancePath}. Unable to coerce ${typeof input} to ${type}.`
|
|
1106
|
-
});
|
|
1107
|
-
return undefined;
|
|
1108
|
-
}
|
|
1109
|
-
const matchResult = numTypeMatcher(result2);
|
|
1110
|
-
if (matchResult.success) {
|
|
1111
|
-
return result2;
|
|
1112
|
-
}
|
|
1113
|
-
options.errors.push({
|
|
1114
|
-
instancePath: options.instancePath,
|
|
1115
|
-
schemaPath: `${options.schemaPath}/type`,
|
|
1116
|
-
message: `Error at ${options.instancePath}. ${matchResult.message}`
|
|
1117
|
-
});
|
|
1118
|
-
return undefined;
|
|
1119
|
-
}
|
|
1120
|
-
}
|
|
1121
|
-
},
|
|
1122
|
-
"~standard": createStandardSchemaProperty(validate, parse)
|
|
1123
|
-
};
|
|
1124
|
-
hideInvalidProperties(result);
|
|
1125
|
-
return result;
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1128
|
-
function object(propA, propB) {
|
|
1129
|
-
const isIdShorthand = typeof propA === "string";
|
|
1130
|
-
const input = isIdShorthand ? propB : propA;
|
|
1131
|
-
const options = isIdShorthand ? { id: propA } : propB ?? {};
|
|
1132
|
-
const schema = {
|
|
1133
|
-
properties: {}
|
|
1134
|
-
};
|
|
1135
|
-
if (typeof options.strict === "boolean") {
|
|
1136
|
-
schema.strict = options.strict;
|
|
1137
|
-
}
|
|
1138
|
-
for (const key of Object.keys(input)) {
|
|
1139
|
-
const prop = input[key];
|
|
1140
|
-
if (prop.metadata[SCHEMA_METADATA].optional) {
|
|
1141
|
-
if (!schema.optionalProperties) {
|
|
1142
|
-
schema.optionalProperties = {};
|
|
1143
|
-
}
|
|
1144
|
-
schema.optionalProperties[key] = prop;
|
|
1145
|
-
continue;
|
|
1146
|
-
}
|
|
1147
|
-
schema.properties[key] = prop;
|
|
1148
|
-
}
|
|
1149
|
-
const validate = (input2) => {
|
|
1150
|
-
return validateObjectSchema(schema, input2);
|
|
1151
|
-
};
|
|
1152
|
-
const parse = (input2, context) => {
|
|
1153
|
-
return parseObjectSchema(
|
|
1154
|
-
schema,
|
|
1155
|
-
input2,
|
|
1156
|
-
context,
|
|
1157
|
-
false
|
|
1158
|
-
);
|
|
1159
|
-
};
|
|
1160
|
-
const result = {
|
|
1161
|
-
...schema,
|
|
1162
|
-
metadata: {
|
|
1163
|
-
id: options.id,
|
|
1164
|
-
description: options.description,
|
|
1165
|
-
isDeprecated: options.isDeprecated,
|
|
1166
|
-
[SCHEMA_METADATA]: {
|
|
1167
|
-
output: {},
|
|
1168
|
-
parse,
|
|
1169
|
-
coerce(input2, context) {
|
|
1170
|
-
return parseObjectSchema(
|
|
1171
|
-
schema,
|
|
1172
|
-
input2,
|
|
1173
|
-
context,
|
|
1174
|
-
true
|
|
1175
|
-
);
|
|
1176
|
-
},
|
|
1177
|
-
validate,
|
|
1178
|
-
serialize(input2, context) {
|
|
1179
|
-
return serializeObject(
|
|
1180
|
-
schema,
|
|
1181
|
-
input2,
|
|
1182
|
-
context
|
|
1183
|
-
);
|
|
1184
|
-
}
|
|
1185
|
-
}
|
|
1186
|
-
},
|
|
1187
|
-
"~standard": createStandardSchemaProperty(validate, parse)
|
|
1188
|
-
};
|
|
1189
|
-
hideInvalidProperties(result);
|
|
1190
|
-
return result;
|
|
1191
|
-
}
|
|
1192
|
-
function parseObjectSchema(schema, input, data, coerce = false) {
|
|
1193
|
-
let parsedInput = input;
|
|
1194
|
-
if (data.instancePath.length === 0 && typeof input === "string") {
|
|
1195
|
-
try {
|
|
1196
|
-
const result2 = JSON.parse(input);
|
|
1197
|
-
parsedInput = result2;
|
|
1198
|
-
} catch (_err) {
|
|
1199
|
-
data.errors.push({
|
|
1200
|
-
instancePath: data.instancePath,
|
|
1201
|
-
schemaPath: `${data.schemaPath}/properties`,
|
|
1202
|
-
message: `Error at ${data.instancePath}. Invalid JSON.`
|
|
1203
|
-
});
|
|
1204
|
-
return undefined;
|
|
1205
|
-
}
|
|
1206
|
-
}
|
|
1207
|
-
if (!isObject(parsedInput)) {
|
|
1208
|
-
data.errors.push({
|
|
1209
|
-
instancePath: data.instancePath,
|
|
1210
|
-
schemaPath: `${data.schemaPath}/properties`,
|
|
1211
|
-
message: `Error at ${data.instancePath}. Expected object`
|
|
1212
|
-
});
|
|
1213
|
-
return undefined;
|
|
1214
|
-
}
|
|
1215
|
-
const result = {};
|
|
1216
|
-
const optionalProps = schema.optionalProperties ?? {};
|
|
1217
|
-
const inputKeys = Object.keys(parsedInput);
|
|
1218
|
-
const requiredKeys = Object.keys(schema.properties);
|
|
1219
|
-
const optionalKeys = Object.keys(optionalProps);
|
|
1220
|
-
if (schema.strict) {
|
|
1221
|
-
for (const key of inputKeys) {
|
|
1222
|
-
if (!requiredKeys.includes(key) && !optionalKeys.includes(key) && key !== data.discriminatorKey) {
|
|
1223
|
-
data.errors.push({
|
|
1224
|
-
instancePath: `${data.instancePath}/${key}`,
|
|
1225
|
-
schemaPath: `${data.schemaPath}/strict`,
|
|
1226
|
-
message: `Error at ${data.instancePath}/${key}. Key '${key}' is not included in the schema. To allow additional input properties set strict to 'false'.`
|
|
1227
|
-
});
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
if (data.errors.length) {
|
|
1232
|
-
return undefined;
|
|
1233
|
-
}
|
|
1234
|
-
if (data.discriminatorKey) {
|
|
1235
|
-
result[data.discriminatorKey] = data.discriminatorValue;
|
|
1236
|
-
}
|
|
1237
|
-
for (const key of requiredKeys) {
|
|
1238
|
-
const val = parsedInput[key];
|
|
1239
|
-
const prop = schema.properties[key];
|
|
1240
|
-
if (coerce) {
|
|
1241
|
-
result[key] = prop.metadata[SCHEMA_METADATA].coerce(val, {
|
|
1242
|
-
instancePath: `${data.instancePath}/${key}`,
|
|
1243
|
-
schemaPath: `${data.schemaPath}/properties/${key}`,
|
|
1244
|
-
errors: data.errors
|
|
1245
|
-
});
|
|
1246
|
-
continue;
|
|
1247
|
-
}
|
|
1248
|
-
result[key] = prop.metadata[SCHEMA_METADATA].parse(val, {
|
|
1249
|
-
instancePath: `${data.instancePath}/${key}`,
|
|
1250
|
-
schemaPath: `${data.schemaPath}/properties/${key}`,
|
|
1251
|
-
errors: data.errors
|
|
1252
|
-
});
|
|
1253
|
-
}
|
|
1254
|
-
for (const key of optionalKeys) {
|
|
1255
|
-
const val = parsedInput[key];
|
|
1256
|
-
const prop = optionalProps[key];
|
|
1257
|
-
if (val === undefined) {
|
|
1258
|
-
continue;
|
|
1259
|
-
}
|
|
1260
|
-
if (coerce) {
|
|
1261
|
-
result[key] = prop.metadata[SCHEMA_METADATA].coerce(val, {
|
|
1262
|
-
instancePath: `${data.instancePath}/${key}`,
|
|
1263
|
-
schemaPath: `${data.schemaPath}/optionalProperties/${key}`,
|
|
1264
|
-
errors: data.errors
|
|
1265
|
-
});
|
|
1266
|
-
continue;
|
|
1267
|
-
}
|
|
1268
|
-
if (typeof val !== "undefined") {
|
|
1269
|
-
result[key] = prop.metadata[SCHEMA_METADATA].parse(val, {
|
|
1270
|
-
instancePath: `${data.instancePath}/${key}`,
|
|
1271
|
-
schemaPath: `${data.schemaPath}/optionalProperties/${key}`,
|
|
1272
|
-
errors: data.errors
|
|
1273
|
-
});
|
|
1274
|
-
}
|
|
1275
|
-
}
|
|
1276
|
-
if (data.errors.length) {
|
|
1277
|
-
return undefined;
|
|
1278
|
-
}
|
|
1279
|
-
return result;
|
|
1280
|
-
}
|
|
1281
|
-
function validateObjectSchema(schema, input) {
|
|
1282
|
-
if (!isObject(input)) {
|
|
1283
|
-
return false;
|
|
1284
|
-
}
|
|
1285
|
-
const allowedKeys = schema.strict ? [] : undefined;
|
|
1286
|
-
for (const key of Object.keys(schema.properties)) {
|
|
1287
|
-
const propSchema = schema.properties[key];
|
|
1288
|
-
const isValid = propSchema.metadata[SCHEMA_METADATA].validate(
|
|
1289
|
-
input[key]
|
|
1290
|
-
);
|
|
1291
|
-
if (!isValid) return false;
|
|
1292
|
-
allowedKeys?.push(key);
|
|
1293
|
-
}
|
|
1294
|
-
if (schema.optionalProperties) {
|
|
1295
|
-
for (const key of Object.keys(schema.optionalProperties)) {
|
|
1296
|
-
const propsSchema = schema.optionalProperties[key];
|
|
1297
|
-
const isValid = propsSchema.metadata[SCHEMA_METADATA].validate(
|
|
1298
|
-
input[key]
|
|
1299
|
-
);
|
|
1300
|
-
if (!isValid) return false;
|
|
1301
|
-
allowedKeys?.push(key);
|
|
1302
|
-
}
|
|
1303
|
-
}
|
|
1304
|
-
if (allowedKeys) {
|
|
1305
|
-
for (const key of Object.keys(input)) {
|
|
1306
|
-
if (!allowedKeys.includes(key)) return false;
|
|
1307
|
-
}
|
|
1308
|
-
}
|
|
1309
|
-
return true;
|
|
1310
|
-
}
|
|
1311
|
-
function pick(inputSchema, keys, options = {}) {
|
|
1312
|
-
const schema = {
|
|
1313
|
-
properties: {},
|
|
1314
|
-
nullable: inputSchema.nullable
|
|
1315
|
-
};
|
|
1316
|
-
if (typeof options.strict === "boolean") {
|
|
1317
|
-
schema.strict = options.strict;
|
|
1318
|
-
}
|
|
1319
|
-
Object.keys(inputSchema.properties).forEach((key) => {
|
|
1320
|
-
if (!schema.properties) {
|
|
1321
|
-
return;
|
|
1322
|
-
}
|
|
1323
|
-
if (keys.includes(key)) {
|
|
1324
|
-
schema.properties[key] = inputSchema.properties[key];
|
|
1325
|
-
}
|
|
1326
|
-
});
|
|
1327
|
-
if (inputSchema.optionalProperties) {
|
|
1328
|
-
schema.optionalProperties = {};
|
|
1329
|
-
Object.keys(inputSchema.optionalProperties).forEach((key) => {
|
|
1330
|
-
if (!schema.optionalProperties || !inputSchema.optionalProperties) {
|
|
1331
|
-
return;
|
|
1332
|
-
}
|
|
1333
|
-
if (keys.includes(key)) {
|
|
1334
|
-
schema.optionalProperties[key] = inputSchema.optionalProperties[key];
|
|
1335
|
-
}
|
|
1336
|
-
});
|
|
1337
|
-
}
|
|
1338
|
-
const parse = (input, context) => {
|
|
1339
|
-
return parseObjectSchema(schema, input, context, false);
|
|
1340
|
-
};
|
|
1341
|
-
const validate = (input) => {
|
|
1342
|
-
return validateObjectSchema(schema, input);
|
|
1343
|
-
};
|
|
1344
|
-
const result = {
|
|
1345
|
-
...schema,
|
|
1346
|
-
metadata: {
|
|
1347
|
-
id: options.id,
|
|
1348
|
-
description: options.description,
|
|
1349
|
-
isDeprecated: options.isDeprecated,
|
|
1350
|
-
[SCHEMA_METADATA]: {
|
|
1351
|
-
output: {},
|
|
1352
|
-
parse,
|
|
1353
|
-
coerce(input, context) {
|
|
1354
|
-
return parseObjectSchema(
|
|
1355
|
-
schema,
|
|
1356
|
-
input,
|
|
1357
|
-
context,
|
|
1358
|
-
false
|
|
1359
|
-
);
|
|
1360
|
-
},
|
|
1361
|
-
validate,
|
|
1362
|
-
serialize(input, context) {
|
|
1363
|
-
return serializeObject(schema, input, context);
|
|
1364
|
-
}
|
|
1365
|
-
}
|
|
1366
|
-
},
|
|
1367
|
-
"~standard": createStandardSchemaProperty(validate, parse)
|
|
1368
|
-
};
|
|
1369
|
-
hideInvalidProperties(result);
|
|
1370
|
-
return result;
|
|
1371
|
-
}
|
|
1372
|
-
function omit(inputSchema, keys, options = {}) {
|
|
1373
|
-
const schema = {
|
|
1374
|
-
properties: {
|
|
1375
|
-
...inputSchema.properties
|
|
1376
|
-
},
|
|
1377
|
-
nullable: inputSchema.nullable
|
|
1378
|
-
};
|
|
1379
|
-
if (typeof options.strict === "boolean") {
|
|
1380
|
-
schema.strict = options.strict;
|
|
1381
|
-
}
|
|
1382
|
-
Object.keys(inputSchema.properties).forEach((key) => {
|
|
1383
|
-
if (keys.includes(key)) {
|
|
1384
|
-
delete schema.properties[key];
|
|
1385
|
-
}
|
|
1386
|
-
});
|
|
1387
|
-
if (inputSchema.optionalProperties) {
|
|
1388
|
-
schema.optionalProperties = {
|
|
1389
|
-
...inputSchema.optionalProperties
|
|
1390
|
-
};
|
|
1391
|
-
Object.keys(inputSchema.optionalProperties).forEach((key) => {
|
|
1392
|
-
if (!schema.optionalProperties) {
|
|
1393
|
-
return;
|
|
1394
|
-
}
|
|
1395
|
-
if (keys.includes(key)) {
|
|
1396
|
-
delete schema.optionalProperties[key];
|
|
1397
|
-
}
|
|
1398
|
-
});
|
|
1399
|
-
}
|
|
1400
|
-
const validate = (input) => validateObjectSchema(schema, input);
|
|
1401
|
-
const parse = (input, context) => parseObjectSchema(schema, input, context, false);
|
|
1402
|
-
const result = {
|
|
1403
|
-
...schema,
|
|
1404
|
-
metadata: {
|
|
1405
|
-
id: options.id,
|
|
1406
|
-
description: options.description,
|
|
1407
|
-
isDeprecated: options.isDeprecated,
|
|
1408
|
-
[SCHEMA_METADATA]: {
|
|
1409
|
-
output: {},
|
|
1410
|
-
validate,
|
|
1411
|
-
parse,
|
|
1412
|
-
serialize(input, context) {
|
|
1413
|
-
return serializeObject(schema, input, context);
|
|
1414
|
-
},
|
|
1415
|
-
coerce(input, context) {
|
|
1416
|
-
return parseObjectSchema(
|
|
1417
|
-
schema,
|
|
1418
|
-
input,
|
|
1419
|
-
context,
|
|
1420
|
-
true
|
|
1421
|
-
);
|
|
1422
|
-
}
|
|
1423
|
-
}
|
|
1424
|
-
},
|
|
1425
|
-
"~standard": createStandardSchemaProperty(validate, parse)
|
|
1426
|
-
};
|
|
1427
|
-
hideInvalidProperties(result);
|
|
1428
|
-
return result;
|
|
1429
|
-
}
|
|
1430
|
-
function serializeObject(schema, input, data) {
|
|
1431
|
-
const strParts = [];
|
|
1432
|
-
if (data.discriminatorKey && data.discriminatorValue) {
|
|
1433
|
-
strParts.push(
|
|
1434
|
-
`"${data.discriminatorKey}":"${data.discriminatorValue}"`
|
|
1435
|
-
);
|
|
1436
|
-
}
|
|
1437
|
-
for (const key of Object.keys(schema.properties)) {
|
|
1438
|
-
const prop = schema.properties[key];
|
|
1439
|
-
const val = input[key];
|
|
1440
|
-
if (typeof val !== "undefined") {
|
|
1441
|
-
strParts.push(
|
|
1442
|
-
`"${key}":${prop.metadata[SCHEMA_METADATA].serialize(val, {
|
|
1443
|
-
instancePath: `${data.instancePath}/${key}`,
|
|
1444
|
-
schemaPath: `${data.schemaPath}/properties/${key}`,
|
|
1445
|
-
errors: data.errors
|
|
1446
|
-
})}`
|
|
1447
|
-
);
|
|
1448
|
-
}
|
|
1449
|
-
}
|
|
1450
|
-
if (schema.optionalProperties) {
|
|
1451
|
-
for (const key of Object.keys(schema.optionalProperties)) {
|
|
1452
|
-
const prop = schema.optionalProperties[key];
|
|
1453
|
-
const val = input[key];
|
|
1454
|
-
if (typeof val !== "undefined") {
|
|
1455
|
-
strParts.push(
|
|
1456
|
-
`"${key}":${prop.metadata[SCHEMA_METADATA].serialize(val, {
|
|
1457
|
-
instancePath: `${data.instancePath}/${key}`,
|
|
1458
|
-
schemaPath: `${data.schemaPath}/optionalProperties/${key}`,
|
|
1459
|
-
errors: data.errors
|
|
1460
|
-
})}`
|
|
1461
|
-
);
|
|
1462
|
-
}
|
|
1463
|
-
}
|
|
1464
|
-
}
|
|
1465
|
-
return `{${strParts.join(",")}}`;
|
|
1466
|
-
}
|
|
1467
|
-
function extend(baseSchema, inputSchema, options = {}) {
|
|
1468
|
-
const schema = {
|
|
1469
|
-
properties: {
|
|
1470
|
-
...baseSchema.properties,
|
|
1471
|
-
...inputSchema.properties
|
|
1472
|
-
},
|
|
1473
|
-
optionalProperties: {
|
|
1474
|
-
...baseSchema.optionalProperties,
|
|
1475
|
-
...inputSchema.optionalProperties
|
|
1476
|
-
}
|
|
1477
|
-
};
|
|
1478
|
-
if (typeof options.strict === "boolean") {
|
|
1479
|
-
schema.strict = options.strict;
|
|
1480
|
-
}
|
|
1481
|
-
const isType = (input) => validateObjectSchema(schema, input);
|
|
1482
|
-
const meta = {
|
|
1483
|
-
id: options.id,
|
|
1484
|
-
description: options.description,
|
|
1485
|
-
[SCHEMA_METADATA]: {
|
|
1486
|
-
output: {},
|
|
1487
|
-
parse(input, context) {
|
|
1488
|
-
return parseObjectSchema(schema, input, context, false);
|
|
1489
|
-
},
|
|
1490
|
-
coerce(input, context) {
|
|
1491
|
-
return parseObjectSchema(schema, input, context, true);
|
|
1492
|
-
},
|
|
1493
|
-
validate: isType,
|
|
1494
|
-
serialize(input, context) {
|
|
1495
|
-
return serializeObject(schema, input, context);
|
|
1496
|
-
}
|
|
1497
|
-
}
|
|
1498
|
-
};
|
|
1499
|
-
schema.metadata = meta;
|
|
1500
|
-
return schema;
|
|
1501
|
-
}
|
|
1502
|
-
function partial(schema, options = {}) {
|
|
1503
|
-
const newSchema = {
|
|
1504
|
-
properties: {},
|
|
1505
|
-
optionalProperties: {},
|
|
1506
|
-
nullable: schema.nullable
|
|
1507
|
-
};
|
|
1508
|
-
if (typeof options.strict === "boolean") {
|
|
1509
|
-
schema.strict = options.strict;
|
|
1510
|
-
}
|
|
1511
|
-
for (const key of Object.keys(schema.properties)) {
|
|
1512
|
-
const prop = schema.properties[key];
|
|
1513
|
-
newSchema.optionalProperties[key] = optional(prop);
|
|
1514
|
-
}
|
|
1515
|
-
if (schema.optionalProperties && newSchema.optionalProperties) {
|
|
1516
|
-
for (const key of Object.keys(schema.optionalProperties)) {
|
|
1517
|
-
const prop = schema.optionalProperties[key];
|
|
1518
|
-
if (prop.metadata[SCHEMA_METADATA].optional) {
|
|
1519
|
-
newSchema.optionalProperties[key] = prop;
|
|
1520
|
-
} else {
|
|
1521
|
-
newSchema.optionalProperties[key] = optional(prop);
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
}
|
|
1525
|
-
const validate = (input) => {
|
|
1526
|
-
return validateObjectSchema(newSchema, input);
|
|
1527
|
-
};
|
|
1528
|
-
const parse = (input, context) => {
|
|
1529
|
-
return parseObjectSchema(newSchema, input, context, false);
|
|
1530
|
-
};
|
|
1531
|
-
const meta = {
|
|
1532
|
-
id: options.id,
|
|
1533
|
-
description: options.description,
|
|
1534
|
-
[SCHEMA_METADATA]: {
|
|
1535
|
-
output: {},
|
|
1536
|
-
optional: schema.metadata[SCHEMA_METADATA].optional,
|
|
1537
|
-
validate,
|
|
1538
|
-
parse,
|
|
1539
|
-
coerce(input, context) {
|
|
1540
|
-
return parseObjectSchema(
|
|
1541
|
-
newSchema,
|
|
1542
|
-
input,
|
|
1543
|
-
context,
|
|
1544
|
-
true
|
|
1545
|
-
);
|
|
1546
|
-
},
|
|
1547
|
-
serialize(input, context) {
|
|
1548
|
-
return serializeObject(schema, input, context);
|
|
1549
|
-
}
|
|
1550
|
-
}
|
|
1551
|
-
};
|
|
1552
|
-
newSchema.metadata = meta;
|
|
1553
|
-
newSchema["~standard"] = createStandardSchemaProperty(
|
|
1554
|
-
validate,
|
|
1555
|
-
parse
|
|
1556
|
-
);
|
|
1557
|
-
hideInvalidProperties(newSchema);
|
|
1558
|
-
return newSchema;
|
|
1559
|
-
}
|
|
1560
|
-
|
|
1561
|
-
function string(opts = {}) {
|
|
1562
|
-
const result = {
|
|
1563
|
-
type: "string",
|
|
1564
|
-
metadata: {
|
|
1565
|
-
id: opts.id,
|
|
1566
|
-
description: opts.description,
|
|
1567
|
-
isDeprecated: opts.isDeprecated,
|
|
1568
|
-
[SCHEMA_METADATA]: {
|
|
1569
|
-
output: "",
|
|
1570
|
-
parse: parse$2,
|
|
1571
|
-
coerce: coerce$1,
|
|
1572
|
-
validate: validate$1,
|
|
1573
|
-
serialize(input, context) {
|
|
1574
|
-
if (context.instancePath.length === 0) {
|
|
1575
|
-
return input;
|
|
1576
|
-
}
|
|
1577
|
-
if (input.length < 42) {
|
|
1578
|
-
return serializeSmallString(input);
|
|
1579
|
-
}
|
|
1580
|
-
if (input.length < 5e3 && !STR_ESCAPE.test(input)) {
|
|
1581
|
-
return `"${input}"`;
|
|
1582
|
-
}
|
|
1583
|
-
return JSON.stringify(input);
|
|
1584
|
-
}
|
|
1585
|
-
}
|
|
1586
|
-
},
|
|
1587
|
-
"~standard": createStandardSchemaProperty(validate$1, parse$2)
|
|
1588
|
-
};
|
|
1589
|
-
hideInvalidProperties(result);
|
|
1590
|
-
return result;
|
|
1591
|
-
}
|
|
1592
|
-
function serializeString(input) {
|
|
1593
|
-
if (input.length < 42) {
|
|
1594
|
-
return serializeSmallString(input);
|
|
1595
|
-
}
|
|
1596
|
-
if (input.length < 5e3 && !STR_ESCAPE.test(input)) {
|
|
1597
|
-
return `"${input}"`;
|
|
1598
|
-
}
|
|
1599
|
-
return JSON.stringify(input);
|
|
1600
|
-
}
|
|
1601
|
-
function validate$1(input) {
|
|
1602
|
-
return typeof input === "string";
|
|
1603
|
-
}
|
|
1604
|
-
function parse$2(input, context) {
|
|
1605
|
-
if (validate$1(input)) {
|
|
1606
|
-
return input;
|
|
1607
|
-
}
|
|
1608
|
-
context.errors.push({
|
|
1609
|
-
instancePath: context.instancePath,
|
|
1610
|
-
schemaPath: `${context.schemaPath}/type`,
|
|
1611
|
-
message: `Error at ${context.instancePath}. Expected 'string' got ${typeof input}.`
|
|
1612
|
-
});
|
|
1613
|
-
return undefined;
|
|
1614
|
-
}
|
|
1615
|
-
function coerce$1(input, context) {
|
|
1616
|
-
return parse$2(input, context);
|
|
1617
|
-
}
|
|
1618
|
-
const STR_ESCAPE = (
|
|
1619
|
-
// eslint-disable-next-line no-control-regex
|
|
1620
|
-
/[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/
|
|
1621
|
-
);
|
|
1622
|
-
function serializeSmallString(input) {
|
|
1623
|
-
const len = input.length;
|
|
1624
|
-
let result = "";
|
|
1625
|
-
let last = -1;
|
|
1626
|
-
let point = 255;
|
|
1627
|
-
for (let i = 0; i < len; i++) {
|
|
1628
|
-
point = input.charCodeAt(i);
|
|
1629
|
-
if (point < 32 || point >= 55296 && point <= 57343) {
|
|
1630
|
-
return JSON.stringify(input);
|
|
1631
|
-
}
|
|
1632
|
-
if (point === 34 || // '"'
|
|
1633
|
-
point === 92) {
|
|
1634
|
-
if (last === -1) {
|
|
1635
|
-
last = 0;
|
|
1636
|
-
}
|
|
1637
|
-
result += input.slice(last, i) + "\\";
|
|
1638
|
-
last = i;
|
|
1639
|
-
}
|
|
1640
|
-
}
|
|
1641
|
-
if (last === -1) {
|
|
1642
|
-
return `"${input}"`;
|
|
1643
|
-
}
|
|
1644
|
-
return `"${result}${input.slice(last)}"`;
|
|
1645
|
-
}
|
|
1646
|
-
|
|
1647
|
-
function record(schema, opts = {}) {
|
|
1648
|
-
const validateFn = (input) => {
|
|
1649
|
-
if (!isObject(input)) {
|
|
1650
|
-
return false;
|
|
1651
|
-
}
|
|
1652
|
-
for (const key of Object.keys(input)) {
|
|
1653
|
-
const val = input[key];
|
|
1654
|
-
const isValid = schema.metadata[SCHEMA_METADATA].validate(val);
|
|
1655
|
-
if (!isValid) {
|
|
1656
|
-
return false;
|
|
1657
|
-
}
|
|
1658
|
-
}
|
|
1659
|
-
return true;
|
|
1660
|
-
};
|
|
1661
|
-
const parseFn = (input, ctx) => {
|
|
1662
|
-
return parse$1(schema, input, ctx, false);
|
|
1663
|
-
};
|
|
1664
|
-
const result = {
|
|
1665
|
-
values: schema,
|
|
1666
|
-
metadata: {
|
|
1667
|
-
id: opts.id,
|
|
1668
|
-
description: opts.description,
|
|
1669
|
-
isDeprecated: opts.isDeprecated,
|
|
1670
|
-
[SCHEMA_METADATA]: {
|
|
1671
|
-
output: {},
|
|
1672
|
-
validate: validateFn,
|
|
1673
|
-
parse: parseFn,
|
|
1674
|
-
coerce(input, data) {
|
|
1675
|
-
return parse$1(schema, input, data, true);
|
|
1676
|
-
},
|
|
1677
|
-
serialize(input, context) {
|
|
1678
|
-
let result2 = "{";
|
|
1679
|
-
const keys = Object.keys(input);
|
|
1680
|
-
for (let i = 0; i < keys.length; i++) {
|
|
1681
|
-
const key = keys[i];
|
|
1682
|
-
const val = input[key];
|
|
1683
|
-
if (i > 0) result2 += ",";
|
|
1684
|
-
result2 += serializeString(key);
|
|
1685
|
-
result2 += ":";
|
|
1686
|
-
result2 += schema.metadata[SCHEMA_METADATA].serialize(
|
|
1687
|
-
val,
|
|
1688
|
-
{
|
|
1689
|
-
instancePath: `${context.instancePath}/${key}`,
|
|
1690
|
-
schemaPath: `${context.schemaPath}/values`,
|
|
1691
|
-
errors: context.errors
|
|
1692
|
-
}
|
|
1693
|
-
);
|
|
1694
|
-
}
|
|
1695
|
-
result2 += "}";
|
|
1696
|
-
return result2;
|
|
1697
|
-
}
|
|
1698
|
-
}
|
|
1699
|
-
},
|
|
1700
|
-
"~standard": createStandardSchemaProperty(validateFn, parseFn)
|
|
1701
|
-
};
|
|
1702
|
-
hideInvalidProperties(result);
|
|
1703
|
-
return result;
|
|
1704
|
-
}
|
|
1705
|
-
function parse$1(schema, input, data, coerce = false) {
|
|
1706
|
-
let parsedInput = input;
|
|
1707
|
-
if (data.instancePath.length === 0 && typeof input === "string") {
|
|
1708
|
-
parsedInput = JSON.parse(input);
|
|
1709
|
-
}
|
|
1710
|
-
if (!isObject(parsedInput)) {
|
|
1711
|
-
data.errors.push({
|
|
1712
|
-
instancePath: data.instancePath,
|
|
1713
|
-
schemaPath: `${data.schemaPath}/values`,
|
|
1714
|
-
message: `Error at ${data.instancePath} Expected object`
|
|
1715
|
-
});
|
|
1716
|
-
return undefined;
|
|
1717
|
-
}
|
|
1718
|
-
const result = {};
|
|
1719
|
-
for (const key of Object.keys(parsedInput)) {
|
|
1720
|
-
const val = parsedInput[key];
|
|
1721
|
-
if (coerce) {
|
|
1722
|
-
result[key] = schema.metadata[SCHEMA_METADATA].coerce(val, {
|
|
1723
|
-
instancePath: `${data.instancePath}/${key}`,
|
|
1724
|
-
schemaPath: `${data.schemaPath}/values`,
|
|
1725
|
-
errors: data.errors
|
|
1726
|
-
});
|
|
1727
|
-
} else {
|
|
1728
|
-
result[key] = schema.metadata[SCHEMA_METADATA].parse(val, {
|
|
1729
|
-
instancePath: `${data.instancePath}/${key}`,
|
|
1730
|
-
schemaPath: `${data.schemaPath}/values`,
|
|
1731
|
-
errors: data.errors
|
|
1732
|
-
});
|
|
1733
|
-
}
|
|
1734
|
-
}
|
|
1735
|
-
if (data.errors.length) {
|
|
1736
|
-
return undefined;
|
|
1737
|
-
}
|
|
1738
|
-
return result;
|
|
1739
|
-
}
|
|
1740
|
-
|
|
1741
|
-
let recursiveTypeCount = 0;
|
|
1742
|
-
function recursive(propA, propB) {
|
|
1743
|
-
const isIdShorthand = typeof propA === "string";
|
|
1744
|
-
const callback = isIdShorthand ? propB : propA;
|
|
1745
|
-
const options = isIdShorthand ? { id: propA } : propB ?? {};
|
|
1746
|
-
const recursiveFns = {};
|
|
1747
|
-
if (!options.id) {
|
|
1748
|
-
recursiveTypeCount++;
|
|
1749
|
-
console.warn(
|
|
1750
|
-
`[@arrirpc/schema] WARNING: It is highly recommended to specify an ID for recursive types.`
|
|
1751
|
-
);
|
|
1752
|
-
}
|
|
1753
|
-
const id = options.id ?? `TypeRef${recursiveTypeCount}`;
|
|
1754
|
-
const metadata = {
|
|
1755
|
-
[SCHEMA_METADATA]: {
|
|
1756
|
-
output: "",
|
|
1757
|
-
parse(input, context) {
|
|
1758
|
-
if (recursiveFns[id]) {
|
|
1759
|
-
return recursiveFns[id].parse(input, context);
|
|
1760
|
-
}
|
|
1761
|
-
},
|
|
1762
|
-
serialize(input, context) {
|
|
1763
|
-
if (recursiveFns[id]) {
|
|
1764
|
-
return recursiveFns[id].serialize(input, context);
|
|
1765
|
-
}
|
|
1766
|
-
return "";
|
|
1767
|
-
},
|
|
1768
|
-
validate(input) {
|
|
1769
|
-
if (recursiveFns[id]) {
|
|
1770
|
-
return recursiveFns[id].validate(input);
|
|
1771
|
-
}
|
|
1772
|
-
return false;
|
|
1773
|
-
},
|
|
1774
|
-
coerce(input, context) {
|
|
1775
|
-
if (recursiveFns[id]) {
|
|
1776
|
-
return recursiveFns[id].coerce(input, context);
|
|
1777
|
-
}
|
|
1778
|
-
}
|
|
1779
|
-
}
|
|
1780
|
-
};
|
|
1781
|
-
const schema = {
|
|
1782
|
-
ref: id,
|
|
1783
|
-
metadata,
|
|
1784
|
-
"~standard": createStandardSchemaProperty(
|
|
1785
|
-
metadata[SCHEMA_METADATA].validate,
|
|
1786
|
-
metadata[SCHEMA_METADATA].parse
|
|
1787
|
-
)
|
|
1788
|
-
};
|
|
1789
|
-
hideInvalidProperties(schema);
|
|
1790
|
-
const mainSchema = callback(schema);
|
|
1791
|
-
mainSchema.metadata.id = id;
|
|
1792
|
-
mainSchema.metadata.description = options?.description ?? mainSchema.metadata.description;
|
|
1793
|
-
mainSchema.metadata.isDeprecated = options?.isDeprecated ?? mainSchema.metadata.isDeprecated;
|
|
1794
|
-
recursiveFns[id] = {
|
|
1795
|
-
validate: mainSchema.metadata[SCHEMA_METADATA].validate,
|
|
1796
|
-
parse: mainSchema.metadata[SCHEMA_METADATA].parse,
|
|
1797
|
-
serialize: mainSchema.metadata[SCHEMA_METADATA].serialize,
|
|
1798
|
-
coerce: mainSchema.metadata[SCHEMA_METADATA].coerce
|
|
1799
|
-
};
|
|
1800
|
-
return mainSchema;
|
|
1801
|
-
}
|
|
1802
|
-
|
|
1803
|
-
function timestamp(opts = {}) {
|
|
1804
|
-
const result = {
|
|
1805
|
-
type: "timestamp",
|
|
1806
|
-
metadata: {
|
|
1807
|
-
id: opts.id,
|
|
1808
|
-
description: opts.description,
|
|
1809
|
-
isDeprecated: opts.isDeprecated,
|
|
1810
|
-
[SCHEMA_METADATA]: {
|
|
1811
|
-
output: /* @__PURE__ */ new Date(),
|
|
1812
|
-
validate,
|
|
1813
|
-
parse,
|
|
1814
|
-
coerce,
|
|
1815
|
-
serialize
|
|
1816
|
-
}
|
|
1817
|
-
},
|
|
1818
|
-
"~standard": createStandardSchemaProperty(validate, parse)
|
|
1819
|
-
};
|
|
1820
|
-
hideInvalidProperties(result);
|
|
1821
|
-
return result;
|
|
1822
|
-
}
|
|
1823
|
-
function validate(input) {
|
|
1824
|
-
return typeof input === "object" && input instanceof Date;
|
|
1825
|
-
}
|
|
1826
|
-
function parse(input, data) {
|
|
1827
|
-
if (validate(input)) {
|
|
1828
|
-
return input;
|
|
1829
|
-
}
|
|
1830
|
-
if (typeof input === "string") {
|
|
1831
|
-
const result = Date.parse(input);
|
|
1832
|
-
if (Number.isNaN(result)) {
|
|
1833
|
-
data?.errors.push({
|
|
1834
|
-
message: `Error at ${data.instancePath}. Invalid date string.`,
|
|
1835
|
-
instancePath: data.instancePath,
|
|
1836
|
-
schemaPath: `${data.schemaPath}/type`
|
|
1837
|
-
});
|
|
1838
|
-
return undefined;
|
|
1839
|
-
}
|
|
1840
|
-
return new Date(result);
|
|
1841
|
-
}
|
|
1842
|
-
data.errors.push({
|
|
1843
|
-
message: `Error at ${data.instancePath}. Invalid date.`,
|
|
1844
|
-
instancePath: data.instancePath,
|
|
1845
|
-
schemaPath: `${data.schemaPath}/type`
|
|
1846
|
-
});
|
|
1847
|
-
return undefined;
|
|
1848
|
-
}
|
|
1849
|
-
function coerce(input, options) {
|
|
1850
|
-
if (typeof input === "number") {
|
|
1851
|
-
return new Date(input);
|
|
1852
|
-
}
|
|
1853
|
-
return parse(input, options);
|
|
1854
|
-
}
|
|
1855
|
-
function serialize(input, data) {
|
|
1856
|
-
if (data.instancePath.length === 0) {
|
|
1857
|
-
return input.toISOString();
|
|
1858
|
-
}
|
|
1859
|
-
return `"${input.toISOString()}"`;
|
|
1860
|
-
}
|
|
1861
|
-
|
|
1862
|
-
exports.NumberTypeValues = NumberTypeValues;
|
|
1863
|
-
exports.SCHEMA_METADATA = SCHEMA_METADATA;
|
|
1864
|
-
exports.STR_ESCAPE = STR_ESCAPE;
|
|
1865
|
-
exports.ValidationError = ValidationError;
|
|
1866
|
-
exports.any = any;
|
|
1867
|
-
exports.array = array;
|
|
1868
|
-
exports.boolean = boolean;
|
|
1869
|
-
exports.clone = clone;
|
|
1870
|
-
exports.coerce = coerce$2;
|
|
1871
|
-
exports.createStandardSchemaProperty = createStandardSchemaProperty;
|
|
1872
|
-
exports.discriminator = discriminator;
|
|
1873
|
-
exports.enumerator = enumerator;
|
|
1874
|
-
exports.errors = errors;
|
|
1875
|
-
exports.extend = extend;
|
|
1876
|
-
exports.float32 = float32;
|
|
1877
|
-
exports.float64 = float64;
|
|
1878
|
-
exports.hideInvalidProperties = hideInvalidProperties;
|
|
1879
|
-
exports.int16 = int16;
|
|
1880
|
-
exports.int16Max = int16Max;
|
|
1881
|
-
exports.int16Min = int16Min;
|
|
1882
|
-
exports.int32 = int32;
|
|
1883
|
-
exports.int32Max = int32Max;
|
|
1884
|
-
exports.int32Min = int32Min;
|
|
1885
|
-
exports.int64 = int64;
|
|
1886
|
-
exports.int64Max = int64Max;
|
|
1887
|
-
exports.int64Min = int64Min;
|
|
1888
|
-
exports.int8 = int8;
|
|
1889
|
-
exports.int8Max = int8Max;
|
|
1890
|
-
exports.int8Min = int8Min;
|
|
1891
|
-
exports.isAAraySchema = isAAraySchema;
|
|
1892
|
-
exports.isADiscriminatorSchema = isADiscriminatorSchema;
|
|
1893
|
-
exports.isAObjectSchema = isAObjectSchema;
|
|
1894
|
-
exports.isARecordSchema = isARecordSchema;
|
|
1895
|
-
exports.isARefSchema = isARefSchema;
|
|
1896
|
-
exports.isAScalarSchema = isAScalarSchema;
|
|
1897
|
-
exports.isASchema = isASchema;
|
|
1898
|
-
exports.isAStringEnumSchema = isAStringEnumSchema;
|
|
1899
|
-
exports.isObject = isObject;
|
|
1900
|
-
exports.isValidationError = isValidationError;
|
|
1901
|
-
exports.nullable = nullable;
|
|
1902
|
-
exports.number = number;
|
|
1903
|
-
exports.object = object;
|
|
1904
|
-
exports.omit = omit;
|
|
1905
|
-
exports.optional = optional;
|
|
1906
|
-
exports.parse = parse$4;
|
|
1907
|
-
exports.parseObjectSchema = parseObjectSchema;
|
|
1908
|
-
exports.partial = partial;
|
|
1909
|
-
exports.pick = pick;
|
|
1910
|
-
exports.record = record;
|
|
1911
|
-
exports.recursive = recursive;
|
|
1912
|
-
exports.safeCoerce = safeCoerce;
|
|
1913
|
-
exports.safeParse = safeParse;
|
|
1914
|
-
exports.sanitizeJson = sanitizeJson;
|
|
1915
|
-
exports.serialize = serialize$1;
|
|
1916
|
-
exports.serializeObject = serializeObject;
|
|
1917
|
-
exports.serializeSmallString = serializeSmallString;
|
|
1918
|
-
exports.serializeString = serializeString;
|
|
1919
|
-
exports.string = string;
|
|
1920
|
-
exports.stringEnum = stringEnum;
|
|
1921
|
-
exports.timestamp = timestamp;
|
|
1922
|
-
exports.uint16 = uint16;
|
|
1923
|
-
exports.uint16Max = uint16Max;
|
|
1924
|
-
exports.uint16Min = uint16Min;
|
|
1925
|
-
exports.uint32 = uint32;
|
|
1926
|
-
exports.uint32Max = uint32Max;
|
|
1927
|
-
exports.uint32Min = uint32Min;
|
|
1928
|
-
exports.uint64 = uint64;
|
|
1929
|
-
exports.uint64Max = uint64Max;
|
|
1930
|
-
exports.uint64Min = uint64Min;
|
|
1931
|
-
exports.uint8 = uint8;
|
|
1932
|
-
exports.uint8Max = uint8Max;
|
|
1933
|
-
exports.uint8Min = uint8Min;
|
|
1934
|
-
exports.validate = validate$3;
|
|
1935
|
-
exports.validateObjectSchema = validateObjectSchema;
|