@arrirpc/schema 0.73.0 → 0.74.0

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