@arrirpc/schema 0.73.0 → 0.74.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,13 +1,34 @@
1
1
  'use strict';
2
2
 
3
3
  const typeDefs = require('@arrirpc/type-defs');
4
+ const adapters = require('./shared/schema.Cwqn0t9l.cjs');
4
5
  const scule = require('scule');
5
- const timestamp = require('./shared/schema.COiEWm_0.cjs');
6
6
 
7
- function createParsingTemplate(input, schema) {
7
+ const int8Min = -128;
8
+ const int8Max = 127;
9
+ const uint8Min = 0;
10
+ const uint8Max = 255;
11
+ const int16Min = -32768;
12
+ const int16Max = 32767;
13
+ const uint16Min = 0;
14
+ const uint16Max = 65535;
15
+ const int32Min = -2147483648;
16
+ const int32Max = 2147483647;
17
+ const uint32Min = 0;
18
+ const uint32Max = 4294967295;
19
+ const int64Min = BigInt("-9223372036854775808");
20
+ const int64Max = BigInt("9223372036854775807");
21
+ const uint64Min = BigInt("0");
22
+ const uint64Max = BigInt("18446744073709551615");
23
+
24
+ function createParsingTemplate(input, schema, shouldCoerce = false) {
8
25
  const fallbackTemplate = `
9
- function $fallback(instancePath, schemaPath) {
10
- throw new Error(\`Error parsing input. InstancePath: "\${instancePath}". SchemaPath: "\${schemaPath}"\`);
26
+ function $fallback(instancePath, schemaPath, message) {
27
+ context.errors.push({
28
+ message: message,
29
+ instancePath: instancePath,
30
+ schemaPath: schemaPath,
31
+ })
11
32
  }`;
12
33
  let jsonParseCheck = "";
13
34
  const subFunctions = {};
@@ -17,7 +38,8 @@ function createParsingTemplate(input, schema) {
17
38
  schema,
18
39
  instancePath: "",
19
40
  schemaPath: "",
20
- subFunctions
41
+ subFunctions,
42
+ shouldCoerce
21
43
  });
22
44
  const jsonTemplate = schemaTemplate$1({
23
45
  val: "json",
@@ -25,7 +47,8 @@ function createParsingTemplate(input, schema) {
25
47
  schema,
26
48
  instancePath: "",
27
49
  schemaPath: "",
28
- subFunctions
50
+ subFunctions,
51
+ shouldCoerce
29
52
  });
30
53
  if (typeDefs.isSchemaFormProperties(schema) || typeDefs.isSchemaFormValues(schema) || typeDefs.isSchemaFormElements(schema) || typeDefs.isSchemaFormDiscriminator(schema)) {
31
54
  jsonParseCheck = `if (typeof ${input} === 'string') {
@@ -67,17 +90,17 @@ function schemaTemplate$1(input) {
67
90
  case "float32":
68
91
  return floatTemplate$1(input);
69
92
  case "int32":
70
- return intTemplate$1(input, timestamp.int32Min, timestamp.int32Max);
93
+ return intTemplate$1(input, int32Min, int32Max);
71
94
  case "int16":
72
- return intTemplate$1(input, timestamp.int16Min, timestamp.int16Max);
95
+ return intTemplate$1(input, int16Min, int16Max);
73
96
  case "int8":
74
- return intTemplate$1(input, timestamp.int8Min, timestamp.int8Max);
97
+ return intTemplate$1(input, int8Min, int8Max);
75
98
  case "uint32":
76
- return intTemplate$1(input, timestamp.uint32Min, timestamp.uint32Max);
99
+ return intTemplate$1(input, uint32Min, uint32Max);
77
100
  case "uint16":
78
- return intTemplate$1(input, timestamp.uint16Min, timestamp.uint16Max);
101
+ return intTemplate$1(input, uint16Min, uint16Max);
79
102
  case "uint8":
80
- return intTemplate$1(input, timestamp.uint8Min, timestamp.uint8Max);
103
+ return intTemplate$1(input, uint8Min, uint8Max);
81
104
  case "int64":
82
105
  return bigIntTemplate$2(input, false);
83
106
  case "uint64":
@@ -115,8 +138,27 @@ function anyTemplate$1(input) {
115
138
  return `${input.targetVal} = ${input.val}`;
116
139
  }
117
140
  function booleanTemplate$2(input) {
118
- const errorMessage = input.instancePath.length ? `Expected boolean for ${input.instancePath}` : `Expected boolean`;
141
+ const errorMessage = "expected boolean";
119
142
  const templateParts = [];
143
+ if (input.shouldCoerce) {
144
+ templateParts.push(`if (${input.val} === true || ${input.val} === 'true' || ${input.val} === 'TRUE' || ${input.val} === 1 || ${input.val} === '1') {
145
+ ${input.targetVal} = true;
146
+ }
147
+ else if (${input.val} === false || ${input.val} === 'false' || ${input.val} === 'FALSE' || ${input.val} === 0 || ${input.val} === '0') {
148
+ ${input.targetVal} = false;
149
+ }`);
150
+ if (input.schema.nullable) {
151
+ templateParts.push(`else if (${input.val} === null || ${input.val} === 'null') {
152
+ ${input.targetVal} = null;
153
+ }`);
154
+ }
155
+ templateParts.push(
156
+ `else {
157
+ $fallback("${input.instancePath}", "${input.schemaPath}/type", "${errorMessage}");
158
+ }`
159
+ );
160
+ return templateParts.join("\n");
161
+ }
120
162
  if (input.instancePath.length === 0) {
121
163
  templateParts.push(`if (typeof ${input.val} === 'string') {
122
164
  if (${input.val} === 'true') {
@@ -152,7 +194,7 @@ function booleanTemplate$2(input) {
152
194
  return templateParts.join("\n");
153
195
  }
154
196
  function stringTemplate$2(input) {
155
- const errorMessage = `Expected string at ${input.instancePath}`;
197
+ const errorMessage = `expected string`;
156
198
  const mainTemplate = `if (typeof ${input.val} === 'string') {
157
199
  ${input.targetVal} = ${input.val};
158
200
  } else {
@@ -175,19 +217,21 @@ function getVarName(input) {
175
217
  function floatTemplate$1(input) {
176
218
  const valName = getVarName(`${input.val}Val`);
177
219
  const templateParts = [];
178
- if (input.instancePath.length === 0) {
220
+ if (input.instancePath.length === 0 || input.shouldCoerce) {
179
221
  templateParts.push(`if (typeof ${input.val} === 'string') {
180
222
  const ${valName} = Number(${input.val});
181
- if (!Number.isNaN(parsedVal)) {
223
+ if (!Number.isNaN(${valName})) {
182
224
  ${input.targetVal} = ${valName};
183
225
  }`);
184
226
  if (input.schema.nullable) {
185
- templateParts.push(`if (${input.val} === 'null') {
227
+ templateParts.push(`else if (${input.val} === 'null') {
186
228
  ${input.targetVal} = null;
187
229
  }`);
188
230
  }
189
231
  templateParts.push(
190
- `$fallback("${input.instancePath}", "${input.schemaPath}/type", \`Could not parse number from \${${input.val}}\`);`
232
+ `else {
233
+ $fallback("${input.instancePath}", "${input.schemaPath}/type", \`Could not parse number from \${${input.val}}\`);
234
+ }`
191
235
  );
192
236
  templateParts.push("}");
193
237
  }
@@ -198,31 +242,36 @@ function floatTemplate$1(input) {
198
242
  $fallback("${input.instancePath}", "${input.schemaPath}/type", "${errorMessage}");
199
243
  }`;
200
244
  if (input.schema.nullable) {
201
- templateParts.push(`if (${input.val} === null) {
245
+ templateParts.push(`${input.instancePath.length === 0 || input.shouldCoerce ? "else " : ""}if (${input.val} === null) {
202
246
  ${input.targetVal} = null;
203
247
  } else {
204
248
  ${mainTemplate}
205
249
  }`);
206
250
  } else {
207
- templateParts.push(mainTemplate);
251
+ templateParts.push(
252
+ `${input.instancePath.length === 0 || input.shouldCoerce ? "else " : ""}${mainTemplate}`
253
+ );
208
254
  }
209
255
  return templateParts.join("\n");
210
256
  }
211
257
  function intTemplate$1(input, min, max) {
212
258
  const templateParts = [];
213
- if (input.instancePath.length === 0) {
259
+ const shouldCoerce = input.shouldCoerce || input.instancePath.length === 0;
260
+ if (shouldCoerce) {
214
261
  templateParts.push(`if (typeof ${input.val} === 'string') {
215
262
  const parsedVal = Number(${input.val});
216
263
  if (Number.isInteger(parsedVal) && parsedVal >= ${min} && parsedVal <= ${max}) {
217
- ${input.targetVal} = ${input.val};
264
+ ${input.targetVal} = parsedVal;
218
265
  }`);
219
266
  if (input.schema.nullable) {
220
- templateParts.push(`if (${input.val} === 'null') {
267
+ templateParts.push(`else if (${input.val} === 'null') {
221
268
  ${input.targetVal} = null;
222
269
  }`);
223
270
  }
224
271
  templateParts.push(
225
- `$fallback("${input.instancePath}", "${input.schemaPath}", "Expected valid integer between ${min} and ${max});`
272
+ `else {
273
+ $fallback("${input.instancePath}", "${input.schemaPath}", "Expected valid integer between ${min} and ${max}");
274
+ }`
226
275
  );
227
276
  templateParts.push("}");
228
277
  }
@@ -231,14 +280,15 @@ function intTemplate$1(input, min, max) {
231
280
  } else {
232
281
  $fallback("${input.instancePath}", "${input.schemaPath}", "Expected valid integer between ${min} and ${max}");
233
282
  }`;
283
+ const prefix = shouldCoerce ? `else ` : "";
234
284
  if (input.schema.nullable) {
235
- templateParts.push(`if (${input.val} === null) {
285
+ templateParts.push(`${prefix}if (${input.val} === null) {
236
286
  ${input.targetVal} = null;
237
287
  } else {
238
288
  ${mainTemplate}
239
289
  }`);
240
290
  } else {
241
- templateParts.push(mainTemplate);
291
+ templateParts.push(prefix + mainTemplate);
242
292
  }
243
293
  return templateParts.join("\n");
244
294
  }
@@ -247,7 +297,7 @@ function bigIntTemplate$2(input, isUnsigned) {
247
297
  templateParts.push(
248
298
  `if (typeof ${input.val} === 'string' || typeof ${input.val} === 'number') {`
249
299
  );
250
- if (input.instancePath.length === 0 && input.schema.nullable) {
300
+ if ((input.instancePath.length === 0 || input.shouldCoerce) && input.schema.nullable) {
251
301
  templateParts.push(`if (${input.val} === 'null') {
252
302
  ${input.targetVal} = null;
253
303
  } else {`);
@@ -264,9 +314,9 @@ function bigIntTemplate$2(input, isUnsigned) {
264
314
  templateParts.push(`${input.targetVal} = val;`);
265
315
  }
266
316
  templateParts.push(`} catch(err) {
267
- $fallback("${input.instancePath}", "${input.schemaPath}", "Unable to parse BigInt from ${input.val}.");
317
+ $fallback("${input.instancePath}", "${input.schemaPath}", \`Unable to parse BigInt from \${${input.val}}\`);
268
318
  }`);
269
- if (input.instancePath.length === 0 && input.schema.nullable) {
319
+ if ((input.instancePath.length === 0 || input.shouldCoerce) && input.schema.nullable) {
270
320
  templateParts.push("}");
271
321
  }
272
322
  templateParts.push("}");
@@ -289,12 +339,41 @@ function bigIntTemplate$2(input, isUnsigned) {
289
339
  }`);
290
340
  }
291
341
  templateParts.push(`else {
292
- $fallback("${input.instancePath}", "${input.schemaPath}", "Expected BigInt or Integer string. Got \${${input.val}}");
342
+ $fallback("${input.instancePath}", "${input.schemaPath}", \`Expected BigInt or Integer string. Got \${${input.val}}\`);
293
343
  }`);
294
344
  return templateParts.join("\n");
295
345
  }
296
346
  function timestampTemplate$2(input) {
297
347
  const templateParts = [];
348
+ if (input.shouldCoerce) {
349
+ return `if (typeof ${input.val} === 'string') {
350
+ try {
351
+ const parsedVal = new Date(${input.val});
352
+ if (!Number.isNaN(parsedVal.getMonth())) {
353
+ ${input.targetVal} = parsedVal;
354
+ } ${input.schema.nullable ? `else if (${input.val} === 'null') {
355
+ ${input.targetVal} = null;
356
+ }` : ""} else {
357
+ $fallback("${input.instancePath}", "${input.schemaPath}", "Unable to coerce date");
358
+ }
359
+ } catch (err) {
360
+ $fallback("${input.instancePath}", "${input.schemaPath}", "Unable to parse date");
361
+ }
362
+ } else if (typeof ${input.val} === 'number') {
363
+ const parsedVal = new Date(${input.val});
364
+ if (!Number.isNaN(parsedVal.getMonth())) {
365
+ ${input.targetVal} = parsedVal;
366
+ } else {
367
+ $fallback("${input.instancePath}", "${input.schemaPath}", "Unable to parse date");
368
+ }
369
+ } else if (typeof ${input.val} === 'object' && ${input.val} instanceof Date) {
370
+ ${input.targetVal} = ${input.val};
371
+ }${input.schema.nullable ? ` else if (${input.val} === null) {
372
+ ${input.targetVal} = null;
373
+ }` : ""} else {
374
+ $fallback("${input.instancePath}", "${input.schemaPath}", "Unable to parse date");
375
+ }`;
376
+ }
298
377
  if (input.instancePath.length === 0) {
299
378
  templateParts.push(`if (typeof ${input.val} === 'string') {
300
379
  const parsedVal = new Date(${input.val});
@@ -316,7 +395,7 @@ function timestampTemplate$2(input) {
316
395
  } else if (typeof ${input.val} === 'string') {
317
396
  ${input.targetVal} = new Date(${input.val});
318
397
  } else {
319
- $fallback("${input.instancePath}", "${input.schemaPath}", "Expected instanceof Date or ISO Date string at ${input.instancePath}")
398
+ $fallback("${input.instancePath}", "${input.schemaPath}", "Expected instanceof Date or ISO Date string")
320
399
  }`;
321
400
  if (input.schema.nullable) {
322
401
  templateParts.push(`if (${input.val} === null) {
@@ -333,7 +412,7 @@ function enumTemplate$2(input) {
333
412
  const enumTemplate2 = input.schema.enum.map((val) => `${input.val} === "${val}"`).join(" || ");
334
413
  const errorMessage = `Expected one of the following values: [${input.schema.enum.join(
335
414
  ", "
336
- )}]${input.instancePath.length ? ` at ${input.instancePath}` : ""}.`;
415
+ )}]`;
337
416
  const templateParts = [];
338
417
  templateParts.push(`if (typeof ${input.val} === 'string') {
339
418
  if (${enumTemplate2}) {
@@ -396,7 +475,8 @@ function objectTemplate$2(input) {
396
475
  schema: subSchema,
397
476
  instancePath: `${input.instancePath}/${key}`,
398
477
  schemaPath: `${input.schemaPath}/properties/${key}`,
399
- subFunctions: input.subFunctions
478
+ subFunctions: input.subFunctions,
479
+ shouldCoerce: input.shouldCoerce
400
480
  });
401
481
  parsingParts.push(innerTemplate);
402
482
  }
@@ -409,7 +489,8 @@ function objectTemplate$2(input) {
409
489
  schema: subSchema,
410
490
  instancePath: `${input.instancePath}/${key}`,
411
491
  schemaPath: `${input.schemaPath}/optionalProperties/${key}`,
412
- subFunctions: input.subFunctions
492
+ subFunctions: input.subFunctions,
493
+ shouldCoerce: input.shouldCoerce
413
494
  });
414
495
  parsingParts.push(`if (typeof ${input.val}.${key} === 'undefined') {
415
496
  // ignore undefined
@@ -452,10 +533,11 @@ function arrayTemplate$2(input) {
452
533
  const innerTemplate = schemaTemplate$1({
453
534
  val: itemVar,
454
535
  targetVal: itemResultVar,
455
- instancePath: `${input.instancePath}/[0]`,
536
+ instancePath: `${input.instancePath}/[i]`,
456
537
  schemaPath: `${input.schemaPath}/elements`,
457
538
  schema: input.schema.elements,
458
- subFunctions: input.subFunctions
539
+ subFunctions: input.subFunctions,
540
+ shouldCoerce: input.shouldCoerce
459
541
  });
460
542
  const mainTemplate = `if (Array.isArray(${input.val})) {
461
543
  const ${resultVar} = [];
@@ -490,7 +572,8 @@ function discriminatorTemplate$2(input) {
490
572
  instancePath: `${input.instancePath}`,
491
573
  discriminatorKey: input.schema.discriminator,
492
574
  discriminatorValue: type,
493
- subFunctions: input.subFunctions
575
+ subFunctions: input.subFunctions,
576
+ shouldCoerce: input.shouldCoerce
494
577
  });
495
578
  switchParts.push(`case "${type}": {
496
579
  ${template}
@@ -543,7 +626,8 @@ function recordTemplate$2(input) {
543
626
  instancePath: `${input.instancePath}/[key]`,
544
627
  schemaPath: `${input.schemaPath}/values`,
545
628
  schema: input.schema.values,
546
- subFunctions: input.subFunctions
629
+ subFunctions: input.subFunctions,
630
+ shouldCoerce: input.shouldCoerce
547
631
  });
548
632
  const mainTemplate = `if (typeof ${input.val} === 'object' && ${input.val} !== null) {
549
633
  const ${resultVal} = {};
@@ -593,8 +677,7 @@ function createSerializationV2Template(inputName, schema) {
593
677
  instancePath: "",
594
678
  outputPrefix: "",
595
679
  needsSanitization: [],
596
- subFunctions
597
- };
680
+ subFunctions};
598
681
  const result = template(context);
599
682
  if (typeDefs.isSchemaFormType(schema) || typeDefs.isSchemaFormEnum(schema)) {
600
683
  return result;
@@ -832,8 +915,7 @@ function objectTemplate$1(input) {
832
915
  schemaPath: `${input.schemaPath}/properties/${key}`,
833
916
  outputPrefix: includeComma ? `,"${key}":` : `"${key}":`,
834
917
  needsSanitization: input.needsSanitization,
835
- subFunctions: input.subFunctions
836
- });
918
+ subFunctions: input.subFunctions});
837
919
  templateParts.push(innerTemplate);
838
920
  }
839
921
  if (isAllOptionalKeys) {
@@ -856,8 +938,7 @@ function objectTemplate$1(input) {
856
938
  targetVal: input.targetVal,
857
939
  outputPrefix: `"${key}":`,
858
940
  needsSanitization: input.needsSanitization,
859
- subFunctions: input.subFunctions
860
- });
941
+ subFunctions: input.subFunctions});
861
942
  const innerTemplateWithComma = template({
862
943
  schema: optionalPropSchema,
863
944
  schemaPath: `${input.schemaPath}/optionalProperties/${key}`,
@@ -866,8 +947,7 @@ function objectTemplate$1(input) {
866
947
  targetVal: input.targetVal,
867
948
  outputPrefix: `,"${key}":`,
868
949
  needsSanitization: input.needsSanitization,
869
- subFunctions: input.subFunctions
870
- });
950
+ subFunctions: input.subFunctions});
871
951
  templateParts.push(`if (typeof ${innerVal} !== 'undefined') {
872
952
  if (${hasFieldsVar}) {
873
953
  ${innerTemplateWithComma}
@@ -893,8 +973,7 @@ function objectTemplate$1(input) {
893
973
  targetVal: "json",
894
974
  outputPrefix: `,"${key}":`,
895
975
  needsSanitization: input.needsSanitization,
896
- subFunctions: input.subFunctions
897
- });
976
+ subFunctions: input.subFunctions});
898
977
  const completeInnerTemplate = `if (typeof ${innerVal} !== 'undefined') {
899
978
  ${innerTemplate}
900
979
  }`;
@@ -943,8 +1022,7 @@ function arrayTemplate$1(input) {
943
1022
  targetVal: input.targetVal,
944
1023
  outputPrefix: "",
945
1024
  needsSanitization: input.needsSanitization,
946
- subFunctions: input.subFunctions
947
- });
1025
+ subFunctions: input.subFunctions});
948
1026
  templateParts.push(`for (let i = 0; i < ${input.val}.length; i++) {
949
1027
  const ${itemVarName} = ${input.val}[i];
950
1028
  if (i !== 0) {
@@ -977,8 +1055,7 @@ function recordTemplate$1(input) {
977
1055
  targetVal: input.targetVal,
978
1056
  outputPrefix: "",
979
1057
  needsSanitization: input.needsSanitization,
980
- subFunctions: input.subFunctions
981
- });
1058
+ subFunctions: input.subFunctions});
982
1059
  templateParts.push(`for (let i = 0; i < ${keysVarName}.length; i++) {
983
1060
  const key = ${keysVarName}[i];
984
1061
  const innerVal = ${input.val}[key];
@@ -1046,8 +1123,7 @@ function discriminatorTemplate$1(input) {
1046
1123
  discriminatorValue: val,
1047
1124
  outputPrefix: input.outputPrefix,
1048
1125
  needsSanitization: input.needsSanitization,
1049
- subFunctions: input.subFunctions
1050
- });
1126
+ subFunctions: input.subFunctions});
1051
1127
  templateParts.push(`case '${val}': {
1052
1128
  ${innerTemplate}
1053
1129
  break;
@@ -1115,12 +1191,10 @@ function createValidationTemplate(inputName, schema) {
1115
1191
  const subFunctions = {};
1116
1192
  const template = schemaTemplate({
1117
1193
  val: inputName,
1118
- targetVal: "",
1119
1194
  schema,
1120
1195
  schemaPath: ``,
1121
1196
  instancePath: "",
1122
- subFunctions
1123
- });
1197
+ subFunctions});
1124
1198
  const subFunctionBodies = Object.keys(subFunctions).map(
1125
1199
  (key) => subFunctions[key]
1126
1200
  );
@@ -1128,7 +1202,7 @@ function createValidationTemplate(inputName, schema) {
1128
1202
  return ${template}`;
1129
1203
  }
1130
1204
  function schemaTemplate(input) {
1131
- if (timestamp.isAScalarSchema(input.schema)) {
1205
+ if (adapters.isAScalarSchema(input.schema)) {
1132
1206
  switch (input.schema.type) {
1133
1207
  case "boolean":
1134
1208
  return booleanTemplate(input);
@@ -1136,17 +1210,17 @@ function schemaTemplate(input) {
1136
1210
  case "float64":
1137
1211
  return floatTemplate(input);
1138
1212
  case "int8":
1139
- return intTemplate(input, timestamp.int8Min, timestamp.int8Max);
1213
+ return intTemplate(input, int8Min, int8Max);
1140
1214
  case "int16":
1141
- return intTemplate(input, timestamp.int16Min, timestamp.int16Max);
1215
+ return intTemplate(input, int16Min, int16Max);
1142
1216
  case "int32":
1143
- return intTemplate(input, timestamp.int32Min, timestamp.int32Max);
1217
+ return intTemplate(input, int32Min, int32Max);
1144
1218
  case "uint8":
1145
- return intTemplate(input, timestamp.uint8Min, timestamp.uint8Max);
1219
+ return intTemplate(input, uint8Min, uint8Max);
1146
1220
  case "uint16":
1147
- return intTemplate(input, timestamp.uint16Min, timestamp.uint16Max);
1221
+ return intTemplate(input, uint16Min, uint16Max);
1148
1222
  case "uint32":
1149
- return intTemplate(input, timestamp.uint32Min, timestamp.uint32Max);
1223
+ return intTemplate(input, uint32Min, uint32Max);
1150
1224
  case "int64":
1151
1225
  return bigIntTemplate(input, false);
1152
1226
  case "uint64":
@@ -1157,22 +1231,22 @@ function schemaTemplate(input) {
1157
1231
  return timestampTemplate(input);
1158
1232
  }
1159
1233
  }
1160
- if (timestamp.isAObjectSchema(input.schema)) {
1234
+ if (adapters.isAObjectSchema(input.schema)) {
1161
1235
  return objectTemplate(input);
1162
1236
  }
1163
- if (timestamp.isAStringEnumSchema(input.schema)) {
1237
+ if (adapters.isAStringEnumSchema(input.schema)) {
1164
1238
  return enumTemplate(input);
1165
1239
  }
1166
- if (timestamp.isAAraySchema(input.schema)) {
1240
+ if (adapters.isAAraySchema(input.schema)) {
1167
1241
  return arrayTemplate(input);
1168
1242
  }
1169
- if (timestamp.isARecordSchema(input.schema)) {
1243
+ if (adapters.isARecordSchema(input.schema)) {
1170
1244
  return recordTemplate(input);
1171
1245
  }
1172
- if (timestamp.isADiscriminatorSchema(input.schema)) {
1246
+ if (adapters.isADiscriminatorSchema(input.schema)) {
1173
1247
  return discriminatorTemplate(input);
1174
1248
  }
1175
- if (timestamp.isARefSchema(input.schema)) {
1249
+ if (adapters.isARefSchema(input.schema)) {
1176
1250
  return refTemplate(input);
1177
1251
  }
1178
1252
  return "true";
@@ -1240,9 +1314,7 @@ function objectTemplate(input) {
1240
1314
  schemaPath: `${input.schemaPath}/properties/${key}`,
1241
1315
  instancePath: `${input.instancePath}/${key}`,
1242
1316
  val: `${input.val}.${key}`,
1243
- targetVal: "",
1244
- subFunctions: input.subFunctions
1245
- })
1317
+ subFunctions: input.subFunctions})
1246
1318
  );
1247
1319
  }
1248
1320
  if (input.schema.optionalProperties) {
@@ -1253,16 +1325,14 @@ function objectTemplate(input) {
1253
1325
  schemaPath: `${input.schemaPath}/optionalProperties/${key}`,
1254
1326
  instancePath: `${input.instancePath}/${key}`,
1255
1327
  val: `${input.val}.${key}`,
1256
- targetVal: "",
1257
- subFunctions: input.subFunctions
1258
- });
1328
+ subFunctions: input.subFunctions});
1259
1329
  parts.push(
1260
1330
  `(typeof ${input.val}.${key} === 'undefined' || (${template}))`
1261
1331
  );
1262
1332
  }
1263
1333
  }
1264
1334
  let mainTemplate = parts.join(" && ");
1265
- const fnName = refFunctionName(input.schema.metadata.id ?? "");
1335
+ const fnName = refFunctionName(input.schema.metadata?.id ?? "");
1266
1336
  if (Object.keys(input.subFunctions).includes(fnName)) {
1267
1337
  if (!input.subFunctions[fnName]) {
1268
1338
  input.subFunctions[fnName] = `function ${fnName}(input) {
@@ -1289,9 +1359,7 @@ function arrayTemplate(input) {
1289
1359
  instancePath: `${input.instancePath}/item`,
1290
1360
  schemaPath: `${input.schemaPath}/elements`,
1291
1361
  schema: input.schema.elements,
1292
- targetVal: "",
1293
- subFunctions: input.subFunctions
1294
- });
1362
+ subFunctions: input.subFunctions});
1295
1363
  if (input.schema.nullable) {
1296
1364
  return `((Array.isArray(${input.val}) && ${input.val}.every((item) => ${innerTemplate})) || ${input.val} === null)`;
1297
1365
  }
@@ -1303,9 +1371,7 @@ function recordTemplate(input) {
1303
1371
  instancePath: `${input.instancePath}`,
1304
1372
  schemaPath: `${input.schemaPath}/values`,
1305
1373
  val: `${input.val}[key]`,
1306
- targetVal: "",
1307
- subFunctions: input.subFunctions
1308
- });
1374
+ subFunctions: input.subFunctions});
1309
1375
  const mainTemplate = `typeof ${input.val} === 'object' && ${input.val} !== null && Object.keys(${input.val}).every((key) => ${subTemplate})`;
1310
1376
  if (input.schema.nullable) {
1311
1377
  return `((${mainTemplate}) || ${input.val} === null)`;
@@ -1322,18 +1388,16 @@ function discriminatorTemplate(input) {
1322
1388
  parts.push(
1323
1389
  objectTemplate({
1324
1390
  val: input.val,
1325
- targetVal: "",
1326
1391
  schema: subSchema,
1327
1392
  schemaPath: `${input.schemaPath}/mapping/${discriminatorVal}`,
1328
1393
  instancePath: input.instancePath,
1329
1394
  discriminatorKey: input.schema.discriminator,
1330
1395
  discriminatorValue: discriminatorVal,
1331
- subFunctions: input.subFunctions
1332
- })
1396
+ subFunctions: input.subFunctions})
1333
1397
  );
1334
1398
  }
1335
1399
  let mainTemplate = `typeof ${input.val} === 'object' && ${input.val} !== null && (${parts.join(" || ")})`;
1336
- const fnName = refFunctionName(input.schema.metadata.id ?? "");
1400
+ const fnName = refFunctionName(input.schema.metadata?.id ?? "");
1337
1401
  if (Object.keys(input.subFunctions).includes(fnName)) {
1338
1402
  if (!input.subFunctions[fnName]) {
1339
1403
  input.subFunctions[fnName] = `function ${fnName}(input) {
@@ -1361,108 +1425,211 @@ function refTemplate(input) {
1361
1425
  return `${fnName}(${input.val})`;
1362
1426
  }
1363
1427
 
1428
+ function isValueError(input) {
1429
+ if (typeof input !== "object" || !input) {
1430
+ return false;
1431
+ }
1432
+ return "message" in input && typeof input.message === "string" && (typeof input.instancePath === "undefined" || typeof input.instancePath === "string") && (typeof input.schemaPath === "undefined" || typeof input.schemaPath === "string");
1433
+ }
1434
+ function errorMessageFromErrors(errors) {
1435
+ if (errors.length === 0) {
1436
+ return `Input failed validation`;
1437
+ }
1438
+ if (errors.length === 1) {
1439
+ return errors[0].message;
1440
+ }
1441
+ let propStr = "[";
1442
+ for (let i = 0; i < errors.length; i++) {
1443
+ const err = errors[i];
1444
+ if (i > 0) {
1445
+ propStr += ", ";
1446
+ }
1447
+ propStr += err.instancePath;
1448
+ }
1449
+ propStr += "]";
1450
+ return `Invalid input. Affected properties ${propStr}.`;
1451
+ }
1452
+ class ValidationException extends Error {
1453
+ errors;
1454
+ constructor(options) {
1455
+ super(options.message ?? errorMessageFromErrors(options.errors));
1456
+ this.errors = options.errors;
1457
+ }
1458
+ }
1459
+ function isValidationException(input) {
1460
+ return input instanceof ValidationException;
1461
+ }
1462
+
1364
1463
  function compile(schema) {
1365
1464
  const validateCode = createValidationTemplate("input", schema);
1366
- const parser = getCompiledParser("input", schema);
1367
- const parseFn = parser.fn;
1465
+ const parser = getCompiledParser("input", schema, false);
1466
+ const parserFn = parser.fn;
1467
+ const coercer = getCompiledParser("input", schema, true);
1468
+ const coercerFn = coercer.fn;
1368
1469
  const serializer = getCompiledSerializer(schema);
1369
1470
  const serializeFn = serializer.fn;
1471
+ const serialize = (input) => {
1472
+ try {
1473
+ const result2 = serializeFn(input);
1474
+ return {
1475
+ success: true,
1476
+ value: result2
1477
+ };
1478
+ } catch (err) {
1479
+ return {
1480
+ success: false,
1481
+ errors: [
1482
+ {
1483
+ instancePath: "/",
1484
+ message: err instanceof Error ? err.message : `${err}`
1485
+ }
1486
+ ]
1487
+ };
1488
+ }
1489
+ };
1370
1490
  const validate = new Function("input", validateCode);
1371
1491
  const parse = (input) => {
1492
+ const context = adapters.newValidationContext();
1372
1493
  try {
1373
- return parseFn(input);
1494
+ const result2 = parserFn(input, context);
1495
+ if (context.errors.length) {
1496
+ return {
1497
+ success: false,
1498
+ errors: context.errors
1499
+ };
1500
+ }
1501
+ return {
1502
+ success: true,
1503
+ value: result2
1504
+ };
1374
1505
  } catch (err) {
1375
- const errors = timestamp.errors(schema, input);
1376
- let errorMessage = err instanceof Error ? err.message : "";
1377
- if (errors.length) {
1378
- errorMessage = errors[0].message ?? `Parsing error at ${errors[0].instancePath}`;
1506
+ if (context.errors.length) {
1507
+ return {
1508
+ success: false,
1509
+ errors: context.errors
1510
+ };
1379
1511
  }
1380
- throw new timestamp.ValidationError({
1381
- message: errorMessage,
1382
- errors
1383
- });
1512
+ return {
1513
+ success: false,
1514
+ errors: [
1515
+ {
1516
+ message: err instanceof Error ? err.message : `${err}`,
1517
+ instancePath: "/"
1518
+ }
1519
+ ]
1520
+ };
1384
1521
  }
1385
1522
  };
1386
- const result = {
1387
- validate,
1388
- parse,
1389
- safeParse(input) {
1390
- try {
1391
- const result2 = parseFn(input);
1523
+ const coerce = (input) => {
1524
+ const context = adapters.newValidationContext();
1525
+ try {
1526
+ const result2 = coercerFn(input, context);
1527
+ if (context.errors.length) {
1392
1528
  return {
1393
- success: true,
1394
- value: result2
1529
+ success: false,
1530
+ errors: context.errors
1395
1531
  };
1396
- } catch (err) {
1397
- const errors = timestamp.errors(schema, input);
1398
- let errorMessage = err instanceof Error ? err.message : "";
1399
- if (errors.length) {
1400
- errorMessage = errors[0].message ?? `Parsing error at ${errors[0].instancePath}`;
1401
- }
1532
+ }
1533
+ return {
1534
+ success: true,
1535
+ value: result2
1536
+ };
1537
+ } catch (err) {
1538
+ if (context.errors.length) {
1402
1539
  return {
1403
1540
  success: false,
1404
- error: new timestamp.ValidationError({
1405
- message: errorMessage,
1406
- errors
1407
- })
1541
+ errors: context.errors
1408
1542
  };
1409
1543
  }
1544
+ return {
1545
+ success: false,
1546
+ errors: [
1547
+ {
1548
+ message: err instanceof Error ? err.message : `${err}`,
1549
+ instancePath: "/"
1550
+ }
1551
+ ]
1552
+ };
1553
+ }
1554
+ };
1555
+ const result = {
1556
+ schema: JSON.parse(JSON.stringify(schema)),
1557
+ validate,
1558
+ parse,
1559
+ parseUnsafe(input) {
1560
+ const context = adapters.newValidationContext();
1561
+ const result2 = parserFn(input, context);
1562
+ if (context.errors.length) {
1563
+ throw new ValidationException({ errors: context.errors });
1564
+ }
1565
+ return result2;
1410
1566
  },
1411
- serialize(input) {
1412
- try {
1413
- return serializeFn(input);
1414
- } catch (err) {
1415
- const errors = timestamp.errors(schema, input);
1416
- let errorMessage = err instanceof Error ? err.message : "";
1417
- if (errors.length) {
1418
- errorMessage = errors[0].message ?? `Serialization error at ${errors[0].instancePath}`;
1419
- }
1420
- throw new timestamp.ValidationError({ message: errorMessage, errors });
1567
+ coerce,
1568
+ coerceUnsafe(input) {
1569
+ const context = adapters.newValidationContext();
1570
+ const result2 = coercerFn(input, context);
1571
+ if (context.errors.length) {
1572
+ throw new ValidationException({ errors: context.errors });
1421
1573
  }
1574
+ return result2;
1575
+ },
1576
+ serialize,
1577
+ serializeUnsafe(input) {
1578
+ return serializeFn(input);
1422
1579
  },
1423
1580
  compiledCode: {
1424
1581
  validate: validateCode,
1425
1582
  parse: parser.code,
1583
+ coerce: coercer.code,
1426
1584
  serialize: serializer.code
1427
1585
  },
1428
- "~standard": timestamp.createStandardSchemaProperty(validate, parse)
1586
+ "~standard": adapters.createStandardSchemaProperty(validate, (input, ctx) => {
1587
+ const result2 = parse(input);
1588
+ if (!result2.success) {
1589
+ ctx.errors = result2.errors;
1590
+ return undefined;
1591
+ }
1592
+ return result2.value;
1593
+ })
1429
1594
  };
1430
1595
  return result;
1431
1596
  }
1432
- function getCompiledParser(input, schema) {
1433
- const code = createParsingTemplate(input, schema);
1597
+ function getCompiledParser(input, schema, shouldCoerce) {
1598
+ const code = createParsingTemplate(input, schema, shouldCoerce);
1434
1599
  if (typeDefs.isSchemaFormType(schema)) {
1435
1600
  switch (schema.type) {
1436
1601
  case "float32":
1437
1602
  case "float64":
1438
1603
  if (schema.nullable) {
1439
1604
  return {
1440
- fn: nullableFloatParser,
1605
+ fn: nullableFloatDecoder,
1441
1606
  code
1442
1607
  };
1443
1608
  }
1444
1609
  return {
1445
- fn: compiledFloatParser,
1610
+ fn: compiledFloatDecoder,
1446
1611
  code
1447
1612
  };
1448
1613
  case "int64":
1449
1614
  return {
1450
- fn(input2) {
1451
- return bigIntParser(
1615
+ fn(input2, context) {
1616
+ return bigIntDecoder(
1452
1617
  input2,
1453
1618
  false,
1454
- schema.nullable ?? false
1619
+ schema.nullable ?? false,
1620
+ context
1455
1621
  );
1456
1622
  },
1457
1623
  code
1458
1624
  };
1459
1625
  case "uint64":
1460
1626
  return {
1461
- fn(input2) {
1462
- return bigIntParser(
1627
+ fn(input2, context) {
1628
+ return bigIntDecoder(
1463
1629
  input2,
1464
1630
  true,
1465
- schema.nullable ?? false
1631
+ schema.nullable ?? false,
1632
+ context
1466
1633
  );
1467
1634
  },
1468
1635
  code
@@ -1470,106 +1637,191 @@ function getCompiledParser(input, schema) {
1470
1637
  case "int32":
1471
1638
  if (schema.nullable) {
1472
1639
  return {
1473
- fn(input2) {
1474
- return nullableIntParser(input2, timestamp.int32Min, timestamp.int32Max);
1640
+ fn(input2, context) {
1641
+ return nullableIntDecoder(
1642
+ input2,
1643
+ int32Min,
1644
+ int32Max,
1645
+ context
1646
+ );
1475
1647
  },
1476
1648
  code
1477
1649
  };
1478
1650
  }
1479
1651
  return {
1480
- fn(input2) {
1481
- return intParser(input2, timestamp.int32Min, timestamp.int32Max);
1652
+ fn(input2, context) {
1653
+ return intDecoder(input2, int32Min, int32Max, context);
1482
1654
  },
1483
1655
  code
1484
1656
  };
1485
1657
  case "int16":
1486
1658
  if (schema.nullable) {
1487
1659
  return {
1488
- fn(input2) {
1489
- return nullableIntParser(input2, timestamp.int16Min, timestamp.int16Max);
1660
+ fn(input2, context) {
1661
+ return nullableIntDecoder(
1662
+ input2,
1663
+ int16Min,
1664
+ int16Max,
1665
+ context
1666
+ );
1490
1667
  },
1491
1668
  code
1492
1669
  };
1493
1670
  }
1494
1671
  return {
1495
- fn(input2) {
1496
- return intParser(input2, timestamp.int16Min, timestamp.int16Max);
1672
+ fn(input2, context) {
1673
+ return intDecoder(input2, int16Min, int16Max, context);
1497
1674
  },
1498
1675
  code
1499
1676
  };
1500
1677
  case "int8":
1501
1678
  if (schema.nullable) {
1502
1679
  return {
1503
- fn(input2) {
1504
- return nullableIntParser(input2, timestamp.int8Min, timestamp.int8Max);
1680
+ fn(input2, context) {
1681
+ return nullableIntDecoder(
1682
+ input2,
1683
+ int8Min,
1684
+ int8Max,
1685
+ context
1686
+ );
1505
1687
  },
1506
1688
  code
1507
1689
  };
1508
1690
  }
1509
1691
  return {
1510
- fn(input2) {
1511
- return intParser(input2, timestamp.int8Min, timestamp.int8Max);
1692
+ fn(input2, context) {
1693
+ return intDecoder(input2, int8Min, int8Max, context);
1512
1694
  },
1513
1695
  code
1514
1696
  };
1515
1697
  case "uint32":
1516
1698
  if (schema.nullable) {
1517
1699
  return {
1518
- fn(input2) {
1519
- return nullableIntParser(
1700
+ fn(input2, context) {
1701
+ return nullableIntDecoder(
1520
1702
  input2,
1521
- timestamp.uint32Min,
1522
- timestamp.uint32Max
1703
+ uint32Min,
1704
+ uint32Max,
1705
+ context
1523
1706
  );
1524
1707
  },
1525
1708
  code
1526
1709
  };
1527
1710
  }
1528
1711
  return {
1529
- fn(input2) {
1530
- return intParser(input2, timestamp.uint32Min, timestamp.uint32Max);
1712
+ fn(input2, context) {
1713
+ return intDecoder(input2, uint32Min, uint32Max, context);
1531
1714
  },
1532
1715
  code
1533
1716
  };
1534
1717
  case "uint16":
1535
1718
  if (schema.nullable) {
1536
1719
  return {
1537
- fn(input2) {
1538
- return nullableIntParser(
1720
+ fn(input2, context) {
1721
+ return nullableIntDecoder(
1539
1722
  input2,
1540
- timestamp.uint16Min,
1541
- timestamp.uint16Max
1723
+ uint16Min,
1724
+ uint16Max,
1725
+ context
1542
1726
  );
1543
1727
  },
1544
1728
  code
1545
1729
  };
1546
1730
  }
1547
1731
  return {
1548
- fn(input2) {
1549
- return intParser(input2, timestamp.uint16Min, timestamp.uint16Max);
1732
+ fn(input2, context) {
1733
+ return intDecoder(input2, uint16Min, uint16Max, context);
1550
1734
  },
1551
1735
  code
1552
1736
  };
1553
1737
  case "uint8":
1554
1738
  if (schema.nullable) {
1555
1739
  return {
1556
- fn(input2) {
1557
- return nullableIntParser(input2, timestamp.uint8Min, timestamp.uint8Max);
1740
+ fn(input2, context) {
1741
+ return nullableIntDecoder(
1742
+ input2,
1743
+ uint8Min,
1744
+ uint8Max,
1745
+ context
1746
+ );
1558
1747
  },
1559
1748
  code
1560
1749
  };
1561
1750
  }
1562
1751
  return {
1563
- fn(input2) {
1564
- return intParser(input2, timestamp.uint8Min, timestamp.uint8Max);
1752
+ fn(input2, context) {
1753
+ return intDecoder(input2, uint8Min, uint8Max, context);
1565
1754
  },
1566
1755
  code
1567
1756
  };
1568
1757
  case "boolean":
1758
+ if (shouldCoerce) {
1759
+ if (schema.nullable) {
1760
+ return {
1761
+ code,
1762
+ fn(input2, context) {
1763
+ switch (input2) {
1764
+ case true:
1765
+ return true;
1766
+ case false:
1767
+ return false;
1768
+ case "true":
1769
+ case "TRUE":
1770
+ case "1":
1771
+ case 1:
1772
+ return true;
1773
+ case "false":
1774
+ case "FALSE":
1775
+ case "0":
1776
+ case 0:
1777
+ return false;
1778
+ case null:
1779
+ case "null":
1780
+ return null;
1781
+ default:
1782
+ context.errors.push({
1783
+ instancePath: "/",
1784
+ schemaPath: "/type",
1785
+ message: `Unable to coerce ${input2} into boolean`
1786
+ });
1787
+ return undefined;
1788
+ }
1789
+ }
1790
+ };
1791
+ }
1792
+ return {
1793
+ code,
1794
+ fn(input2, context) {
1795
+ switch (input2) {
1796
+ case true:
1797
+ return true;
1798
+ case false:
1799
+ return false;
1800
+ case "true":
1801
+ case "TRUE":
1802
+ case "1":
1803
+ case 1:
1804
+ return true;
1805
+ case "false":
1806
+ case "FALSE":
1807
+ case "0":
1808
+ case 0:
1809
+ return false;
1810
+ default:
1811
+ context.errors.push({
1812
+ instancePath: "/",
1813
+ schemaPath: "/type",
1814
+ message: `Unable to coerce ${input2} into boolean`
1815
+ });
1816
+ return undefined;
1817
+ }
1818
+ }
1819
+ };
1820
+ }
1569
1821
  if (schema.nullable) {
1570
1822
  return {
1571
1823
  code,
1572
- fn(input2) {
1824
+ fn(input2, context) {
1573
1825
  if (typeof input2 === "string") {
1574
1826
  if (input2 === "true") {
1575
1827
  return true;
@@ -1587,20 +1839,17 @@ function getCompiledParser(input, schema) {
1587
1839
  if (input2 === null) {
1588
1840
  return null;
1589
1841
  }
1590
- throw new timestamp.ValidationError({
1591
- message: `Expected boolean. Got ${typeof input2}`,
1592
- errors: [
1593
- {
1594
- instancePath: "",
1595
- schemaPath: "/type"
1596
- }
1597
- ]
1842
+ context.errors.push({
1843
+ instancePath: "",
1844
+ schemaPath: "/type",
1845
+ message: `Expected boolean. Got ${typeof input2}`
1598
1846
  });
1847
+ return undefined;
1599
1848
  }
1600
1849
  };
1601
1850
  }
1602
1851
  return {
1603
- fn(input2) {
1852
+ fn(input2, context) {
1604
1853
  if (typeof input2 === "string") {
1605
1854
  if (input2 === "true") {
1606
1855
  return true;
@@ -1612,22 +1861,19 @@ function getCompiledParser(input, schema) {
1612
1861
  if (typeof input2 === "boolean") {
1613
1862
  return input2;
1614
1863
  }
1615
- throw new timestamp.ValidationError({
1616
- message: `Expected boolean. Got ${typeof input2}`,
1617
- errors: [
1618
- {
1619
- instancePath: "",
1620
- schemaPath: "/type"
1621
- }
1622
- ]
1864
+ context.errors.push({
1865
+ instancePath: "",
1866
+ schemaPath: "/type",
1867
+ message: `Expected boolean. Got ${typeof input2}`
1623
1868
  });
1869
+ return undefined;
1624
1870
  },
1625
1871
  code
1626
1872
  };
1627
1873
  case "string":
1628
1874
  if (schema.nullable) {
1629
1875
  return {
1630
- fn(input2) {
1876
+ fn(input2, context) {
1631
1877
  if (typeof input2 === "string") {
1632
1878
  if (input2 === "null") {
1633
1879
  return null;
@@ -1637,44 +1883,100 @@ function getCompiledParser(input, schema) {
1637
1883
  if (input2 === null) {
1638
1884
  return null;
1639
1885
  }
1640
- throw new timestamp.ValidationError({
1641
- message: `Expected string or null. Got ${typeof input2}.`,
1642
- errors: [
1643
- {
1644
- instancePath: "",
1645
- schemaPath: "/type"
1646
- }
1647
- ]
1886
+ context.errors.push({
1887
+ instancePath: "",
1888
+ schemaPath: "/type",
1889
+ message: `Expected string or null. Got ${typeof input2}.`
1648
1890
  });
1891
+ return undefined;
1649
1892
  },
1650
1893
  code
1651
1894
  };
1652
1895
  }
1653
1896
  return {
1654
- fn(input2) {
1897
+ fn(input2, context) {
1655
1898
  if (typeof input2 === "string") {
1656
1899
  return input2;
1657
1900
  }
1658
- throw new timestamp.ValidationError({
1901
+ context.errors.push({
1659
1902
  message: `Expected string. Got ${typeof input2}.`,
1660
- errors: [
1661
- {
1662
- instancePath: "",
1663
- schemaPath: "/type"
1664
- }
1665
- ]
1903
+ instancePath: "/",
1904
+ schemaPath: "/type"
1666
1905
  });
1906
+ return undefined;
1667
1907
  },
1668
1908
  code
1669
1909
  };
1670
1910
  case "timestamp":
1911
+ if (shouldCoerce) {
1912
+ if (schema.nullable) {
1913
+ return {
1914
+ fn(input2, context) {
1915
+ if (typeof input2 === "string") {
1916
+ const decodedInput = new Date(input2);
1917
+ if (!Number.isNaN(decodedInput.getMonth())) {
1918
+ return decodedInput;
1919
+ }
1920
+ if (input2 === "null") {
1921
+ return null;
1922
+ }
1923
+ }
1924
+ if (typeof input2 === "number") {
1925
+ const decodedInput = new Date(input2);
1926
+ if (!Number.isNaN(decodedInput.getMonth())) {
1927
+ return decodedInput;
1928
+ }
1929
+ }
1930
+ if (typeof input2 === "object" && input2 instanceof Date) {
1931
+ return input2;
1932
+ }
1933
+ if (input2 === null) {
1934
+ return null;
1935
+ }
1936
+ context.errors.push({
1937
+ instancePath: "",
1938
+ schemaPath: "/type",
1939
+ message: `Expected instanceof Date, ISO Date string, or null. Got ${typeof input2}.`
1940
+ });
1941
+ return undefined;
1942
+ },
1943
+ code
1944
+ };
1945
+ }
1946
+ return {
1947
+ fn(input2, context) {
1948
+ if (typeof input2 === "string") {
1949
+ const decodedInput = new Date(input2);
1950
+ if (!Number.isNaN(decodedInput.getMonth())) {
1951
+ return decodedInput;
1952
+ }
1953
+ }
1954
+ if (typeof input2 === "number") {
1955
+ const decodedInput = new Date(input2);
1956
+ if (!Number.isNaN(decodedInput.getMonth())) {
1957
+ return decodedInput;
1958
+ }
1959
+ }
1960
+ if (typeof input2 === "object" && input2 instanceof Date) {
1961
+ return input2;
1962
+ }
1963
+ context.errors.push({
1964
+ instancePath: "",
1965
+ schemaPath: "/type",
1966
+ message: `Expected instanceof Date or ISO Date string. Got ${typeof input2}.`
1967
+ });
1968
+ return undefined;
1969
+ },
1970
+ code
1971
+ };
1972
+ }
1671
1973
  if (schema.nullable) {
1672
1974
  return {
1673
- fn(input2) {
1975
+ fn(input2, context) {
1674
1976
  if (typeof input2 === "string") {
1675
- const parsedInput = new Date(input2);
1676
- if (!Number.isNaN(parsedInput.getMonth())) {
1677
- return parsedInput;
1977
+ const decodedInput = new Date(input2);
1978
+ if (!Number.isNaN(decodedInput.getMonth())) {
1979
+ return decodedInput;
1678
1980
  }
1679
1981
  if (input2 === "null") {
1680
1982
  return null;
@@ -1686,39 +1988,33 @@ function getCompiledParser(input, schema) {
1686
1988
  if (input2 === null) {
1687
1989
  return null;
1688
1990
  }
1689
- throw new timestamp.ValidationError({
1690
- message: `Expected instanceof Date, ISO Date string, or null. Got ${typeof input2}.`,
1691
- errors: [
1692
- {
1693
- instancePath: "",
1694
- schemaPath: "/type"
1695
- }
1696
- ]
1991
+ context.errors.push({
1992
+ instancePath: "",
1993
+ schemaPath: "/type",
1994
+ message: `Expected instanceof Date, ISO Date string, or null. Got ${typeof input2}.`
1697
1995
  });
1996
+ return undefined;
1698
1997
  },
1699
1998
  code
1700
1999
  };
1701
2000
  }
1702
2001
  return {
1703
- fn(input2) {
2002
+ fn(input2, context) {
1704
2003
  if (typeof input2 === "string") {
1705
- const parsedInput = new Date(input2);
1706
- if (!Number.isNaN(parsedInput.getMonth())) {
1707
- return parsedInput;
2004
+ const decodedInput = new Date(input2);
2005
+ if (!Number.isNaN(decodedInput.getMonth())) {
2006
+ return decodedInput;
1708
2007
  }
1709
2008
  }
1710
2009
  if (typeof input2 === "object" && input2 instanceof Date) {
1711
2010
  return input2;
1712
2011
  }
1713
- throw new timestamp.ValidationError({
1714
- message: `Expected instance of Date or ISO date string. Got ${typeof input2}.`,
1715
- errors: [
1716
- {
1717
- instancePath: "",
1718
- schemaPath: "/type"
1719
- }
1720
- ]
2012
+ context.errors.push({
2013
+ instancePath: "",
2014
+ schemaPath: "/type",
2015
+ message: `Expected instance of Date or ISO date string. Got ${typeof input2}.`
1721
2016
  });
2017
+ return undefined;
1722
2018
  },
1723
2019
  code
1724
2020
  };
@@ -1727,7 +2023,7 @@ function getCompiledParser(input, schema) {
1727
2023
  if (typeDefs.isSchemaFormEnum(schema)) {
1728
2024
  if (schema.nullable) {
1729
2025
  return {
1730
- fn(input2) {
2026
+ fn(input2, context) {
1731
2027
  if (typeof input2 === "string") {
1732
2028
  for (const val of schema.enum) {
1733
2029
  if (input2 === val) {
@@ -1741,23 +2037,20 @@ function getCompiledParser(input, schema) {
1741
2037
  if (input2 === null) {
1742
2038
  return null;
1743
2039
  }
1744
- throw new timestamp.ValidationError({
2040
+ context.errors.push({
2041
+ instancePath: "",
2042
+ schemaPath: "/enum",
1745
2043
  message: `Expected one of the following values: [${schema.enum.join(
1746
2044
  ", "
1747
- )}] or null. Got ${typeof input2}.`,
1748
- errors: [
1749
- {
1750
- instancePath: "",
1751
- schemaPath: "/enum"
1752
- }
1753
- ]
2045
+ )}] or null. Got ${typeof input2}.`
1754
2046
  });
2047
+ return undefined;
1755
2048
  },
1756
2049
  code
1757
2050
  };
1758
2051
  }
1759
2052
  return {
1760
- fn(input2) {
2053
+ fn(input2, context) {
1761
2054
  if (typeof input2 === "string") {
1762
2055
  for (const val of schema.enum) {
1763
2056
  if (input2 === val) {
@@ -1765,70 +2058,58 @@ function getCompiledParser(input, schema) {
1765
2058
  }
1766
2059
  }
1767
2060
  }
1768
- throw new timestamp.ValidationError({
2061
+ context.errors.push({
2062
+ instancePath: "",
2063
+ schemaPath: "/enum",
1769
2064
  message: `Expected one of the following values: [${schema.enum.join(
1770
2065
  ", "
1771
- )}]. Got ${typeof input2}.`,
1772
- errors: [
1773
- {
1774
- instancePath: "",
1775
- schemaPath: "/enum"
1776
- }
1777
- ]
2066
+ )}]. Got ${typeof input2}.`
1778
2067
  });
2068
+ return undefined;
1779
2069
  },
1780
2070
  code
1781
2071
  };
1782
2072
  }
1783
- return { fn: new Function(input, code), code };
2073
+ return { fn: new Function(input, "context", code), code };
1784
2074
  }
1785
- function compiledFloatParser(input) {
2075
+ function compiledFloatDecoder(input, context) {
1786
2076
  if (typeof input === "string") {
1787
- const parsedVal = Number(input);
1788
- if (!Number.isNaN(parsedVal)) {
1789
- return parsedVal;
2077
+ const decodedVal = Number(input);
2078
+ if (!Number.isNaN(decodedVal)) {
2079
+ return decodedVal;
1790
2080
  }
1791
- throw new timestamp.ValidationError({
1792
- message: `Unable to parse number from ${input}`,
1793
- errors: [
1794
- {
1795
- instancePath: "",
1796
- schemaPath: "/type"
1797
- }
1798
- ]
2081
+ context.errors.push({
2082
+ instancePath: "",
2083
+ schemaPath: "/type",
2084
+ message: `Unable to decode number from ${input}`
1799
2085
  });
2086
+ return undefined;
1800
2087
  }
1801
2088
  if (typeof input === "number" && !Number.isNaN(input)) {
1802
2089
  return input;
1803
2090
  }
1804
- throw new timestamp.ValidationError({
1805
- message: `Expected number. Got ${typeof input}`,
1806
- errors: [
1807
- {
1808
- instancePath: "",
1809
- schemaPath: "/type"
1810
- }
1811
- ]
2091
+ context.errors.push({
2092
+ instancePath: "",
2093
+ schemaPath: "/type",
2094
+ message: `Expected number. Got ${typeof input}`
1812
2095
  });
2096
+ return undefined;
1813
2097
  }
1814
- function nullableFloatParser(input) {
2098
+ function nullableFloatDecoder(input, context) {
1815
2099
  if (typeof input === "string") {
1816
- const parsedVal = Number(input);
1817
- if (!Number.isNaN(parsedVal)) {
1818
- return parsedVal;
2100
+ const decodedVal = Number(input);
2101
+ if (!Number.isNaN(decodedVal)) {
2102
+ return decodedVal;
1819
2103
  }
1820
2104
  if (input === "null") {
1821
2105
  return null;
1822
2106
  }
1823
- throw new timestamp.ValidationError({
1824
- message: `Unable to parse number from ${input}`,
1825
- errors: [
1826
- {
1827
- instancePath: "",
1828
- schemaPath: "/type"
1829
- }
1830
- ]
2107
+ context.errors.push({
2108
+ instancePath: "",
2109
+ schemaPath: "/type",
2110
+ message: `Unable to decode number from ${input}`
1831
2111
  });
2112
+ return undefined;
1832
2113
  }
1833
2114
  if (typeof input === "number" && !Number.isNaN(input)) {
1834
2115
  return input;
@@ -1836,17 +2117,14 @@ function nullableFloatParser(input) {
1836
2117
  if (input === null) {
1837
2118
  return null;
1838
2119
  }
1839
- throw new timestamp.ValidationError({
1840
- message: `Expected number or null. Got ${typeof input}`,
1841
- errors: [
1842
- {
1843
- instancePath: "",
1844
- schemaPath: "/type"
1845
- }
1846
- ]
2120
+ context.errors.push({
2121
+ instancePath: "",
2122
+ schemaPath: "/type",
2123
+ message: `Expected number or null. Got ${typeof input}`
1847
2124
  });
2125
+ return undefined;
1848
2126
  }
1849
- function bigIntParser(input, isUnsigned, isNullable) {
2127
+ function bigIntDecoder(input, isUnsigned, isNullable, context) {
1850
2128
  if (typeof input === "string" || typeof input === "number") {
1851
2129
  if (isNullable && input === "null") {
1852
2130
  return null;
@@ -1854,70 +2132,71 @@ function bigIntParser(input, isUnsigned, isNullable) {
1854
2132
  try {
1855
2133
  const val = BigInt(input);
1856
2134
  if (isUnsigned) {
1857
- if (val >= BigInt("0")) {
2135
+ if (val >= uint64Min && val <= uint64Max) {
1858
2136
  return val;
1859
2137
  }
1860
- throw new timestamp.ValidationError({
1861
- message: "uint64 must be greater than or equal to 0",
1862
- errors: [
1863
- {
1864
- message: "uint64 must be greater than or equal to 0",
1865
- schemaPath: "/type",
1866
- instancePath: ""
1867
- }
1868
- ]
2138
+ context.errors.push({
2139
+ message: "uint64 must an integer between 0 and 18,446,744,073,709,551,615",
2140
+ schemaPath: "/type",
2141
+ instancePath: ""
1869
2142
  });
2143
+ return void 0;
1870
2144
  }
1871
- return val;
2145
+ if (val >= int64Min && val <= int64Max) {
2146
+ return val;
2147
+ }
2148
+ context.errors.push({
2149
+ schemaPath: "/type",
2150
+ instancePath: "/",
2151
+ message: `int64 must be an integer between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807`
2152
+ });
2153
+ return void 0;
1872
2154
  } catch (err) {
1873
- throw new timestamp.ValidationError({
1874
- message: `Error transforming ${input} to BigInt`,
1875
- errors: [
1876
- {
1877
- schemaPath: "/type",
1878
- instancePath: "",
1879
- data: err
1880
- }
1881
- ]
2155
+ context.errors.push({
2156
+ schemaPath: "/type",
2157
+ instancePath: "",
2158
+ data: err,
2159
+ message: `Error transforming ${input} to BigInt`
1882
2160
  });
2161
+ return undefined;
1883
2162
  }
1884
2163
  }
1885
2164
  if (typeof input === "bigint") {
1886
2165
  if (isUnsigned) {
1887
- if (input >= BigInt("0")) {
2166
+ if (input >= uint64Min && input <= uint64Max) {
1888
2167
  return input;
1889
2168
  }
1890
- throw new timestamp.ValidationError({
2169
+ context.errors.push({
1891
2170
  message: "uint64 must be greater than or equal to 0",
1892
- errors: [
1893
- {
1894
- message: "uint64 must be greater than or equal to 0",
1895
- schemaPath: "/type",
1896
- instancePath: ""
1897
- }
1898
- ]
2171
+ schemaPath: "/type",
2172
+ instancePath: "/"
1899
2173
  });
1900
2174
  }
1901
- return input;
2175
+ if (input >= int64Min && input <= int64Max) {
2176
+ return input;
2177
+ }
2178
+ context.errors.push({
2179
+ schemaPath: "/type",
2180
+ instancePath: "/",
2181
+ message: `int64 must be an integer between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807`
2182
+ });
2183
+ return undefined;
1902
2184
  }
1903
2185
  if (isNullable && input === null) {
1904
2186
  return null;
1905
2187
  }
1906
- throw new timestamp.ValidationError({
1907
- message: "Expected BigInt or Integer string",
1908
- errors: [
1909
- {
1910
- schemaPath: "/type",
1911
- instancePath: ""
1912
- }
1913
- ]
2188
+ context.errors.push({
2189
+ schemaPath: "/type",
2190
+ instancePath: "/",
2191
+ message: "Expected BigInt or Integer string"
1914
2192
  });
2193
+ return undefined;
1915
2194
  }
1916
- function nullableIntParser(input, minNum, maxNum) {
2195
+ function nullableIntDecoder(input, minNum, maxNum, context) {
1917
2196
  if (typeof input === "string") {
1918
- const parsedInput = Number(input);
1919
- if (Number.isInteger(parsedInput) && parsedInput >= minNum && parsedInput <= maxNum) {
1920
- return parsedInput;
2197
+ const decodedInput = Number(input);
2198
+ if (Number.isInteger(decodedInput) && decodedInput >= minNum && decodedInput <= maxNum) {
2199
+ return decodedInput;
1921
2200
  }
1922
2201
  if (input === "null") {
1923
2202
  return null;
@@ -1929,35 +2208,29 @@ function nullableIntParser(input, minNum, maxNum) {
1929
2208
  if (input === null) {
1930
2209
  return null;
1931
2210
  }
1932
- throw new timestamp.ValidationError({
1933
- message: `Expected valid integer between ${minNum} & ${maxNum} or null. Got ${typeof input}.`,
1934
- errors: [
1935
- {
1936
- instancePath: "",
1937
- schemaPath: "/type"
1938
- }
1939
- ]
2211
+ context.errors.push({
2212
+ instancePath: "",
2213
+ schemaPath: "/type",
2214
+ message: `Expected valid integer between ${minNum} & ${maxNum} or null. Got ${typeof input}.`
1940
2215
  });
2216
+ return undefined;
1941
2217
  }
1942
- function intParser(input, minNum, maxNum) {
2218
+ function intDecoder(input, minNum, maxNum, context) {
1943
2219
  if (typeof input === "string") {
1944
- const parsedInput = Number(input);
1945
- if (Number.isInteger(parsedInput) && parsedInput >= minNum && parsedInput <= maxNum) {
1946
- return parsedInput;
2220
+ const decodedInput = Number(input);
2221
+ if (Number.isInteger(decodedInput) && decodedInput >= minNum && decodedInput <= maxNum) {
2222
+ return decodedInput;
1947
2223
  }
1948
2224
  }
1949
2225
  if (typeof input === "number" && Number.isInteger(input) && input >= minNum && input <= maxNum) {
1950
2226
  return input;
1951
2227
  }
1952
- throw new timestamp.ValidationError({
1953
- message: `Expected valid integer between ${minNum} & ${maxNum}`,
1954
- errors: [
1955
- {
1956
- instancePath: "",
1957
- schemaPath: "/type"
1958
- }
1959
- ]
2228
+ context.errors.push({
2229
+ instancePath: "",
2230
+ schemaPath: "/type",
2231
+ message: `Expected valid integer between ${minNum} & ${maxNum}`
1960
2232
  });
2233
+ return undefined;
1961
2234
  }
1962
2235
  function getCompiledSerializer(schema) {
1963
2236
  const code = createSerializationV2Template("input", schema);
@@ -2089,193 +2362,1996 @@ function getCompiledSerializer(schema) {
2089
2362
  };
2090
2363
  }
2091
2364
 
2092
- const _namespace = {
2093
- __proto__: null,
2094
- any: timestamp.any,
2095
- array: timestamp.array,
2096
- boolean: timestamp.boolean,
2097
- clone: timestamp.clone,
2098
- coerce: timestamp.coerce,
2099
- compile: compile,
2100
- discriminator: timestamp.discriminator,
2101
- enumerator: timestamp.enumerator,
2102
- errors: timestamp.errors,
2103
- extend: timestamp.extend,
2104
- float32: timestamp.float32,
2105
- float64: timestamp.float64,
2106
- int16: timestamp.int16,
2107
- int32: timestamp.int32,
2108
- int64: timestamp.int64,
2109
- int8: timestamp.int8,
2110
- nullable: timestamp.nullable,
2111
- number: timestamp.number,
2112
- object: timestamp.object,
2113
- omit: timestamp.omit,
2114
- optional: timestamp.optional,
2115
- parse: timestamp.parse,
2116
- partial: timestamp.partial,
2117
- pick: timestamp.pick,
2118
- record: timestamp.record,
2119
- recursive: timestamp.recursive,
2120
- safeCoerce: timestamp.safeCoerce,
2121
- safeParse: timestamp.safeParse,
2122
- serialize: timestamp.serialize,
2123
- string: timestamp.string,
2124
- stringEnum: timestamp.stringEnum,
2125
- timestamp: timestamp.timestamp,
2126
- uint16: timestamp.uint16,
2127
- uint32: timestamp.uint32,
2128
- uint64: timestamp.uint64,
2129
- uint8: timestamp.uint8,
2130
- validate: timestamp.validate
2131
- };
2132
-
2133
- function isAdaptedSchema(input) {
2134
- return input.metadata[timestamp.SCHEMA_METADATA]._isAdapted === true;
2135
- }
2136
- function validatorFromAdaptedSchema(schema) {
2137
- const result = {
2138
- compiledCode: {
2139
- parse: "",
2140
- serialize: "",
2141
- validate: ""
2142
- },
2143
- validate: schema.metadata[timestamp.SCHEMA_METADATA].validate,
2144
- parse(input) {
2145
- const context = {
2146
- instancePath: "",
2147
- schemaPath: "",
2148
- errors: []
2149
- };
2150
- return schema.metadata[timestamp.SCHEMA_METADATA].parse(input, context);
2151
- },
2152
- safeParse(input) {
2153
- const context = {
2154
- instancePath: "",
2155
- schemaPath: "",
2156
- errors: []
2157
- };
2365
+ function any(options = {}) {
2366
+ const validate = (input) => true;
2367
+ const parse = (input, context) => {
2368
+ if (context.instancePath.length === 0 && typeof input === "string") {
2158
2369
  try {
2159
- const result2 = schema.metadata[timestamp.SCHEMA_METADATA].parse(
2160
- input,
2161
- context
2162
- );
2163
- return {
2164
- success: true,
2165
- value: result2
2166
- };
2167
- } catch (err) {
2168
- if (err instanceof timestamp.ValidationError) {
2169
- return {
2170
- success: false,
2171
- error: err
2172
- };
2370
+ return JSON.parse(input);
2371
+ } catch {
2372
+ return input;
2373
+ }
2374
+ }
2375
+ return input;
2376
+ };
2377
+ const validator = {
2378
+ output: undefined,
2379
+ parse,
2380
+ coerce: (input, context) => {
2381
+ if (context.instancePath.length === 0 && typeof input === "string") {
2382
+ try {
2383
+ return JSON.parse(input);
2384
+ } catch {
2385
+ return input;
2173
2386
  }
2174
- return {
2175
- success: false,
2176
- error: new timestamp.ValidationError({
2177
- message: context.errors[0]?.message ?? `Error parsing input. ${err}`,
2178
- errors: context.errors
2179
- })
2180
- };
2181
2387
  }
2388
+ return input;
2182
2389
  },
2390
+ validate,
2183
2391
  serialize(input) {
2184
- const context = {
2185
- instancePath: "",
2186
- schemaPath: "",
2187
- errors: []
2188
- };
2189
- return schema.metadata[timestamp.SCHEMA_METADATA].serialize(input, context);
2392
+ return JSON.stringify(input);
2393
+ }
2394
+ };
2395
+ const result = {
2396
+ metadata: {
2397
+ id: options.id,
2398
+ description: options.description,
2399
+ isDeprecated: options.isDeprecated
2400
+ },
2401
+ [adapters.VALIDATOR_KEY]: validator,
2402
+ "~standard": adapters.createStandardSchemaProperty(validate, parse)
2403
+ };
2404
+ adapters.hideInvalidProperties(result);
2405
+ return result;
2406
+ }
2407
+
2408
+ function array(schema, opts = {}) {
2409
+ const validateType = (input) => {
2410
+ return validate$5(schema, input);
2411
+ };
2412
+ const parseType = (input, context) => {
2413
+ return parse$5(schema, input, context, false);
2414
+ };
2415
+ const validator = {
2416
+ output: [],
2417
+ parse: parseType,
2418
+ coerce(input, context) {
2419
+ return parse$5(schema, input, context, true);
2420
+ },
2421
+ validate: validateType,
2422
+ serialize(input, context) {
2423
+ const strParts = [];
2424
+ for (let i = 0; i < input.length; i++) {
2425
+ const item = input[i];
2426
+ const part = schema[adapters.VALIDATOR_KEY].serialize(item, {
2427
+ instancePath: `${context.instancePath}/${i}`,
2428
+ schemaPath: `${context.schemaPath}/elements`,
2429
+ errors: context.errors,
2430
+ depth: context.depth + 1,
2431
+ maxDepth: context.maxDepth,
2432
+ exitOnFirstError: context.exitOnFirstError
2433
+ });
2434
+ if (!part) {
2435
+ return undefined;
2436
+ }
2437
+ strParts.push(part);
2438
+ }
2439
+ return `[${strParts.join(",")}]`;
2440
+ }
2441
+ };
2442
+ const result = {
2443
+ elements: schema,
2444
+ metadata: {
2445
+ id: opts.id,
2446
+ description: opts.description,
2447
+ isDeprecated: opts.isDeprecated
2448
+ },
2449
+ [adapters.VALIDATOR_KEY]: validator,
2450
+ "~standard": adapters.createStandardSchemaProperty(validateType, parseType)
2451
+ };
2452
+ adapters.hideInvalidProperties(result);
2453
+ return result;
2454
+ }
2455
+ function validate$5(innerSchema, input) {
2456
+ if (!Array.isArray(input)) {
2457
+ return false;
2458
+ }
2459
+ for (const item of input) {
2460
+ const isValid = innerSchema[adapters.VALIDATOR_KEY].validate(item);
2461
+ if (!isValid) {
2462
+ return false;
2463
+ }
2464
+ }
2465
+ return true;
2466
+ }
2467
+ function parse$5(innerSchema, input, context, coerce = false) {
2468
+ let parsedInput = input;
2469
+ if (context.instancePath.length === 0 && typeof input === "string") {
2470
+ try {
2471
+ parsedInput = JSON.parse(input);
2472
+ } catch (_) {
2473
+ context.errors.push({
2474
+ instancePath: context.instancePath,
2475
+ schemaPath: `${context.schemaPath}/elements`,
2476
+ message: `Error at ${context.instancePath}. Invalid JSON.`
2477
+ });
2478
+ return undefined;
2479
+ }
2480
+ }
2481
+ if (!Array.isArray(parsedInput)) {
2482
+ context.errors.push({
2483
+ instancePath: context.instancePath,
2484
+ schemaPath: `${context.schemaPath}/elements`,
2485
+ message: `Error at ${context.instancePath}. Expected array. Got ${typeof input}.`
2486
+ });
2487
+ return undefined;
2488
+ }
2489
+ const result = [];
2490
+ for (let i = 0; i < parsedInput.length; i++) {
2491
+ const item = parsedInput[i];
2492
+ if (coerce) {
2493
+ const parsedItem = innerSchema[adapters.VALIDATOR_KEY].coerce(item, {
2494
+ instancePath: `${context.instancePath}/${i}`,
2495
+ schemaPath: `${context.schemaPath}/elements`,
2496
+ errors: context.errors,
2497
+ depth: context.depth + 1,
2498
+ maxDepth: context.maxDepth,
2499
+ exitOnFirstError: context.exitOnFirstError
2500
+ });
2501
+ result.push(parsedItem);
2502
+ } else {
2503
+ const parsedItem = innerSchema[adapters.VALIDATOR_KEY].parse(item, {
2504
+ instancePath: `${context.instancePath}/${i}`,
2505
+ schemaPath: `${context.schemaPath}/elements`,
2506
+ errors: context.errors,
2507
+ depth: context.depth + 1,
2508
+ maxDepth: context.maxDepth,
2509
+ exitOnFirstError: context.exitOnFirstError
2510
+ });
2511
+ result.push(parsedItem);
2512
+ }
2513
+ }
2514
+ if (context.errors.length) {
2515
+ return undefined;
2516
+ }
2517
+ return result;
2518
+ }
2519
+
2520
+ function boolean(opts = {}) {
2521
+ const validator = {
2522
+ output: false,
2523
+ parse: parse$4,
2524
+ validate: validate$4,
2525
+ serialize: serialize$2,
2526
+ coerce(input, context) {
2527
+ if (validate$4(input)) {
2528
+ return input;
2529
+ }
2530
+ switch (input) {
2531
+ case "true":
2532
+ case "TRUE":
2533
+ case "1":
2534
+ case 1:
2535
+ return true;
2536
+ case "false":
2537
+ case "FALSE":
2538
+ case "0":
2539
+ case 0:
2540
+ return false;
2541
+ }
2542
+ context.errors.push({
2543
+ instancePath: context.instancePath,
2544
+ schemaPath: context.schemaPath,
2545
+ message: `Error at ${context.instancePath}. Unable to coerce ${input} to boolean.`
2546
+ });
2547
+ return undefined;
2548
+ }
2549
+ };
2550
+ const result = {
2551
+ type: "boolean",
2552
+ metadata: {
2553
+ id: opts.id,
2554
+ description: opts.description,
2555
+ isDeprecated: opts.isDeprecated
2556
+ },
2557
+ [adapters.VALIDATOR_KEY]: validator,
2558
+ "~standard": adapters.createStandardSchemaProperty(validate$4, parse$4)
2559
+ };
2560
+ adapters.hideInvalidProperties(result);
2561
+ return result;
2562
+ }
2563
+ function validate$4(input) {
2564
+ return typeof input === "boolean";
2565
+ }
2566
+ function parse$4(input, data) {
2567
+ if (validate$4(input)) {
2568
+ return input;
2569
+ }
2570
+ if (data.instancePath.length === 0 && typeof input === "string") {
2571
+ if (input === "true") {
2572
+ return true;
2573
+ }
2574
+ if (input === "false") {
2575
+ return false;
2576
+ }
2577
+ data.errors.push({
2578
+ instancePath: data.instancePath,
2579
+ schemaPath: `${data.schemaPath}/type`,
2580
+ message: `Error at ${data.instancePath}. Expected boolean. Got ${typeof input}.`
2581
+ });
2582
+ return undefined;
2583
+ }
2584
+ data.errors.push({
2585
+ instancePath: data.instancePath,
2586
+ schemaPath: `${data.schemaPath}/type`,
2587
+ message: `Error at ${data.instancePath}. Expected boolean. Got ${typeof input}.`
2588
+ });
2589
+ return undefined;
2590
+ }
2591
+ function serialize$2(input) {
2592
+ return `${input}`;
2593
+ }
2594
+
2595
+ function discriminator(propA, propB, propC) {
2596
+ const isIdShorthand = typeof propB === "string";
2597
+ const discriminator2 = isIdShorthand ? propB : propA;
2598
+ const mapping = isIdShorthand ? propC : propB;
2599
+ const opts = isIdShorthand ? { id: propA } : propC ?? {};
2600
+ if (isIdShorthand) {
2601
+ opts.id = propA;
2602
+ }
2603
+ const isType = (input) => {
2604
+ return validate$3(discriminator2, mapping, input);
2605
+ };
2606
+ const parseType = (input, context) => {
2607
+ return parse$3(discriminator2, mapping, input, context, false);
2608
+ };
2609
+ const validator = {
2610
+ output: {},
2611
+ validate: isType,
2612
+ parse: parseType,
2613
+ coerce: (input, context) => {
2614
+ return parse$3(discriminator2, mapping, input, context, true);
2615
+ },
2616
+ serialize(input, context) {
2617
+ const discriminatorVal = input[discriminator2] ?? "";
2618
+ const targetSchema = mapping[discriminatorVal];
2619
+ if (!targetSchema) {
2620
+ context.errors.push(
2621
+ discriminatorMappingError(discriminatorVal, context)
2622
+ );
2623
+ return undefined;
2624
+ }
2625
+ const result2 = targetSchema[adapters.VALIDATOR_KEY].serialize(input, {
2626
+ instancePath: context.instancePath,
2627
+ schemaPath: `${context.schemaPath}/mapping/${discriminatorVal}`,
2628
+ errors: context.errors,
2629
+ discriminatorKey: discriminator2,
2630
+ discriminatorValue: discriminatorVal,
2631
+ depth: context.depth,
2632
+ maxDepth: context.maxDepth,
2633
+ exitOnFirstError: context.exitOnFirstError
2634
+ });
2635
+ return result2;
2636
+ }
2637
+ };
2638
+ const result = {
2639
+ discriminator: discriminator2,
2640
+ mapping,
2641
+ metadata: {
2642
+ id: opts.id,
2643
+ description: opts.description,
2644
+ isDeprecated: opts.isDeprecated
2645
+ },
2646
+ [adapters.VALIDATOR_KEY]: validator,
2647
+ "~standard": adapters.createStandardSchemaProperty(isType, parseType)
2648
+ };
2649
+ adapters.hideInvalidProperties(result);
2650
+ return result;
2651
+ }
2652
+ function validate$3(discriminator2, mapping, input) {
2653
+ if (!adapters.isObject(input)) {
2654
+ return false;
2655
+ }
2656
+ if (!(discriminator2 in input)) {
2657
+ return false;
2658
+ }
2659
+ const discriminatorOptions = Object.keys(mapping);
2660
+ if (!discriminatorOptions.includes(input[discriminator2])) {
2661
+ return false;
2662
+ }
2663
+ const targetSchema = mapping[input[discriminator2]];
2664
+ if (!targetSchema) {
2665
+ return false;
2666
+ }
2667
+ return targetSchema[adapters.VALIDATOR_KEY].validate(input);
2668
+ }
2669
+ function parse$3(discriminator2, mapping, input, context, coerce = false) {
2670
+ let parsedInput = input;
2671
+ if (typeof input === "string" && input.length && context.instancePath.length === 0) {
2672
+ try {
2673
+ parsedInput = JSON.parse(input);
2674
+ } catch (err) {
2675
+ context.errors.push({
2676
+ message: err instanceof Error ? err.message : `${err}`,
2677
+ data: err,
2678
+ instancePath: context.instancePath,
2679
+ schemaPath: context.schemaPath
2680
+ });
2681
+ return undefined;
2682
+ }
2683
+ }
2684
+ if (!adapters.isObject(parsedInput)) {
2685
+ context.errors.push({
2686
+ instancePath: context.instancePath,
2687
+ schemaPath: `${context.schemaPath}/discriminator`,
2688
+ message: `Error at ${context.instancePath}. Expected object. Got ${typeof parsedInput}.`
2689
+ });
2690
+ return undefined;
2691
+ }
2692
+ if (!(discriminator2 in parsedInput)) {
2693
+ context.errors.push({
2694
+ instancePath: `${context.instancePath}/${discriminator2}`,
2695
+ schemaPath: `${context.schemaPath}/discriminator`,
2696
+ message: `Error at ${context.instancePath}/${discriminator2}. Discriminator field "${discriminator2}" cannot be undefined`
2697
+ });
2698
+ return undefined;
2699
+ }
2700
+ const acceptedDiscriminatorVals = Object.keys(mapping);
2701
+ const discriminatorVal = parsedInput[discriminator2];
2702
+ if (!acceptedDiscriminatorVals.includes(discriminatorVal)) {
2703
+ context.errors.push({
2704
+ instancePath: `${context.instancePath}/${discriminator2}`,
2705
+ schemaPath: `${context.schemaPath}/discriminator`,
2706
+ message: `Error at ${context.instancePath}/${discriminator2}. "${parsedInput[discriminator2]}" is not one of the accepted discriminator values: [${acceptedDiscriminatorVals.join(
2707
+ ", "
2708
+ )}]`
2709
+ });
2710
+ return undefined;
2711
+ }
2712
+ const targetSchema = mapping[parsedInput[discriminator2]];
2713
+ if (!targetSchema) {
2714
+ return undefined;
2715
+ }
2716
+ if (coerce) {
2717
+ const result2 = targetSchema[adapters.VALIDATOR_KEY].coerce(parsedInput, {
2718
+ instancePath: context.instancePath,
2719
+ schemaPath: `${context.schemaPath}/mapping/${discriminatorVal}`,
2720
+ errors: context.errors,
2721
+ discriminatorKey: discriminator2,
2722
+ discriminatorValue: discriminatorVal,
2723
+ depth: context.depth + 1,
2724
+ maxDepth: context.maxDepth,
2725
+ exitOnFirstError: context.exitOnFirstError
2726
+ });
2727
+ return result2;
2728
+ }
2729
+ const result = targetSchema[adapters.VALIDATOR_KEY].parse(parsedInput, {
2730
+ instancePath: context.instancePath,
2731
+ schemaPath: `${context.schemaPath}/mapping/${discriminatorVal}`,
2732
+ errors: context.errors,
2733
+ discriminatorKey: discriminator2,
2734
+ discriminatorValue: discriminatorVal,
2735
+ depth: context.depth + 1,
2736
+ maxDepth: context.maxDepth,
2737
+ exitOnFirstError: context.exitOnFirstError
2738
+ });
2739
+ return result;
2740
+ }
2741
+ function discriminatorMappingError(discriminatorVal, data) {
2742
+ return {
2743
+ message: `Error fetching discriminator schema. Mapping for "${discriminatorVal} is undefined.`,
2744
+ instancePath: data.instancePath,
2745
+ schemaPath: data.schemaPath
2746
+ };
2747
+ }
2748
+
2749
+ const stringEnum = enumerator;
2750
+ function enumerator(paramA, paramB) {
2751
+ const isIdShorthand = typeof paramA === "string";
2752
+ const enumVal = isIdShorthand ? paramB : paramA;
2753
+ const meta = isIdShorthand ? { id: paramA } : paramB;
2754
+ const isType = (input) => {
2755
+ if (typeof input !== "string") {
2756
+ return false;
2757
+ }
2758
+ for (const val of enumVal) {
2759
+ if (val === input) {
2760
+ return true;
2761
+ }
2762
+ }
2763
+ return false;
2764
+ };
2765
+ const parse = (input, context) => {
2766
+ if (isType(input)) {
2767
+ return input;
2768
+ }
2769
+ context.errors.push({
2770
+ instancePath: context.instancePath,
2771
+ schemaPath: `${context.schemaPath}/enum`,
2772
+ message: `Error at ${context.instancePath}. Invalid enum value. Expected one of the following values: [${enumVal.join(
2773
+ ", "
2774
+ )}]`
2775
+ });
2776
+ return undefined;
2777
+ };
2778
+ const validator = {
2779
+ output: paramA[0] ?? "",
2780
+ parse,
2781
+ coerce: (input, context) => {
2782
+ if (isType(input)) {
2783
+ return input;
2784
+ }
2785
+ context.errors.push({
2786
+ instancePath: context.instancePath,
2787
+ schemaPath: `${context.schemaPath}/enum`,
2788
+ message: `Error at ${context.instancePath}. Invalid enum value. Expected one of the following values: [${enumVal.join(
2789
+ ", "
2790
+ )}]`
2791
+ });
2792
+ return undefined;
2793
+ },
2794
+ validate: isType,
2795
+ serialize(input, context) {
2796
+ if (context.instancePath.length === 0) {
2797
+ return input;
2798
+ }
2799
+ return `"${input}"`;
2800
+ }
2801
+ };
2802
+ const result = {
2803
+ enum: enumVal,
2804
+ metadata: {
2805
+ id: meta?.id,
2806
+ description: meta?.description,
2807
+ isDeprecated: meta?.isDeprecated
2808
+ },
2809
+ [adapters.VALIDATOR_KEY]: validator,
2810
+ "~standard": adapters.createStandardSchemaProperty(isType, parse)
2811
+ };
2812
+ adapters.hideInvalidProperties(result);
2813
+ return result;
2814
+ }
2815
+
2816
+ function nullable(schema, opts = {}) {
2817
+ const isType = (val) => {
2818
+ if (val === null) {
2819
+ return true;
2820
+ }
2821
+ return schema[adapters.VALIDATOR_KEY].validate(val);
2822
+ };
2823
+ const parse = (val, data) => {
2824
+ if (data.instancePath.length === 0 && typeof val === "string" && val === "null") {
2825
+ return null;
2826
+ }
2827
+ if (val === null) {
2828
+ return null;
2829
+ }
2830
+ return schema[adapters.VALIDATOR_KEY].parse(val, data);
2831
+ };
2832
+ const validator = {
2833
+ output: null,
2834
+ optional: schema[adapters.VALIDATOR_KEY].optional,
2835
+ validate: isType,
2836
+ parse,
2837
+ coerce(val, data) {
2838
+ if (val === null) {
2839
+ return null;
2840
+ }
2841
+ if (val === "null") {
2842
+ return null;
2843
+ }
2844
+ return schema[adapters.VALIDATOR_KEY].coerce(val, data);
2845
+ },
2846
+ serialize(val, data) {
2847
+ if (val === null) {
2848
+ return "null";
2849
+ }
2850
+ return schema[adapters.VALIDATOR_KEY].serialize(val, data);
2851
+ }
2852
+ };
2853
+ const result = {
2854
+ ...schema,
2855
+ nullable: true,
2856
+ metadata: {
2857
+ id: opts.id ?? schema.metadata?.id,
2858
+ description: opts.description ?? schema.metadata?.description,
2859
+ isDeprecated: opts.isDeprecated ?? schema.metadata?.isDeprecated
2860
+ },
2861
+ [adapters.VALIDATOR_KEY]: validator,
2862
+ "~standard": adapters.createStandardSchemaProperty(
2863
+ validator.validate,
2864
+ validator.parse
2865
+ )
2866
+ };
2867
+ adapters.hideInvalidProperties(result);
2868
+ return result;
2869
+ }
2870
+ function optional(input, opts = {}) {
2871
+ const isType = (val) => {
2872
+ if (val === undefined) {
2873
+ return true;
2874
+ }
2875
+ return input[adapters.VALIDATOR_KEY].validate(val);
2876
+ };
2877
+ const parse = (val, context) => {
2878
+ if (typeof val === "undefined") {
2879
+ return undefined;
2880
+ }
2881
+ if (context.instancePath.length === 0 && val === "undefined") {
2882
+ return undefined;
2883
+ }
2884
+ return input[adapters.VALIDATOR_KEY].parse(val, context);
2885
+ };
2886
+ const validator = {
2887
+ output: undefined,
2888
+ optional: true,
2889
+ validate: isType,
2890
+ parse,
2891
+ coerce: (val, data) => {
2892
+ if (typeof val === "undefined") {
2893
+ return undefined;
2894
+ }
2895
+ if (val === "undefined") {
2896
+ return undefined;
2897
+ }
2898
+ return input[adapters.VALIDATOR_KEY].coerce(val, data);
2899
+ },
2900
+ serialize: (val, data) => {
2901
+ if (typeof val === "undefined") {
2902
+ return "undefined";
2903
+ }
2904
+ return input[adapters.VALIDATOR_KEY].serialize(val, data);
2905
+ }
2906
+ };
2907
+ const result = {
2908
+ ...input,
2909
+ metadata: {
2910
+ id: opts.id ?? input.metadata?.id,
2911
+ description: opts.description ?? input.metadata?.description,
2912
+ isDeprecated: opts.isDeprecated ?? input.metadata?.isDeprecated
2913
+ },
2914
+ [adapters.VALIDATOR_KEY]: validator,
2915
+ "~standard": adapters.createStandardSchemaProperty(isType, parse)
2916
+ };
2917
+ adapters.hideInvalidProperties(result);
2918
+ return result;
2919
+ }
2920
+ function clone(input, opts = {}) {
2921
+ const validator = {
2922
+ ...input[adapters.VALIDATOR_KEY]
2923
+ };
2924
+ const schema = {
2925
+ ...input,
2926
+ metadata: {
2927
+ id: opts.id,
2928
+ description: opts.description,
2929
+ isDeprecated: opts.isDeprecated
2930
+ },
2931
+ [adapters.VALIDATOR_KEY]: validator,
2932
+ "~standard": adapters.createStandardSchemaProperty(
2933
+ validator.validate,
2934
+ validator.parse
2935
+ )
2936
+ };
2937
+ adapters.hideInvalidProperties(schema);
2938
+ return schema;
2939
+ }
2940
+
2941
+ function number(opts = {}) {
2942
+ return float64(opts);
2943
+ }
2944
+ function coerceNumber(input, options) {
2945
+ if (typeof input === "string") {
2946
+ const decodedInput = Number(input);
2947
+ if (Number.isNaN(decodedInput)) {
2948
+ options.errors.push({
2949
+ instancePath: `${options.instancePath}`,
2950
+ schemaPath: `${options.schemaPath}/type`,
2951
+ message: `Error at ${options.instancePath}. Unable to coerce ${typeof input} into a number`
2952
+ });
2953
+ return undefined;
2954
+ }
2955
+ return decodeNumber(decodedInput, options);
2956
+ }
2957
+ return decodeNumber(input, options);
2958
+ }
2959
+ function float32(opts = {}) {
2960
+ return numberScalarType("float32", opts, () => {
2961
+ return {
2962
+ success: true
2963
+ };
2964
+ });
2965
+ }
2966
+ function float64(opts = {}) {
2967
+ return numberScalarType("float64", opts, () => {
2968
+ return {
2969
+ success: true
2970
+ };
2971
+ });
2972
+ }
2973
+ function int8(opts = {}) {
2974
+ return numberScalarType("int8", opts, (input) => {
2975
+ const isValid = validateInt(input, int8Min, int8Max);
2976
+ if (isValid) {
2977
+ return {
2978
+ success: true
2979
+ };
2980
+ }
2981
+ return {
2982
+ success: false,
2983
+ message: `Must be an valid integer between ${int8Min} and ${int8Max}`
2984
+ };
2985
+ });
2986
+ }
2987
+ function uint8(opts = {}) {
2988
+ return numberScalarType("uint8", opts, (input) => {
2989
+ const isValid = validateInt(input, uint8Min, uint8Max);
2990
+ if (isValid) {
2991
+ return {
2992
+ success: true
2993
+ };
2994
+ }
2995
+ return {
2996
+ success: false,
2997
+ message: `Must be a valid integer between ${uint8Min} and ${uint8Max}`
2998
+ };
2999
+ });
3000
+ }
3001
+ function int16(opts = {}) {
3002
+ return numberScalarType("int16", opts, (input) => {
3003
+ const isValid = validateInt(input, int16Min, int16Max);
3004
+ if (isValid) {
3005
+ return {
3006
+ success: true
3007
+ };
3008
+ }
3009
+ return {
3010
+ success: false,
3011
+ message: `Must be a valid integer between ${int16Min} and ${int16Max}`
3012
+ };
3013
+ });
3014
+ }
3015
+ function uint16(opts = {}) {
3016
+ return numberScalarType("uint16", opts, (input) => {
3017
+ const isValid = validateInt(input, uint16Min, uint16Max);
3018
+ if (isValid) {
3019
+ return {
3020
+ success: true
3021
+ };
3022
+ }
3023
+ return {
3024
+ success: false,
3025
+ message: `Must be a valid integer between ${uint16Min} and ${uint16Max}`
3026
+ };
3027
+ });
3028
+ }
3029
+ function int32(opts = {}) {
3030
+ return numberScalarType("int32", opts, (input) => {
3031
+ const isValid = validateInt(input, int32Min, int32Max);
3032
+ if (isValid) {
3033
+ return {
3034
+ success: true
3035
+ };
3036
+ }
3037
+ return {
3038
+ success: false,
3039
+ message: `Must be a valid integer between ${int32Min} and ${int32Max}`
3040
+ };
3041
+ });
3042
+ }
3043
+ function uint32(opts = {}) {
3044
+ return numberScalarType("uint32", opts, (input) => {
3045
+ const isValid = validateInt(input, uint32Min, uint32Max);
3046
+ if (isValid) {
3047
+ return { success: true };
3048
+ }
3049
+ return {
3050
+ success: false,
3051
+ message: `Must be a valid integer between ${uint32Min} and ${uint32Max}`
3052
+ };
3053
+ });
3054
+ }
3055
+ function int64(opts = {}) {
3056
+ function isType(input) {
3057
+ return typeof input === "bigint" && input >= int64Min && input <= int64Max;
3058
+ }
3059
+ function parse(input, context) {
3060
+ if (typeof input === "string" || typeof input === "number") {
3061
+ try {
3062
+ const val = BigInt(input);
3063
+ if (isType(val)) {
3064
+ return val;
3065
+ }
3066
+ context.errors.push({
3067
+ message: `Error at ${context.instancePath}. Invalid int64.`,
3068
+ schemaPath: `${context.schemaPath}/type`,
3069
+ instancePath: context.instancePath
3070
+ });
3071
+ return void 0;
3072
+ } catch (_) {
3073
+ context.errors.push({
3074
+ message: `Error at ${context.instancePath}. Unable to transform ${input} to BigInt`,
3075
+ schemaPath: `${context.schemaPath}/type`,
3076
+ instancePath: context.instancePath
3077
+ });
3078
+ return undefined;
3079
+ }
3080
+ }
3081
+ if (isType(input)) {
3082
+ return input;
3083
+ }
3084
+ context.errors.push({
3085
+ message: `Error at ${context.instancePath}. Expected BigInt or integer string`,
3086
+ schemaPath: `${context.schemaPath}/type`,
3087
+ instancePath: context.instancePath
3088
+ });
3089
+ return undefined;
3090
+ }
3091
+ const validator = {
3092
+ output: BigInt("0"),
3093
+ validate: isType,
3094
+ parse,
3095
+ coerce: parse,
3096
+ serialize(input, context) {
3097
+ if (context.instancePath.length === 0) {
3098
+ return input.toString();
3099
+ }
3100
+ return `"${input.toString()}"`;
3101
+ }
3102
+ };
3103
+ const result = {
3104
+ type: "int64",
3105
+ metadata: {
3106
+ id: opts.id,
3107
+ description: opts.description,
3108
+ isDeprecated: opts.isDeprecated
3109
+ },
3110
+ [adapters.VALIDATOR_KEY]: validator,
3111
+ "~standard": adapters.createStandardSchemaProperty(isType, parse)
3112
+ };
3113
+ adapters.hideInvalidProperties(result);
3114
+ return result;
3115
+ }
3116
+ function uint64(opts = {}) {
3117
+ function isType(input) {
3118
+ return typeof input === "bigint" && input >= uint64Min && input <= uint64Max;
3119
+ }
3120
+ function parse(input, context) {
3121
+ if (typeof input === "string" || typeof input === "number") {
3122
+ try {
3123
+ const val = BigInt(input);
3124
+ if (isType(val)) {
3125
+ return val;
3126
+ }
3127
+ context.errors.push({
3128
+ message: `Error at ${context.instancePath}. Invalid uint64.`,
3129
+ schemaPath: `${context.schemaPath}/type`,
3130
+ instancePath: context.instancePath
3131
+ });
3132
+ return void 0;
3133
+ } catch (_) {
3134
+ context.errors.push({
3135
+ message: `Error at ${context.instancePath}. Unable to transform ${input} to BigInt.`,
3136
+ schemaPath: `${context.schemaPath}/type`,
3137
+ instancePath: context.instancePath
3138
+ });
3139
+ return undefined;
3140
+ }
3141
+ }
3142
+ if (isType(input)) {
3143
+ return input;
3144
+ }
3145
+ context.errors.push({
3146
+ message: `Error at ${context.instancePath}. Expected BigInt or integer string.`,
3147
+ schemaPath: `${context.schemaPath}/type`,
3148
+ instancePath: context.instancePath
3149
+ });
3150
+ return undefined;
3151
+ }
3152
+ const validator = {
3153
+ output: BigInt("0"),
3154
+ validate: isType,
3155
+ parse,
3156
+ coerce: parse,
3157
+ serialize(input, context) {
3158
+ if (context.instancePath.length === 0) {
3159
+ return input.toString();
3160
+ }
3161
+ return `"${input.toString()}"`;
3162
+ }
3163
+ };
3164
+ const result = {
3165
+ type: "uint64",
3166
+ metadata: {
3167
+ id: opts.id,
3168
+ description: opts.description,
3169
+ isDeprecated: opts.isDeprecated
3170
+ },
3171
+ [adapters.VALIDATOR_KEY]: validator,
3172
+ "~standard": adapters.createStandardSchemaProperty(isType, parse)
3173
+ };
3174
+ adapters.hideInvalidProperties(result);
3175
+ return result;
3176
+ }
3177
+ function validateNumber(input) {
3178
+ return typeof input === "number";
3179
+ }
3180
+ function validateInt(input, minVal, maxValue) {
3181
+ return Number.isInteger(input) && input >= minVal && input <= maxValue;
3182
+ }
3183
+ function serializeNumber(input, _context) {
3184
+ return input.toString();
3185
+ }
3186
+ function decodeNumber(input, context) {
3187
+ if (context.instancePath.length === 0 && typeof input === "string") {
3188
+ const result = Number(input);
3189
+ if (Number.isNaN(result)) {
3190
+ context?.errors.push({
3191
+ instancePath: context.instancePath,
3192
+ schemaPath: `${context.schemaPath}/type`,
3193
+ message: `Error at ${context.instancePath}. Invalid number.`
3194
+ });
3195
+ return undefined;
3196
+ }
3197
+ return result;
3198
+ }
3199
+ if (typeof input === "number" && !Number.isNaN(input)) {
3200
+ return input;
3201
+ }
3202
+ context?.errors.push({
3203
+ instancePath: context.instancePath,
3204
+ schemaPath: `${context.schemaPath}/type`,
3205
+ message: `Error at ${context.instancePath}. Invalid number.`
3206
+ });
3207
+ return undefined;
3208
+ }
3209
+ function numberScalarType(type, opts, numTypeMatcher) {
3210
+ const validate = (input) => {
3211
+ const isNum = validateNumber(input);
3212
+ if (!isNum) {
3213
+ return false;
3214
+ }
3215
+ return numTypeMatcher(input).success;
3216
+ };
3217
+ const parse = (input, context) => {
3218
+ const result2 = decodeNumber(input, context);
3219
+ if (context.errors.length) {
3220
+ return undefined;
3221
+ }
3222
+ if (typeof result2 !== "number") {
3223
+ context?.errors.push({
3224
+ instancePath: context.instancePath,
3225
+ schemaPath: `${context.schemaPath}/type`,
3226
+ message: `Error at ${context.instancePath}. Invalid number.`
3227
+ });
3228
+ return undefined;
3229
+ }
3230
+ const matchResult = numTypeMatcher(result2);
3231
+ if (matchResult.success) {
3232
+ return result2;
3233
+ }
3234
+ context?.errors.push({
3235
+ instancePath: context.instancePath,
3236
+ schemaPath: `${context.schemaPath}/type`,
3237
+ message: `Error at ${context.instancePath}. ${matchResult.message}`
3238
+ });
3239
+ return undefined;
3240
+ };
3241
+ const validator = {
3242
+ output: 0,
3243
+ validate,
3244
+ parse,
3245
+ serialize: serializeNumber,
3246
+ coerce(input, options) {
3247
+ const result2 = coerceNumber(input, options);
3248
+ if (typeof result2 !== "number") {
3249
+ options.errors.push({
3250
+ instancePath: options.instancePath,
3251
+ schemaPath: `${options.schemaPath}/type`,
3252
+ message: `Error at ${options.instancePath}. Unable to coerce ${typeof input} to ${type}.`
3253
+ });
3254
+ return undefined;
3255
+ }
3256
+ const matchResult = numTypeMatcher(result2);
3257
+ if (matchResult.success) {
3258
+ return result2;
3259
+ }
3260
+ options.errors.push({
3261
+ instancePath: options.instancePath,
3262
+ schemaPath: `${options.schemaPath}/type`,
3263
+ message: `Error at ${options.instancePath}. ${matchResult.message}`
3264
+ });
3265
+ return undefined;
3266
+ }
3267
+ };
3268
+ const result = {
3269
+ type,
3270
+ metadata: {
3271
+ id: opts.id,
3272
+ description: opts.description,
3273
+ isDeprecated: opts.isDeprecated
3274
+ },
3275
+ [adapters.VALIDATOR_KEY]: validator,
3276
+ "~standard": adapters.createStandardSchemaProperty(validate, parse)
3277
+ };
3278
+ adapters.hideInvalidProperties(result);
3279
+ return result;
3280
+ }
3281
+
3282
+ function object(propA, propB) {
3283
+ const isIdShorthand = typeof propA === "string";
3284
+ const input = isIdShorthand ? propB : propA;
3285
+ const options = isIdShorthand ? { id: propA } : propB ?? {};
3286
+ const schema = {
3287
+ properties: {}
3288
+ };
3289
+ if (typeof options.strict === "boolean") {
3290
+ schema.strict = options.strict;
3291
+ }
3292
+ for (const key of Object.keys(input)) {
3293
+ const prop = input[key];
3294
+ if (prop[adapters.VALIDATOR_KEY].optional) {
3295
+ if (!schema.optionalProperties) {
3296
+ schema.optionalProperties = {};
3297
+ }
3298
+ schema.optionalProperties[key] = prop;
3299
+ continue;
3300
+ }
3301
+ schema.properties[key] = prop;
3302
+ }
3303
+ const validate = (input2) => {
3304
+ return validateObjectSchema(schema, input2);
3305
+ };
3306
+ const decode = (input2, context) => {
3307
+ return decodeObjectSchema(
3308
+ schema,
3309
+ input2,
3310
+ context,
3311
+ false
3312
+ );
3313
+ };
3314
+ const validator = {
3315
+ output: {},
3316
+ parse: decode,
3317
+ coerce(input2, context) {
3318
+ return decodeObjectSchema(
3319
+ schema,
3320
+ input2,
3321
+ context,
3322
+ true
3323
+ );
3324
+ },
3325
+ validate,
3326
+ serialize(input2, context) {
3327
+ return serializeObject(
3328
+ schema,
3329
+ input2,
3330
+ context
3331
+ );
3332
+ }
3333
+ };
3334
+ const result = {
3335
+ ...schema,
3336
+ metadata: {
3337
+ id: options.id,
3338
+ description: options.description,
3339
+ isDeprecated: options.isDeprecated
3340
+ },
3341
+ [adapters.VALIDATOR_KEY]: validator,
3342
+ "~standard": adapters.createStandardSchemaProperty(validate, decode)
3343
+ };
3344
+ adapters.hideInvalidProperties(result);
3345
+ return result;
3346
+ }
3347
+ function decodeObjectSchema(schema, input, context, coerce = false) {
3348
+ let parsedInput = input;
3349
+ if (context.instancePath.length === 0 && typeof input === "string") {
3350
+ try {
3351
+ const result2 = JSON.parse(input);
3352
+ parsedInput = result2;
3353
+ } catch (_err) {
3354
+ context.errors.push({
3355
+ instancePath: context.instancePath,
3356
+ schemaPath: `${context.schemaPath}/properties`,
3357
+ message: `Error at ${context.instancePath}. Invalid JSON.`
3358
+ });
3359
+ return undefined;
3360
+ }
3361
+ }
3362
+ if (!adapters.isObject(parsedInput)) {
3363
+ context.errors.push({
3364
+ instancePath: context.instancePath,
3365
+ schemaPath: `${context.schemaPath}/properties`,
3366
+ message: `Error at ${context.instancePath}. Expected object`
3367
+ });
3368
+ return undefined;
3369
+ }
3370
+ const result = {};
3371
+ const optionalProps = schema.optionalProperties ?? {};
3372
+ const inputKeys = Object.keys(parsedInput);
3373
+ const requiredKeys = Object.keys(schema.properties);
3374
+ const optionalKeys = Object.keys(optionalProps);
3375
+ if (schema.strict) {
3376
+ for (const key of inputKeys) {
3377
+ if (!requiredKeys.includes(key) && !optionalKeys.includes(key) && key !== context.discriminatorKey) {
3378
+ context.errors.push({
3379
+ instancePath: `${context.instancePath}/${key}`,
3380
+ schemaPath: `${context.schemaPath}/strict`,
3381
+ message: `Error at ${context.instancePath}/${key}. Key '${key}' is not included in the schema. To allow additional input properties set strict to 'false'.`
3382
+ });
3383
+ }
3384
+ }
3385
+ }
3386
+ if (context.errors.length) {
3387
+ return undefined;
3388
+ }
3389
+ if (context.discriminatorKey) {
3390
+ result[context.discriminatorKey] = context.discriminatorValue;
3391
+ }
3392
+ for (const key of requiredKeys) {
3393
+ const val = parsedInput[key];
3394
+ const prop = schema.properties[key];
3395
+ if (coerce) {
3396
+ result[key] = prop[adapters.VALIDATOR_KEY].coerce(val, {
3397
+ instancePath: `${context.instancePath}/${key}`,
3398
+ schemaPath: `${context.schemaPath}/properties/${key}`,
3399
+ errors: context.errors,
3400
+ depth: context.depth + 1,
3401
+ maxDepth: context.maxDepth,
3402
+ exitOnFirstError: context.exitOnFirstError
3403
+ });
3404
+ if (context.errors.length && context.exitOnFirstError) {
3405
+ return undefined;
3406
+ }
3407
+ continue;
3408
+ }
3409
+ result[key] = prop[adapters.VALIDATOR_KEY].parse(val, {
3410
+ instancePath: `${context.instancePath}/${key}`,
3411
+ schemaPath: `${context.schemaPath}/properties/${key}`,
3412
+ errors: context.errors,
3413
+ depth: context.depth + 1,
3414
+ maxDepth: context.maxDepth,
3415
+ exitOnFirstError: context.exitOnFirstError
3416
+ });
3417
+ if (context.errors.length && context.exitOnFirstError) {
3418
+ return undefined;
3419
+ }
3420
+ }
3421
+ for (const key of optionalKeys) {
3422
+ const val = parsedInput[key];
3423
+ const prop = optionalProps[key];
3424
+ if (val === undefined) {
3425
+ continue;
3426
+ }
3427
+ if (coerce) {
3428
+ result[key] = prop[adapters.VALIDATOR_KEY].coerce(val, {
3429
+ instancePath: `${context.instancePath}/${key}`,
3430
+ schemaPath: `${context.schemaPath}/optionalProperties/${key}`,
3431
+ errors: context.errors,
3432
+ depth: context.depth + 1,
3433
+ maxDepth: context.maxDepth,
3434
+ exitOnFirstError: context.exitOnFirstError
3435
+ });
3436
+ if (context.errors.length && context.exitOnFirstError) {
3437
+ return undefined;
3438
+ }
3439
+ continue;
3440
+ }
3441
+ if (typeof val !== "undefined") {
3442
+ result[key] = prop[adapters.VALIDATOR_KEY].parse(val, {
3443
+ instancePath: `${context.instancePath}/${key}`,
3444
+ schemaPath: `${context.schemaPath}/optionalProperties/${key}`,
3445
+ errors: context.errors,
3446
+ depth: context.depth + 1,
3447
+ maxDepth: context.maxDepth,
3448
+ exitOnFirstError: context.exitOnFirstError
3449
+ });
3450
+ if (context.errors.length && context.exitOnFirstError) {
3451
+ return undefined;
3452
+ }
3453
+ }
3454
+ }
3455
+ if (context.errors.length) {
3456
+ return undefined;
3457
+ }
3458
+ return result;
3459
+ }
3460
+ function validateObjectSchema(schema, input) {
3461
+ if (!adapters.isObject(input)) {
3462
+ return false;
3463
+ }
3464
+ const allowedKeys = schema.strict ? [] : undefined;
3465
+ for (const key of Object.keys(schema.properties)) {
3466
+ const propSchema = schema.properties[key];
3467
+ const isValid = propSchema[adapters.VALIDATOR_KEY].validate(input[key]);
3468
+ if (!isValid) return false;
3469
+ allowedKeys?.push(key);
3470
+ }
3471
+ if (schema.optionalProperties) {
3472
+ for (const key of Object.keys(schema.optionalProperties)) {
3473
+ const propsSchema = schema.optionalProperties[key];
3474
+ const isValid = propsSchema[adapters.VALIDATOR_KEY].validate(input[key]);
3475
+ if (!isValid) return false;
3476
+ allowedKeys?.push(key);
3477
+ }
3478
+ }
3479
+ if (allowedKeys) {
3480
+ for (const key of Object.keys(input)) {
3481
+ if (!allowedKeys.includes(key)) return false;
3482
+ }
3483
+ }
3484
+ return true;
3485
+ }
3486
+ function pick(inputSchema, keys, options = {}) {
3487
+ const schema = {
3488
+ properties: {},
3489
+ nullable: inputSchema.nullable
3490
+ };
3491
+ if (typeof options.strict === "boolean") {
3492
+ schema.strict = options.strict;
3493
+ }
3494
+ Object.keys(inputSchema.properties).forEach((key) => {
3495
+ if (!schema.properties) {
3496
+ return;
3497
+ }
3498
+ if (keys.includes(key)) {
3499
+ schema.properties[key] = inputSchema.properties[key];
3500
+ }
3501
+ });
3502
+ if (inputSchema.optionalProperties) {
3503
+ schema.optionalProperties = {};
3504
+ Object.keys(inputSchema.optionalProperties).forEach((key) => {
3505
+ if (!schema.optionalProperties || !inputSchema.optionalProperties) {
3506
+ return;
3507
+ }
3508
+ if (keys.includes(key)) {
3509
+ schema.optionalProperties[key] = inputSchema.optionalProperties[key];
3510
+ }
3511
+ });
3512
+ }
3513
+ const parse = (input, context) => {
3514
+ return decodeObjectSchema(schema, input, context, false);
3515
+ };
3516
+ const validate = (input) => {
3517
+ return validateObjectSchema(schema, input);
3518
+ };
3519
+ const result = {
3520
+ ...schema,
3521
+ metadata: {
3522
+ id: options.id,
3523
+ description: options.description,
3524
+ isDeprecated: options.isDeprecated
3525
+ },
3526
+ [adapters.VALIDATOR_KEY]: {
3527
+ output: {},
3528
+ parse,
3529
+ coerce(input, context) {
3530
+ return decodeObjectSchema(schema, input, context, false);
3531
+ },
3532
+ validate,
3533
+ serialize(input, context) {
3534
+ return serializeObject(schema, input, context);
3535
+ }
3536
+ },
3537
+ "~standard": adapters.createStandardSchemaProperty(validate, parse)
3538
+ };
3539
+ adapters.hideInvalidProperties(result);
3540
+ return result;
3541
+ }
3542
+ function omit(inputSchema, keys, options = {}) {
3543
+ const schema = {
3544
+ properties: {
3545
+ ...inputSchema.properties
3546
+ },
3547
+ nullable: inputSchema.nullable
3548
+ };
3549
+ if (typeof options.strict === "boolean") {
3550
+ schema.strict = options.strict;
3551
+ }
3552
+ Object.keys(inputSchema.properties).forEach((key) => {
3553
+ if (keys.includes(key)) {
3554
+ delete schema.properties[key];
3555
+ }
3556
+ });
3557
+ if (inputSchema.optionalProperties) {
3558
+ schema.optionalProperties = {
3559
+ ...inputSchema.optionalProperties
3560
+ };
3561
+ Object.keys(inputSchema.optionalProperties).forEach((key) => {
3562
+ if (!schema.optionalProperties) {
3563
+ return;
3564
+ }
3565
+ if (keys.includes(key)) {
3566
+ delete schema.optionalProperties[key];
3567
+ }
3568
+ });
3569
+ }
3570
+ const validate = (input) => validateObjectSchema(schema, input);
3571
+ const parse = (input, context) => decodeObjectSchema(schema, input, context, false);
3572
+ const result = {
3573
+ ...schema,
3574
+ metadata: {
3575
+ id: options.id,
3576
+ description: options.description,
3577
+ isDeprecated: options.isDeprecated
3578
+ },
3579
+ [adapters.VALIDATOR_KEY]: {
3580
+ output: {},
3581
+ validate,
3582
+ parse,
3583
+ serialize(input, context) {
3584
+ return serializeObject(schema, input, context);
3585
+ },
3586
+ coerce(input, context) {
3587
+ return decodeObjectSchema(schema, input, context, true);
3588
+ }
3589
+ },
3590
+ "~standard": adapters.createStandardSchemaProperty(validate, parse)
3591
+ };
3592
+ adapters.hideInvalidProperties(result);
3593
+ return result;
3594
+ }
3595
+ function serializeObject(schema, input, context) {
3596
+ const strParts = [];
3597
+ if (context.discriminatorKey && context.discriminatorValue) {
3598
+ strParts.push(
3599
+ `"${context.discriminatorKey}":"${context.discriminatorValue}"`
3600
+ );
3601
+ }
3602
+ for (const key of Object.keys(schema.properties)) {
3603
+ const prop = schema.properties[key];
3604
+ const val = input[key];
3605
+ if (typeof val !== "undefined") {
3606
+ strParts.push(
3607
+ `"${key}":${prop[adapters.VALIDATOR_KEY].serialize(val, {
3608
+ instancePath: `${context.instancePath}/${key}`,
3609
+ schemaPath: `${context.schemaPath}/properties/${key}`,
3610
+ errors: context.errors,
3611
+ depth: context.depth + 1,
3612
+ maxDepth: context.maxDepth,
3613
+ exitOnFirstError: context.exitOnFirstError
3614
+ })}`
3615
+ );
3616
+ }
3617
+ }
3618
+ if (schema.optionalProperties) {
3619
+ for (const key of Object.keys(schema.optionalProperties)) {
3620
+ const prop = schema.optionalProperties[key];
3621
+ const val = input[key];
3622
+ if (typeof val !== "undefined") {
3623
+ strParts.push(
3624
+ `"${key}":${prop[adapters.VALIDATOR_KEY].serialize(val, {
3625
+ instancePath: `${context.instancePath}/${key}`,
3626
+ schemaPath: `${context.schemaPath}/optionalProperties/${key}`,
3627
+ errors: context.errors,
3628
+ depth: context.depth + 1,
3629
+ maxDepth: context.maxDepth,
3630
+ exitOnFirstError: context.exitOnFirstError
3631
+ })}`
3632
+ );
3633
+ }
3634
+ }
3635
+ }
3636
+ return `{${strParts.join(",")}}`;
3637
+ }
3638
+ function extend(baseSchema, inputSchema, options = {}) {
3639
+ const schema = {
3640
+ properties: {
3641
+ ...baseSchema.properties,
3642
+ ...inputSchema.properties
3643
+ },
3644
+ optionalProperties: {
3645
+ ...baseSchema.optionalProperties,
3646
+ ...inputSchema.optionalProperties
3647
+ },
3648
+ metadata: {
3649
+ id: options.id,
3650
+ description: options.description
3651
+ }
3652
+ };
3653
+ if (typeof options.strict === "boolean") {
3654
+ schema.strict = options.strict;
3655
+ }
3656
+ const isType = (input) => validateObjectSchema(schema, input);
3657
+ const validator = {
3658
+ output: {},
3659
+ parse(input, context) {
3660
+ return decodeObjectSchema(schema, input, context, false);
3661
+ },
3662
+ coerce(input, context) {
3663
+ return decodeObjectSchema(schema, input, context, true);
3664
+ },
3665
+ validate: isType,
3666
+ serialize(input, context) {
3667
+ return serializeObject(schema, input, context);
3668
+ }
3669
+ };
3670
+ const result = {
3671
+ ...schema,
3672
+ [adapters.VALIDATOR_KEY]: validator,
3673
+ "~standard": adapters.createStandardSchemaProperty(
3674
+ validator.validate,
3675
+ validator.parse
3676
+ )
3677
+ };
3678
+ adapters.hideInvalidProperties(result);
3679
+ return result;
3680
+ }
3681
+ function partial(schema, options = {}) {
3682
+ const newSchema = {
3683
+ properties: {},
3684
+ optionalProperties: {},
3685
+ nullable: schema.nullable,
3686
+ metadata: {
3687
+ id: options.id,
3688
+ description: options.description
3689
+ }
3690
+ };
3691
+ if (typeof options.strict === "boolean") {
3692
+ schema.strict = options.strict;
3693
+ }
3694
+ for (const key of Object.keys(schema.properties)) {
3695
+ const prop = schema.properties[key];
3696
+ newSchema.optionalProperties[key] = optional(prop);
3697
+ }
3698
+ if (schema.optionalProperties && newSchema.optionalProperties) {
3699
+ for (const key of Object.keys(schema.optionalProperties)) {
3700
+ const prop = schema.optionalProperties[key];
3701
+ if (prop[adapters.VALIDATOR_KEY].optional) {
3702
+ newSchema.optionalProperties[key] = prop;
3703
+ } else {
3704
+ newSchema.optionalProperties[key] = optional(prop);
3705
+ }
3706
+ }
3707
+ }
3708
+ const validate = (input) => {
3709
+ return validateObjectSchema(newSchema, input);
3710
+ };
3711
+ const parse = (input, context) => {
3712
+ return decodeObjectSchema(newSchema, input, context, false);
3713
+ };
3714
+ const validator = {
3715
+ output: {},
3716
+ optional: schema[adapters.VALIDATOR_KEY].optional,
3717
+ validate,
3718
+ parse,
3719
+ coerce(input, context) {
3720
+ return decodeObjectSchema(newSchema, input, context, true);
3721
+ },
3722
+ serialize(input, context) {
3723
+ return serializeObject(schema, input, context);
3724
+ }
3725
+ };
3726
+ const result = {
3727
+ ...newSchema,
3728
+ [adapters.VALIDATOR_KEY]: validator,
3729
+ ["~standard"]: adapters.createStandardSchemaProperty(
3730
+ validator.validate,
3731
+ validator.parse
3732
+ )
3733
+ };
3734
+ adapters.hideInvalidProperties(result);
3735
+ return result;
3736
+ }
3737
+
3738
+ function string(opts = {}) {
3739
+ const validator = {
3740
+ output: "",
3741
+ parse: decode,
3742
+ coerce: coerce$2,
3743
+ validate: validate$2,
3744
+ serialize(input, context) {
3745
+ if (context.instancePath.length === 0) {
3746
+ return input;
3747
+ }
3748
+ if (input.length < 42) {
3749
+ return serializeSmallString(input);
3750
+ }
3751
+ if (input.length < 5e3 && !STR_ESCAPE.test(input)) {
3752
+ return `"${input}"`;
3753
+ }
3754
+ return JSON.stringify(input);
3755
+ }
3756
+ };
3757
+ const result = {
3758
+ type: "string",
3759
+ metadata: {
3760
+ id: opts.id,
3761
+ description: opts.description,
3762
+ isDeprecated: opts.isDeprecated
3763
+ },
3764
+ [adapters.VALIDATOR_KEY]: validator,
3765
+ "~standard": adapters.createStandardSchemaProperty(validate$2, decode)
3766
+ };
3767
+ adapters.hideInvalidProperties(result);
3768
+ return result;
3769
+ }
3770
+ function serializeString(input) {
3771
+ if (input.length < 42) {
3772
+ return serializeSmallString(input);
3773
+ }
3774
+ if (input.length < 5e3 && !STR_ESCAPE.test(input)) {
3775
+ return `"${input}"`;
3776
+ }
3777
+ return JSON.stringify(input);
3778
+ }
3779
+ function validate$2(input) {
3780
+ return typeof input === "string";
3781
+ }
3782
+ function decode(input, context) {
3783
+ if (validate$2(input)) {
3784
+ return input;
3785
+ }
3786
+ context.errors.push({
3787
+ instancePath: context.instancePath,
3788
+ schemaPath: `${context.schemaPath}/type`,
3789
+ message: `Error at ${context.instancePath}. Expected 'string' got ${typeof input}.`
3790
+ });
3791
+ return undefined;
3792
+ }
3793
+ function coerce$2(input, context) {
3794
+ return decode(input, context);
3795
+ }
3796
+ const STR_ESCAPE = (
3797
+ // eslint-disable-next-line no-control-regex
3798
+ /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/
3799
+ );
3800
+ function serializeSmallString(input) {
3801
+ const len = input.length;
3802
+ let result = "";
3803
+ let last = -1;
3804
+ let point = 255;
3805
+ for (let i = 0; i < len; i++) {
3806
+ point = input.charCodeAt(i);
3807
+ if (point < 32 || point >= 55296 && point <= 57343) {
3808
+ return JSON.stringify(input);
3809
+ }
3810
+ if (point === 34 || // '"'
3811
+ point === 92) {
3812
+ if (last === -1) {
3813
+ last = 0;
3814
+ }
3815
+ result += input.slice(last, i) + "\\";
3816
+ last = i;
3817
+ }
3818
+ }
3819
+ if (last === -1) {
3820
+ return `"${input}"`;
3821
+ }
3822
+ return `"${result}${input.slice(last)}"`;
3823
+ }
3824
+
3825
+ function record(schema, opts = {}) {
3826
+ const validateFn = (input) => {
3827
+ if (!adapters.isObject(input)) {
3828
+ return false;
3829
+ }
3830
+ for (const key of Object.keys(input)) {
3831
+ const val = input[key];
3832
+ const isValid = schema[adapters.VALIDATOR_KEY].validate(val);
3833
+ if (!isValid) {
3834
+ return false;
3835
+ }
3836
+ }
3837
+ return true;
3838
+ };
3839
+ const parseFn = (input, ctx) => {
3840
+ return parse$2(schema, input, ctx, false);
3841
+ };
3842
+ const validator = {
3843
+ output: {},
3844
+ validate: validateFn,
3845
+ parse: parseFn,
3846
+ coerce(input, data) {
3847
+ return parse$2(schema, input, data, true);
3848
+ },
3849
+ serialize(input, context) {
3850
+ if (context.depth >= context.maxDepth) {
3851
+ context.errors.push({
3852
+ instancePath: context.instancePath,
3853
+ schemaPath: context.schemaPath,
3854
+ message: "Max depth reached"
3855
+ });
3856
+ return undefined;
3857
+ }
3858
+ let result2 = "{";
3859
+ const keys = Object.keys(input);
3860
+ for (let i = 0; i < keys.length; i++) {
3861
+ const key = keys[i];
3862
+ const val = input[key];
3863
+ if (i > 0) result2 += ",";
3864
+ result2 += serializeString(key);
3865
+ result2 += ":";
3866
+ result2 += schema[adapters.VALIDATOR_KEY].serialize(val, {
3867
+ instancePath: `${context.instancePath}/${key}`,
3868
+ schemaPath: `${context.schemaPath}/values`,
3869
+ errors: context.errors,
3870
+ exitOnFirstError: context.exitOnFirstError,
3871
+ depth: context.depth + 1,
3872
+ maxDepth: context.maxDepth
3873
+ });
3874
+ }
3875
+ result2 += "}";
3876
+ return result2;
3877
+ }
3878
+ };
3879
+ const result = {
3880
+ values: schema,
3881
+ metadata: {
3882
+ id: opts.id,
3883
+ description: opts.description,
3884
+ isDeprecated: opts.isDeprecated
3885
+ },
3886
+ [adapters.VALIDATOR_KEY]: validator,
3887
+ "~standard": adapters.createStandardSchemaProperty(validateFn, parseFn)
3888
+ };
3889
+ adapters.hideInvalidProperties(result);
3890
+ return result;
3891
+ }
3892
+ function parse$2(schema, input, data, coerce = false) {
3893
+ if (data.depth >= data.maxDepth) {
3894
+ data.errors.push({
3895
+ instancePath: data.instancePath,
3896
+ schemaPath: data.schemaPath,
3897
+ message: "Max depth reached"
3898
+ });
3899
+ return undefined;
3900
+ }
3901
+ let parsedInput = input;
3902
+ if (data.instancePath.length === 0 && typeof input === "string" && input.length > 0) {
3903
+ try {
3904
+ parsedInput = JSON.parse(input);
3905
+ } catch (err) {
3906
+ data.errors.push({
3907
+ message: err instanceof Error ? err.message : `${err}`,
3908
+ instancePath: data.instancePath,
3909
+ schemaPath: data.schemaPath,
3910
+ data: err
3911
+ });
3912
+ return undefined;
3913
+ }
3914
+ }
3915
+ if (!adapters.isObject(parsedInput)) {
3916
+ data.errors.push({
3917
+ instancePath: data.instancePath,
3918
+ schemaPath: `${data.schemaPath}/values`,
3919
+ message: `Error at ${data.instancePath} Expected object`
3920
+ });
3921
+ return undefined;
3922
+ }
3923
+ const result = {};
3924
+ for (const key of Object.keys(parsedInput)) {
3925
+ const val = parsedInput[key];
3926
+ if (coerce) {
3927
+ result[key] = schema[adapters.VALIDATOR_KEY].coerce(val, {
3928
+ instancePath: `${data.instancePath}/${key}`,
3929
+ schemaPath: `${data.schemaPath}/values`,
3930
+ errors: data.errors,
3931
+ maxDepth: data.maxDepth,
3932
+ depth: data.depth + 1,
3933
+ exitOnFirstError: data.exitOnFirstError
3934
+ });
3935
+ } else {
3936
+ result[key] = schema[adapters.VALIDATOR_KEY].parse(val, {
3937
+ instancePath: `${data.instancePath}/${key}`,
3938
+ schemaPath: `${data.schemaPath}/values`,
3939
+ errors: data.errors,
3940
+ maxDepth: data.maxDepth,
3941
+ depth: data.depth + 1,
3942
+ exitOnFirstError: data.exitOnFirstError
3943
+ });
3944
+ }
3945
+ }
3946
+ if (data.errors.length) {
3947
+ return undefined;
3948
+ }
3949
+ return result;
3950
+ }
3951
+
3952
+ let recursiveTypeCount = 0;
3953
+ function recursive(propA, propB) {
3954
+ const isIdShorthand = typeof propA === "string";
3955
+ const callback = isIdShorthand ? propB : propA;
3956
+ const options = isIdShorthand ? { id: propA } : propB ?? {};
3957
+ if (!options.id) {
3958
+ recursiveTypeCount++;
3959
+ console.warn(
3960
+ `[@arrirpc/schema] WARNING: It is highly recommended to specify an ID for recursive types.`
3961
+ );
3962
+ }
3963
+ const id = options.id ?? `TypeRef${recursiveTypeCount}`;
3964
+ const recursiveFns = {};
3965
+ const validator = {
3966
+ output: "",
3967
+ parse(input, context) {
3968
+ if (context.depth >= context.maxDepth) {
3969
+ context.errors.push({
3970
+ message: "Max depth exceeded",
3971
+ instancePath: context.instancePath,
3972
+ schemaPath: context.schemaPath
3973
+ });
3974
+ return undefined;
3975
+ }
3976
+ if (recursiveFns[id]) {
3977
+ return recursiveFns[id].parse(input, {
3978
+ ...context,
3979
+ depth: context.depth + 1
3980
+ });
3981
+ }
3982
+ },
3983
+ serialize(input, context) {
3984
+ if (context.depth >= context.maxDepth) {
3985
+ context.errors.push({
3986
+ message: "Max depth exceeded",
3987
+ instancePath: context.instancePath,
3988
+ schemaPath: context.schemaPath
3989
+ });
3990
+ return undefined;
3991
+ }
3992
+ if (recursiveFns[id]) {
3993
+ return recursiveFns[id].serialize(input, context);
3994
+ }
3995
+ return "";
2190
3996
  },
2191
- "~standard": timestamp.createStandardSchemaProperty(
2192
- schema.metadata[timestamp.SCHEMA_METADATA].validate,
2193
- schema.metadata[timestamp.SCHEMA_METADATA].parse
3997
+ validate(input) {
3998
+ if (recursiveFns[id]) {
3999
+ return recursiveFns[id].validate(input);
4000
+ }
4001
+ return false;
4002
+ },
4003
+ coerce(input, context) {
4004
+ if (context.depth >= context.maxDepth) {
4005
+ context.errors.push({
4006
+ message: "Max depth exceeded",
4007
+ instancePath: context.instancePath,
4008
+ schemaPath: context.schemaPath
4009
+ });
4010
+ return undefined;
4011
+ }
4012
+ if (recursiveFns[id]) {
4013
+ return recursiveFns[id].coerce(input, context);
4014
+ }
4015
+ }
4016
+ };
4017
+ const refSchema = {
4018
+ ref: id,
4019
+ [adapters.VALIDATOR_KEY]: validator,
4020
+ "~standard": adapters.createStandardSchemaProperty(
4021
+ validator.validate,
4022
+ validator.parse
2194
4023
  )
2195
4024
  };
4025
+ adapters.hideInvalidProperties(refSchema);
4026
+ const mainSchema = callback(refSchema);
4027
+ if (!mainSchema.metadata) mainSchema.metadata = {};
4028
+ mainSchema.metadata.id = id;
4029
+ mainSchema.metadata.description = options?.description ?? mainSchema.metadata.description;
4030
+ mainSchema.metadata.isDeprecated = options?.isDeprecated ?? mainSchema.metadata.isDeprecated;
4031
+ recursiveFns[id] = {
4032
+ validate: mainSchema[adapters.VALIDATOR_KEY].validate,
4033
+ parse: mainSchema[adapters.VALIDATOR_KEY].parse,
4034
+ serialize: mainSchema[adapters.VALIDATOR_KEY].serialize,
4035
+ coerce: mainSchema[adapters.VALIDATOR_KEY].coerce
4036
+ };
4037
+ return mainSchema;
4038
+ }
4039
+
4040
+ function timestamp(opts = {}) {
4041
+ const validator = {
4042
+ output: /* @__PURE__ */ new Date(),
4043
+ validate: validate$1,
4044
+ parse: parse$1,
4045
+ coerce: coerce$1,
4046
+ serialize: serialize$1
4047
+ };
4048
+ const result = {
4049
+ type: "timestamp",
4050
+ metadata: {
4051
+ id: opts.id,
4052
+ description: opts.description,
4053
+ isDeprecated: opts.isDeprecated
4054
+ },
4055
+ [adapters.VALIDATOR_KEY]: validator,
4056
+ "~standard": adapters.createStandardSchemaProperty(validate$1, parse$1)
4057
+ };
4058
+ adapters.hideInvalidProperties(result);
4059
+ return result;
4060
+ }
4061
+ function validate$1(input) {
4062
+ return typeof input === "object" && input instanceof Date;
4063
+ }
4064
+ function parse$1(input, data) {
4065
+ try {
4066
+ if (validate$1(input)) {
4067
+ return input;
4068
+ }
4069
+ if (typeof input === "string") {
4070
+ const result = Date.parse(input);
4071
+ if (Number.isNaN(result)) {
4072
+ data?.errors.push({
4073
+ message: `Error at ${data.instancePath}. Invalid date string.`,
4074
+ instancePath: data.instancePath,
4075
+ schemaPath: `${data.schemaPath}/type`
4076
+ });
4077
+ return void 0;
4078
+ }
4079
+ return new Date(result);
4080
+ }
4081
+ data.errors.push({
4082
+ message: `Error at ${data.instancePath}. Invalid date.`,
4083
+ instancePath: data.instancePath,
4084
+ schemaPath: `${data.schemaPath}/type`
4085
+ });
4086
+ return void 0;
4087
+ } catch (err) {
4088
+ data.errors.push({
4089
+ message: err instanceof Error ? err.message : `${err}`,
4090
+ instancePath: data.instancePath,
4091
+ schemaPath: data.schemaPath,
4092
+ data: err
4093
+ });
4094
+ return undefined;
4095
+ }
4096
+ }
4097
+ function coerce$1(input, options) {
4098
+ if (typeof input === "number") {
4099
+ return new Date(input);
4100
+ }
4101
+ return parse$1(input, options);
4102
+ }
4103
+ function serialize$1(input, data) {
4104
+ if (data.instancePath.length === 0) {
4105
+ return input.toISOString();
4106
+ }
4107
+ return `"${input.toISOString()}"`;
4108
+ }
4109
+
4110
+ function sanitizeJson(json) {
4111
+ return json.replace(/[\b]/g, "\\b").replace(/[\f]/g, "\\f").replace(/[\n]/g, "\\n").replace(/[\r]/g, "\\r").replace(/[\t]/g, "\\t");
4112
+ }
4113
+ function validate(schema, input) {
4114
+ return schema[adapters.VALIDATOR_KEY].validate(input);
4115
+ }
4116
+ function parse(schema, input) {
4117
+ const ctx = adapters.newValidationContext();
4118
+ const result = schema[adapters.VALIDATOR_KEY].parse(input, ctx);
4119
+ if (ctx.errors.length) {
4120
+ return {
4121
+ success: false,
4122
+ errors: ctx.errors
4123
+ };
4124
+ }
4125
+ return {
4126
+ success: true,
4127
+ value: result
4128
+ };
4129
+ }
4130
+ function parseUnsafe(schema, input) {
4131
+ const ctx = adapters.newValidationContext();
4132
+ const result = schema[adapters.VALIDATOR_KEY].parse(input, ctx);
4133
+ if (ctx.errors.length) {
4134
+ throw new ValidationException({
4135
+ message: `Unable to parse input. ${ctx.errors[0].message}`,
4136
+ errors: ctx.errors
4137
+ });
4138
+ }
4139
+ return result;
4140
+ }
4141
+ function coerce(schema, input) {
4142
+ const ctx = adapters.newValidationContext();
4143
+ const result = schema[adapters.VALIDATOR_KEY].coerce(input, ctx);
4144
+ if (ctx.errors.length) {
4145
+ return {
4146
+ success: false,
4147
+ errors: ctx.errors
4148
+ };
4149
+ }
4150
+ return {
4151
+ success: true,
4152
+ value: result
4153
+ };
4154
+ }
4155
+ function coerceUnsafe(schema, input) {
4156
+ const ctx = adapters.newValidationContext();
4157
+ const result = schema[adapters.VALIDATOR_KEY].coerce(input, ctx);
4158
+ if (ctx.errors.length) {
4159
+ throw new ValidationException({
4160
+ message: `Unable to coerce input. ${ctx.errors[0]?.message}`,
4161
+ errors: ctx.errors
4162
+ });
4163
+ }
2196
4164
  return result;
2197
4165
  }
4166
+ function serialize(schema, input) {
4167
+ try {
4168
+ const context = adapters.newValidationContext();
4169
+ const result = schema[adapters.VALIDATOR_KEY].serialize(input, context);
4170
+ if (context.errors.length || typeof result === "undefined") {
4171
+ return {
4172
+ success: false,
4173
+ errors: context.errors
4174
+ };
4175
+ }
4176
+ return {
4177
+ success: true,
4178
+ value: result
4179
+ };
4180
+ } catch (err) {
4181
+ if (err instanceof ValidationException) {
4182
+ return {
4183
+ success: false,
4184
+ errors: err.errors
4185
+ };
4186
+ }
4187
+ const errList = errors(schema, input);
4188
+ if (errList.length) {
4189
+ return {
4190
+ success: false,
4191
+ errors: errList
4192
+ };
4193
+ }
4194
+ return {
4195
+ success: false,
4196
+ errors: [
4197
+ {
4198
+ message: err instanceof Error ? err.message : `${err}`,
4199
+ instancePath: "/",
4200
+ data: err
4201
+ }
4202
+ ]
4203
+ };
4204
+ }
4205
+ }
4206
+ function serializeUnsafe(schema, input) {
4207
+ const result = serialize(schema, input);
4208
+ if (!result.success) {
4209
+ throw new ValidationException({
4210
+ message: result.errors.length ? result.errors[0].message : `Unknown error`,
4211
+ errors: result.errors
4212
+ });
4213
+ }
4214
+ return result.value;
4215
+ }
4216
+ function errors(schema, input, exitOnFirstError = false) {
4217
+ const errorList = [];
4218
+ try {
4219
+ schema[adapters.VALIDATOR_KEY].parse(input, {
4220
+ errors: errorList,
4221
+ instancePath: "",
4222
+ schemaPath: "",
4223
+ depth: 0,
4224
+ maxDepth: 500,
4225
+ exitOnFirstError
4226
+ });
4227
+ } catch (err) {
4228
+ errorList.push({ instancePath: "", schemaPath: "", message: `${err}` });
4229
+ }
4230
+ return errorList;
4231
+ }
4232
+
4233
+ const _namespace = {
4234
+ __proto__: null,
4235
+ any: any,
4236
+ array: array,
4237
+ boolean: boolean,
4238
+ clone: clone,
4239
+ coerce: coerce,
4240
+ coerceUnsafe: coerceUnsafe,
4241
+ compile: compile,
4242
+ discriminator: discriminator,
4243
+ enumerator: enumerator,
4244
+ errors: errors,
4245
+ extend: extend,
4246
+ float32: float32,
4247
+ float64: float64,
4248
+ int16: int16,
4249
+ int32: int32,
4250
+ int64: int64,
4251
+ int8: int8,
4252
+ nullable: nullable,
4253
+ number: number,
4254
+ object: object,
4255
+ omit: omit,
4256
+ optional: optional,
4257
+ parse: parse,
4258
+ parseUnsafe: parseUnsafe,
4259
+ partial: partial,
4260
+ pick: pick,
4261
+ record: record,
4262
+ recursive: recursive,
4263
+ serialize: serialize,
4264
+ serializeUnsafe: serializeUnsafe,
4265
+ string: string,
4266
+ stringEnum: stringEnum,
4267
+ timestamp: timestamp,
4268
+ uint16: uint16,
4269
+ uint32: uint32,
4270
+ uint64: uint64,
4271
+ uint8: uint8,
4272
+ validate: validate
4273
+ };
2198
4274
 
2199
- exports.NumberTypeValues = timestamp.NumberTypeValues;
2200
- exports.SCHEMA_METADATA = timestamp.SCHEMA_METADATA;
2201
- exports.STR_ESCAPE = timestamp.STR_ESCAPE;
2202
- exports.ValidationError = timestamp.ValidationError;
2203
- exports.any = timestamp.any;
2204
- exports.array = timestamp.array;
2205
- exports.boolean = timestamp.boolean;
2206
- exports.clone = timestamp.clone;
2207
- exports.coerce = timestamp.coerce;
2208
- exports.createStandardSchemaProperty = timestamp.createStandardSchemaProperty;
2209
- exports.discriminator = timestamp.discriminator;
2210
- exports.enumerator = timestamp.enumerator;
2211
- exports.errors = timestamp.errors;
2212
- exports.extend = timestamp.extend;
2213
- exports.float32 = timestamp.float32;
2214
- exports.float64 = timestamp.float64;
2215
- exports.hideInvalidProperties = timestamp.hideInvalidProperties;
2216
- exports.int16 = timestamp.int16;
2217
- exports.int16Max = timestamp.int16Max;
2218
- exports.int16Min = timestamp.int16Min;
2219
- exports.int32 = timestamp.int32;
2220
- exports.int32Max = timestamp.int32Max;
2221
- exports.int32Min = timestamp.int32Min;
2222
- exports.int64 = timestamp.int64;
2223
- exports.int64Max = timestamp.int64Max;
2224
- exports.int64Min = timestamp.int64Min;
2225
- exports.int8 = timestamp.int8;
2226
- exports.int8Max = timestamp.int8Max;
2227
- exports.int8Min = timestamp.int8Min;
2228
- exports.isAAraySchema = timestamp.isAAraySchema;
2229
- exports.isADiscriminatorSchema = timestamp.isADiscriminatorSchema;
2230
- exports.isAObjectSchema = timestamp.isAObjectSchema;
2231
- exports.isARecordSchema = timestamp.isARecordSchema;
2232
- exports.isARefSchema = timestamp.isARefSchema;
2233
- exports.isAScalarSchema = timestamp.isAScalarSchema;
2234
- exports.isASchema = timestamp.isASchema;
2235
- exports.isAStringEnumSchema = timestamp.isAStringEnumSchema;
2236
- exports.isObject = timestamp.isObject;
2237
- exports.isValidationError = timestamp.isValidationError;
2238
- exports.nullable = timestamp.nullable;
2239
- exports.number = timestamp.number;
2240
- exports.object = timestamp.object;
2241
- exports.omit = timestamp.omit;
2242
- exports.optional = timestamp.optional;
2243
- exports.parse = timestamp.parse;
2244
- exports.parseObjectSchema = timestamp.parseObjectSchema;
2245
- exports.partial = timestamp.partial;
2246
- exports.pick = timestamp.pick;
2247
- exports.record = timestamp.record;
2248
- exports.recursive = timestamp.recursive;
2249
- exports.safeCoerce = timestamp.safeCoerce;
2250
- exports.safeParse = timestamp.safeParse;
2251
- exports.sanitizeJson = timestamp.sanitizeJson;
2252
- exports.serialize = timestamp.serialize;
2253
- exports.serializeObject = timestamp.serializeObject;
2254
- exports.serializeSmallString = timestamp.serializeSmallString;
2255
- exports.serializeString = timestamp.serializeString;
2256
- exports.string = timestamp.string;
2257
- exports.stringEnum = timestamp.stringEnum;
2258
- exports.timestamp = timestamp.timestamp;
2259
- exports.uint16 = timestamp.uint16;
2260
- exports.uint16Max = timestamp.uint16Max;
2261
- exports.uint16Min = timestamp.uint16Min;
2262
- exports.uint32 = timestamp.uint32;
2263
- exports.uint32Max = timestamp.uint32Max;
2264
- exports.uint32Min = timestamp.uint32Min;
2265
- exports.uint64 = timestamp.uint64;
2266
- exports.uint64Max = timestamp.uint64Max;
2267
- exports.uint64Min = timestamp.uint64Min;
2268
- exports.uint8 = timestamp.uint8;
2269
- exports.uint8Max = timestamp.uint8Max;
2270
- exports.uint8Min = timestamp.uint8Min;
2271
- exports.validate = timestamp.validate;
2272
- exports.validateObjectSchema = timestamp.validateObjectSchema;
4275
+ exports.NumberTypeValues = adapters.NumberTypeValues;
4276
+ exports.VALIDATOR_KEY = adapters.VALIDATOR_KEY;
4277
+ exports.isAAraySchema = adapters.isAAraySchema;
4278
+ exports.isADiscriminatorSchema = adapters.isADiscriminatorSchema;
4279
+ exports.isAObjectSchema = adapters.isAObjectSchema;
4280
+ exports.isARecordSchema = adapters.isARecordSchema;
4281
+ exports.isARefSchema = adapters.isARefSchema;
4282
+ exports.isAScalarSchema = adapters.isAScalarSchema;
4283
+ exports.isASchema = adapters.isASchema;
4284
+ exports.isAStringEnumSchema = adapters.isAStringEnumSchema;
4285
+ exports.isObject = adapters.isObject;
4286
+ exports.newValidationContext = adapters.newValidationContext;
4287
+ exports.STR_ESCAPE = STR_ESCAPE;
4288
+ exports.ValidationException = ValidationException;
2273
4289
  exports.a = _namespace;
4290
+ exports.any = any;
4291
+ exports.array = array;
4292
+ exports.boolean = boolean;
4293
+ exports.clone = clone;
4294
+ exports.coerce = coerce;
4295
+ exports.coerceUnsafe = coerceUnsafe;
2274
4296
  exports.compile = compile;
4297
+ exports.decodeObjectSchema = decodeObjectSchema;
4298
+ exports.discriminator = discriminator;
4299
+ exports.enumerator = enumerator;
4300
+ exports.errorMessageFromErrors = errorMessageFromErrors;
4301
+ exports.errors = errors;
4302
+ exports.extend = extend;
4303
+ exports.float32 = float32;
4304
+ exports.float64 = float64;
2275
4305
  exports.getCompiledParser = getCompiledParser;
2276
4306
  exports.getCompiledSerializer = getCompiledSerializer;
2277
- exports.getSchemaParsingCode = createParsingTemplate;
4307
+ exports.getSchemaDecodingCode = createParsingTemplate;
2278
4308
  exports.getSchemaSerializationCode = createSerializationV2Template;
2279
4309
  exports.getSchemaValidationCode = createValidationTemplate;
2280
- exports.isAdaptedSchema = isAdaptedSchema;
2281
- exports.validatorFromAdaptedSchema = validatorFromAdaptedSchema;
4310
+ exports.int16 = int16;
4311
+ exports.int16Max = int16Max;
4312
+ exports.int16Min = int16Min;
4313
+ exports.int32 = int32;
4314
+ exports.int32Max = int32Max;
4315
+ exports.int32Min = int32Min;
4316
+ exports.int64 = int64;
4317
+ exports.int64Max = int64Max;
4318
+ exports.int64Min = int64Min;
4319
+ exports.int8 = int8;
4320
+ exports.int8Max = int8Max;
4321
+ exports.int8Min = int8Min;
4322
+ exports.isValidationException = isValidationException;
4323
+ exports.isValueError = isValueError;
4324
+ exports.nullable = nullable;
4325
+ exports.number = number;
4326
+ exports.object = object;
4327
+ exports.omit = omit;
4328
+ exports.optional = optional;
4329
+ exports.parse = parse;
4330
+ exports.parseUnsafe = parseUnsafe;
4331
+ exports.partial = partial;
4332
+ exports.pick = pick;
4333
+ exports.record = record;
4334
+ exports.recursive = recursive;
4335
+ exports.sanitizeJson = sanitizeJson;
4336
+ exports.serialize = serialize;
4337
+ exports.serializeObject = serializeObject;
4338
+ exports.serializeSmallString = serializeSmallString;
4339
+ exports.serializeString = serializeString;
4340
+ exports.serializeUnsafe = serializeUnsafe;
4341
+ exports.string = string;
4342
+ exports.stringEnum = stringEnum;
4343
+ exports.timestamp = timestamp;
4344
+ exports.uint16 = uint16;
4345
+ exports.uint16Max = uint16Max;
4346
+ exports.uint16Min = uint16Min;
4347
+ exports.uint32 = uint32;
4348
+ exports.uint32Max = uint32Max;
4349
+ exports.uint32Min = uint32Min;
4350
+ exports.uint64 = uint64;
4351
+ exports.uint64Max = uint64Max;
4352
+ exports.uint64Min = uint64Min;
4353
+ exports.uint8 = uint8;
4354
+ exports.uint8Max = uint8Max;
4355
+ exports.uint8Min = uint8Min;
4356
+ exports.validate = validate;
4357
+ exports.validateObjectSchema = validateObjectSchema;