@dzeio/schema 0.4.3 → 0.6.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/Schema.js CHANGED
@@ -32,19 +32,27 @@ __export(Schema_exports, {
32
32
  SchemaArray: () => SchemaArray,
33
33
  SchemaBoolean: () => SchemaBoolean,
34
34
  SchemaDate: () => SchemaDate,
35
+ SchemaDefault: () => SchemaDefault,
35
36
  SchemaEnum: () => SchemaEnum,
37
+ SchemaFile: () => SchemaFile,
38
+ SchemaIntersection: () => SchemaIntersection,
36
39
  SchemaItem: () => SchemaItem,
37
40
  SchemaLiteral: () => SchemaLiteral,
41
+ SchemaNever: () => SchemaNever,
42
+ SchemaNull: () => SchemaNull,
38
43
  SchemaNullable: () => SchemaNullable,
44
+ SchemaNullish: () => SchemaNullish,
39
45
  SchemaNumber: () => SchemaNumber,
40
46
  SchemaObject: () => SchemaObject,
41
47
  SchemaRecord: () => SchemaRecord,
42
48
  SchemaString: () => SchemaString,
49
+ SchemaUndefined: () => SchemaUndefined,
43
50
  SchemaUnion: () => SchemaUnion,
51
+ SchemaVoid: () => SchemaVoid,
44
52
  Types: () => Types,
45
53
  default: () => Schema,
46
54
  isNull: () => isNull,
47
- parceable: () => parceable,
55
+ parsable: () => parsable,
48
56
  parseForm: () => parseForm,
49
57
  parseFormData: () => parseFormData,
50
58
  parseQuery: () => parseQuery,
@@ -76,14 +84,18 @@ var _SchemaItem = class _SchemaItem {
76
84
  };
77
85
  }
78
86
  };
79
- /**
80
- * keep public ?
81
- */
82
- this.validations = [];
83
87
  /**
84
88
  * Function calls saved for serialization
85
89
  */
86
90
  this.savedCalls = [];
91
+ /**
92
+ * list of attributes for custom works
93
+ */
94
+ this.attributes = [];
95
+ /**
96
+ * keep public ?
97
+ */
98
+ this.validations = [];
87
99
  /**
88
100
  * Pre process the variable for various reasons
89
101
  *
@@ -101,21 +113,20 @@ var _SchemaItem = class _SchemaItem {
101
113
  * note: the type of the final post-process MUST be valid
102
114
  */
103
115
  this.postProcess = [];
104
- /**
105
- * list of attributes for custom works
106
- */
107
- this.attributes = [];
108
116
  this.invalidError = "the field is invalid";
117
+ // eslint-disable-next-line @typescript-eslint/member-ordering, @typescript-eslint/unbound-method
118
+ this.default = this.defaultValue;
109
119
  if (items && items.length > 0) {
110
120
  this.items = Array.isArray(items) ? items : Array.from(items);
111
121
  }
112
122
  }
113
- defaultValue(value, strict = true) {
123
+ parseJSON() {
114
124
  this.addPreProcess((input) => {
115
- if (strict && isNull(input) || !strict && !input) {
116
- return value;
125
+ try {
126
+ return typeof input === "string" ? JSON.parse(input) : input;
127
+ } catch {
128
+ return input;
117
129
  }
118
- return input;
119
130
  });
120
131
  return this;
121
132
  }
@@ -137,40 +148,57 @@ var _SchemaItem = class _SchemaItem {
137
148
  clone() {
138
149
  return Schema.fromJSON(this.toJSON());
139
150
  }
151
+ defaultValue(value, strict = true) {
152
+ return new SchemaDefault(this, value, strict);
153
+ }
140
154
  nullable() {
141
155
  return new SchemaNullable(this);
142
156
  }
157
+ or(other) {
158
+ return new SchemaUnion(this, other);
159
+ }
160
+ and(other) {
161
+ return new SchemaIntersection(this, other);
162
+ }
163
+ array() {
164
+ return new SchemaArray(this);
165
+ }
166
+ // eslint-disable-next-line complexity
143
167
  parse(input, options) {
144
- var _a;
168
+ var _a, _b;
145
169
  for (const preProcess of this.preProcess) {
146
170
  input = preProcess(input);
147
171
  }
172
+ const result = this.parseSubItems(input, options);
173
+ if ((_a = result.errors) == null ? void 0 : _a.length) {
174
+ return {
175
+ valid: false,
176
+ errors: result.errors
177
+ };
178
+ }
179
+ input = result.input;
148
180
  if (!this.isOfType(input)) {
149
181
  return {
150
182
  valid: false,
151
183
  errors: [{ message: "invalid type" }]
152
184
  };
153
185
  }
154
- const errors = [];
186
+ const validationErrors = [];
155
187
  for (const validation of this.validations) {
156
188
  if (!validation.fn(input)) {
157
- errors.push({
158
- message: (_a = validation.error) != null ? _a : this.invalidError
189
+ validationErrors.push({
190
+ message: (_b = validation.error) != null ? _b : this.invalidError
159
191
  });
160
192
  if (options == null ? void 0 : options.fast) {
161
- return {
162
- valid: false,
163
- object: input,
164
- errors
165
- };
193
+ break;
166
194
  }
167
195
  }
168
196
  }
169
- if (errors.length > 0) {
197
+ if (validationErrors.length > 0) {
170
198
  return {
171
199
  valid: false,
172
200
  object: input,
173
- errors
201
+ errors: validationErrors
174
202
  };
175
203
  }
176
204
  for (const postProcess of this.postProcess) {
@@ -205,12 +233,43 @@ var _SchemaItem = class _SchemaItem {
205
233
  this.postProcess.push(fn);
206
234
  return this;
207
235
  }
236
+ /** Returns the sub values of the input, the schema item to handle them, and the path to it */
237
+ getSubInputs(_input) {
238
+ return [];
239
+ }
240
+ /** Sets the "part" of the input that this sub schema handles. Overrides the input with the result */
241
+ updateSubItemInput(path, input, value) {
242
+ if (path && (0, import_object_util.isObject)(input)) {
243
+ (0, import_object_util.objectSet)(input, path.split("."), value);
244
+ return input;
245
+ } else {
246
+ return value;
247
+ }
248
+ }
249
+ parseSubItems(input, options) {
250
+ const result = { errors: [], input };
251
+ for (const { item, value, path } of this.getSubInputs(result.input)) {
252
+ const parsed = item.parse(value, options);
253
+ if (parsed.valid) {
254
+ result.input = this.updateSubItemInput(path, result.input, parsed.object);
255
+ } else {
256
+ for (const error of parsed.errors) {
257
+ error.field = error.field ? `${path}.${error.field}` : path;
258
+ result.errors.push(error);
259
+ }
260
+ if (options == null ? void 0 : options.fast) {
261
+ break;
262
+ }
263
+ }
264
+ }
265
+ return result;
266
+ }
208
267
  };
209
268
  __decorateClass([
210
- parceable()
211
- ], _SchemaItem.prototype, "defaultValue", 1);
269
+ parsable()
270
+ ], _SchemaItem.prototype, "parseJSON", 1);
212
271
  __decorateClass([
213
- parceable()
272
+ parsable()
214
273
  ], _SchemaItem.prototype, "in", 1);
215
274
  var SchemaItem = _SchemaItem;
216
275
 
@@ -228,77 +287,37 @@ var SchemaArray = class extends SchemaItem {
228
287
  this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
229
288
  return this;
230
289
  }
231
- parse(input, options) {
232
- const { valid, object, errors = [] } = super.parse(input, options);
233
- if (!valid) {
234
- return {
235
- valid,
236
- object,
237
- errors
238
- };
239
- }
240
- const clone = [];
241
- const errs = [];
242
- for (let idx = 0; idx < object.length; idx++) {
243
- const item = object[idx];
244
- const res = this.values.parse(item);
245
- if (res.valid) {
246
- clone.push(res.object);
247
- continue;
248
- }
249
- const errss = res.errors.map((it) => ({
250
- ...it,
251
- field: it.field ? `${idx}.${it.field}` : idx.toString()
252
- }));
253
- if (options == null ? void 0 : options.fast) {
254
- return {
255
- valid: false,
256
- object: clone,
257
- errors: errss
258
- };
259
- }
260
- errs.push(...errss);
261
- }
262
- if (errs.length > 0) {
263
- return {
264
- valid: false,
265
- object: clone,
266
- errors: errs
267
- };
268
- }
269
- return {
270
- valid: true,
271
- object: clone
272
- };
273
- }
274
290
  unwrap() {
275
291
  return this.values;
276
292
  }
277
293
  isOfType(input) {
278
- return Array.isArray(input);
294
+ return Array.isArray(input) && input.every((entry) => this.values.isOfType(entry));
295
+ }
296
+ getSubInputs(input) {
297
+ return Array.isArray(input) ? input.map((entry, index) => ({ item: this.values, value: entry, path: index.toString() })) : [];
279
298
  }
280
299
  };
281
300
  __decorateClass([
282
- parceable()
301
+ parsable()
283
302
  ], SchemaArray.prototype, "unique", 1);
284
303
  __decorateClass([
285
- parceable()
304
+ parsable()
286
305
  ], SchemaArray.prototype, "split", 1);
287
306
 
288
307
  // src/items/boolean.ts
289
308
  var SchemaBoolean = class extends SchemaItem {
290
309
  parseString(trueValue = "true", falseValue = "false") {
291
- this.addPreProcess((it) => {
292
- if (typeof it !== "string") {
293
- return it;
310
+ this.addPreProcess((input) => {
311
+ if (typeof input !== "string") {
312
+ return input;
294
313
  }
295
- if (it === trueValue) {
314
+ if (input === trueValue) {
296
315
  return true;
297
316
  }
298
- if (it === falseValue) {
317
+ if (input === falseValue) {
299
318
  return false;
300
319
  }
301
- return it;
320
+ return input;
302
321
  });
303
322
  return this;
304
323
  }
@@ -307,7 +326,7 @@ var SchemaBoolean = class extends SchemaItem {
307
326
  }
308
327
  };
309
328
  __decorateClass([
310
- parceable()
329
+ parsable()
311
330
  ], SchemaBoolean.prototype, "parseString", 1);
312
331
 
313
332
  // src/items/nullable.ts
@@ -316,36 +335,29 @@ var SchemaNullable = class extends SchemaItem {
316
335
  super([child]);
317
336
  this.child = child;
318
337
  }
319
- falthyAsNull() {
338
+ falsyAsNull() {
320
339
  this.addPreProcess((it) => !it ? void 0 : it);
321
340
  return this;
322
341
  }
323
342
  unwrap() {
324
343
  return this.child;
325
344
  }
326
- parse(input, options) {
327
- const res = super.parse(input, options);
328
- if (!res.valid) {
329
- return this.child.parse(input, options);
330
- }
331
- if (this.isNull(res.object)) {
332
- return {
333
- valid: true,
334
- object: void 0
335
- };
336
- }
337
- return this.child.parse(input, options);
338
- }
339
345
  isOfType(input) {
340
- return this.isNull(input) || this.child.isOfType(input);
346
+ return typeof input === "undefined" || this.child.isOfType(input);
347
+ }
348
+ getSubInputs(input) {
349
+ return [{ value: input, item: this.child, path: void 0 }];
341
350
  }
342
- isNull(value) {
343
- return typeof value === "undefined" || value === null;
351
+ parseSubItems(input, options) {
352
+ if (isNull(input)) {
353
+ return { input: void 0 };
354
+ }
355
+ return super.parseSubItems(input, options);
344
356
  }
345
357
  };
346
358
  __decorateClass([
347
- parceable()
348
- ], SchemaNullable.prototype, "falthyAsNull", 1);
359
+ parsable()
360
+ ], SchemaNullable.prototype, "falsyAsNull", 1);
349
361
 
350
362
  // src/items/object.ts
351
363
  var import_object_util2 = require("@dzeio/object-util");
@@ -355,37 +367,11 @@ var SchemaObject = class extends SchemaItem {
355
367
  this.model = model;
356
368
  this.id = "object";
357
369
  }
358
- parse(input, options) {
359
- const { valid, object, errors = [] } = super.parse(input, options);
360
- if (!valid) {
361
- return {
362
- valid,
363
- object,
364
- errors
365
- };
366
- }
367
- const clone = (0, import_object_util2.objectClone)(object);
368
- const res = {};
369
- (0, import_object_util2.objectLoop)(this.model, (childSchema, key) => {
370
- const childValue = clone[key];
371
- const child = childSchema.parse(childValue);
372
- if (!child.valid) {
373
- errors.push(...child.errors.map((it) => ({
374
- ...it,
375
- field: it.field ? `${key}.${it.field}` : key
376
- })));
377
- }
378
- res[key] = child.object;
379
- return child.valid || !(options == null ? void 0 : options.fast);
380
- });
381
- return {
382
- valid: errors.length === 0,
383
- errors,
384
- object: res
385
- };
386
- }
387
370
  isOfType(input) {
388
- return (0, import_object_util2.isObject)(input);
371
+ return (0, import_object_util2.isObject)(input) && !(0, import_object_util2.objectFind)(this.model, (item, key) => !item.isOfType(input[key]));
372
+ }
373
+ getSubInputs(input) {
374
+ return (0, import_object_util2.isObject)(input) ? (0, import_object_util2.objectMap)(this.model, (schema, key) => ({ item: schema, value: input[key], path: key.toString() })) : [];
389
375
  }
390
376
  };
391
377
 
@@ -393,18 +379,23 @@ var SchemaObject = class extends SchemaItem {
393
379
  function isNull(value) {
394
380
  return typeof value === "undefined" || value === null;
395
381
  }
396
- function parseQuery(model, query, opts) {
382
+ function parseQuery(schema, query, opts) {
397
383
  const record = {};
398
384
  for (const [key, value] of query) {
399
- record[key] = value;
385
+ const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
386
+ const schemaItem = getSchemaItemAtPath(schema, keys);
387
+ const finalValue = schemaItem instanceof SchemaArray ? query.getAll(key) : value;
388
+ (0, import_object_util3.objectSet)(record, keys, finalValue);
400
389
  }
401
- return model.parse(record, opts);
390
+ return schema.parse(record, opts);
402
391
  }
403
- function parseFormData(model, data, opts) {
392
+ function parseFormData(schema, data, opts) {
404
393
  const record = {};
405
394
  for (const [key, value] of data) {
406
- const hasMultipleValues = data.getAll(key).length > 1;
407
- (0, import_object_util3.objectSet)(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), hasMultipleValues ? data.getAll(key) : value);
395
+ const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
396
+ const schemaItem = getSchemaItemAtPath(schema, keys);
397
+ const finalValue = schemaItem instanceof SchemaArray ? data.getAll(key) : value;
398
+ (0, import_object_util3.objectSet)(record, keys, finalValue);
408
399
  }
409
400
  const handleBoolean = (value, keys) => {
410
401
  var _a;
@@ -424,63 +415,84 @@ function parseFormData(model, data, opts) {
424
415
  (0, import_object_util3.objectSet)(record, keys, !!data.get(keys.join(".")));
425
416
  }
426
417
  };
427
- const handleSchemaForBoolean = (modl, keys = []) => {
428
- (0, import_object_util3.objectLoop)(modl, (value, key) => {
418
+ const handleSchemaForBoolean = (model, keys = []) => {
419
+ (0, import_object_util3.objectLoop)(model, (value, key) => {
429
420
  handleBoolean(value, [...keys, key]);
430
421
  });
431
422
  };
432
- if (model instanceof SchemaObject) {
433
- handleSchemaForBoolean(model.model);
423
+ if (schema instanceof SchemaObject) {
424
+ handleSchemaForBoolean(schema.model);
434
425
  }
435
- return model.parse(record, opts);
426
+ return schema.parse(record, opts);
436
427
  }
437
428
  function parseForm(model, form, opts) {
438
429
  return parseFormData(model, new FormData(form), opts);
439
430
  }
431
+ function getSchemaItemAtPath(schema, path) {
432
+ let current = schema;
433
+ const internalPath = (0, import_object_util3.objectClone)(path);
434
+ for (const key of path) {
435
+ if (!current) {
436
+ return void 0;
437
+ }
438
+ internalPath.shift();
439
+ while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
440
+ current = current.unwrap();
441
+ }
442
+ if (current instanceof SchemaObject) {
443
+ current = current.model[key];
444
+ current = getSchemaItemAtPath(current, internalPath);
445
+ }
446
+ }
447
+ while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
448
+ current = current.unwrap();
449
+ }
450
+ return current;
451
+ }
440
452
 
441
453
  // src/items/any.ts
442
454
  var SchemaAny = class extends SchemaItem {
443
- // @ts-expect-error input while not used is necessary for the `override to work`
444
- isOfType(input) {
455
+ isOfType(_input) {
445
456
  return true;
446
457
  }
447
458
  };
448
459
 
449
460
  // src/items/date.ts
450
461
  var SchemaDate = class extends SchemaItem {
451
- parseString(format = "iso8601") {
452
- switch (format) {
453
- case "yy-mm-dd":
454
- case "iso8601": {
455
- this.addPreProcess((it) => {
456
- if (typeof it !== "string") {
457
- return it;
458
- }
459
- const date = new Date(it);
460
- if (isNaN(date.getDate())) {
461
- return it;
462
- }
463
- return date;
464
- });
465
- break;
462
+ parseString(format = "default") {
463
+ this.addPreProcess((input) => {
464
+ if (typeof input !== "string") {
465
+ return input;
466
466
  }
467
- case "jj/mm/yy": {
468
- this.addPreProcess((input) => {
469
- if (typeof input !== "string") {
470
- return input;
467
+ let date;
468
+ switch (format) {
469
+ case "default": {
470
+ date = new Date(input);
471
+ break;
472
+ }
473
+ case "yy-mm-dd":
474
+ case "iso8601": {
475
+ const splitted = input.split("-").map((it) => Number.parseInt(it, 10));
476
+ if (splitted.length !== 3) {
477
+ break;
471
478
  }
479
+ date = new Date(splitted[2], splitted[1] - 1, splitted[0]);
480
+ break;
481
+ }
482
+ case "jj/mm/yy": {
472
483
  const splitted = input.split("/").map((it) => Number.parseInt(it, 10));
473
484
  if (splitted.length !== 3) {
474
- return input;
485
+ break;
475
486
  }
476
- const date = new Date(splitted[2], splitted[1] - 1, splitted[0]);
477
- if (isNaN(date.getDate())) {
478
- return input;
479
- }
480
- return date;
481
- });
487
+ date = new Date(splitted[2], splitted[1] - 1, splitted[0]);
488
+ break;
489
+ }
482
490
  }
483
- }
491
+ if (!date || isNaN(date.getDate())) {
492
+ return input;
493
+ }
494
+ return date;
495
+ });
484
496
  return this;
485
497
  }
486
498
  isOfType(input) {
@@ -488,7 +500,7 @@ var SchemaDate = class extends SchemaItem {
488
500
  }
489
501
  };
490
502
  __decorateClass([
491
- parceable()
503
+ parsable()
492
504
  ], SchemaDate.prototype, "parseString", 1);
493
505
 
494
506
  // src/items/record.ts
@@ -499,39 +511,31 @@ var SchemaRecord = class extends SchemaItem {
499
511
  this.keys = keys;
500
512
  this.values = values;
501
513
  }
502
- parse(input, options) {
503
- const { valid, object, errors = [] } = super.parse(input, options);
504
- if (!valid) {
505
- return {
506
- valid,
507
- object,
508
- errors
509
- };
510
- }
511
- const clone = {};
512
- (0, import_object_util4.objectLoop)(object, (value, key) => {
513
- var _a, _b;
514
- const res1 = this.keys.parse(key);
515
- const res2 = this.values.parse(value);
516
- if (!res1.valid || !res2.valid) {
517
- errors.push(...((_a = res1.errors) != null ? _a : []).concat(...(_b = res2.errors) != null ? _b : []).map((it) => ({
518
- message: it.message,
519
- field: it.field ? `${key}.${it.field}` : key.toString()
520
- })));
521
- } else {
522
- clone[res1.object] = res2.object;
523
- }
524
- return errors.length === 0 || !(options == null ? void 0 : options.fast);
525
- });
526
- return {
527
- valid: errors.length === 0,
528
- errors,
529
- object: clone
530
- };
531
- }
532
514
  isOfType(input) {
533
515
  return (0, import_object_util4.isObject)(input) && Object.prototype.toString.call(input) === "[object Object]";
534
516
  }
517
+ parseSubItems(input, options) {
518
+ const result = { input, errors: [] };
519
+ if ((0, import_object_util4.isObject)(input)) {
520
+ const clone = {};
521
+ (0, import_object_util4.objectLoop)(input, (value, key) => {
522
+ var _a, _b;
523
+ const res1 = this.keys.parse(key);
524
+ const res2 = this.values.parse(value);
525
+ if (!res1.valid || !res2.valid) {
526
+ result.errors.push(...((_a = res1.errors) != null ? _a : []).concat(...(_b = res2.errors) != null ? _b : []).map((it) => ({
527
+ message: it.message,
528
+ field: it.field ? `${key}.${it.field}` : key.toString()
529
+ })));
530
+ } else {
531
+ clone[res1.object] = res2.object;
532
+ }
533
+ return result.errors.length === 0 || !(options == null ? void 0 : options.fast);
534
+ });
535
+ result.input = clone;
536
+ }
537
+ return result;
538
+ }
535
539
  };
536
540
 
537
541
  // src/items/enum.ts
@@ -605,9 +609,7 @@ var SchemaNumber = class extends SchemaItem {
605
609
  return this;
606
610
  }
607
611
  parseString() {
608
- this.addPreProcess(
609
- (input) => typeof input === "string" ? Number.parseFloat(input) : input
610
- );
612
+ this.addPreProcess((input) => typeof input === "string" ? Number.parseFloat(input) : input);
611
613
  return this;
612
614
  }
613
615
  isOfType(input) {
@@ -621,19 +623,19 @@ var SchemaNumber = class extends SchemaItem {
621
623
  }
622
624
  };
623
625
  __decorateClass([
624
- parceable()
626
+ parsable()
625
627
  ], SchemaNumber.prototype, "lte", 1);
626
628
  __decorateClass([
627
- parceable()
629
+ parsable()
628
630
  ], SchemaNumber.prototype, "gte", 1);
629
631
  __decorateClass([
630
- parceable()
632
+ parsable()
631
633
  ], SchemaNumber.prototype, "lt", 1);
632
634
  __decorateClass([
633
- parceable()
635
+ parsable()
634
636
  ], SchemaNumber.prototype, "gt", 1);
635
637
  __decorateClass([
636
- parceable()
638
+ parsable()
637
639
  ], SchemaNumber.prototype, "parseString", 1);
638
640
 
639
641
  // src/items/string.ts
@@ -690,57 +692,167 @@ var SchemaString = class extends SchemaItem {
690
692
  }
691
693
  };
692
694
  __decorateClass([
693
- parceable()
695
+ parsable()
694
696
  ], SchemaString.prototype, "min", 1);
695
697
  __decorateClass([
696
- parceable()
698
+ parsable()
697
699
  ], SchemaString.prototype, "toCasing", 1);
698
700
  __decorateClass([
699
- parceable()
701
+ parsable()
700
702
  ], SchemaString.prototype, "max", 1);
701
703
  __decorateClass([
702
- parceable()
704
+ parsable()
703
705
  ], SchemaString.prototype, "notEmpty", 1);
704
706
  __decorateClass([
705
- parceable()
707
+ parsable()
706
708
  ], SchemaString.prototype, "regex", 1);
707
709
 
708
710
  // src/items/union.ts
709
711
  var SchemaUnion = class extends SchemaItem {
710
712
  constructor(...schemas) {
711
- super();
713
+ super(schemas);
712
714
  this.schemas = schemas;
713
715
  }
714
- parse(input, options) {
715
- const { valid, object, errors = [] } = super.parse(input, options);
716
- if (!valid) {
717
- return {
718
- valid,
719
- object,
720
- errors
721
- };
722
- }
716
+ isOfType(input) {
717
+ return this.schemas.some((it) => it.isOfType(input));
718
+ }
719
+ parseSubItems(input, options) {
720
+ const result = { input, errors: [] };
723
721
  for (const schema of this.schemas) {
724
- const validation = schema.parse(input, options);
722
+ const validation = schema.parse(result.input, options);
725
723
  if (validation.valid) {
726
- return validation;
724
+ return { input: validation.object };
727
725
  } else {
728
- errors.push(...validation.errors);
726
+ result.errors.push(...validation.errors);
729
727
  }
730
728
  }
731
- return {
732
- valid: false,
733
- errors,
734
- object
735
- };
729
+ return result;
730
+ }
731
+ };
732
+
733
+ // src/items/intersection.ts
734
+ var import_object_util5 = require("@dzeio/object-util");
735
+ var SchemaIntersection = class extends SchemaItem {
736
+ constructor(...schemas) {
737
+ super(schemas);
738
+ this.schemas = schemas;
736
739
  }
737
740
  isOfType(input) {
738
- return this.schemas.some((it) => it.isOfType(input));
741
+ return this.schemas.every((it) => it.isOfType(input));
742
+ }
743
+ parseSubItems(input, options) {
744
+ const result = { input, errors: [] };
745
+ for (const schema of this.schemas) {
746
+ const validation = schema.parse(result.input, options);
747
+ if (!validation.valid) {
748
+ result.errors.push(...validation.errors);
749
+ continue;
750
+ }
751
+ if ((0, import_object_util5.isObject)(result.input)) {
752
+ Object.assign(result.input, validation.object);
753
+ } else {
754
+ result.input = validation.object;
755
+ }
756
+ }
757
+ return result;
758
+ }
759
+ };
760
+
761
+ // src/items/default.ts
762
+ var SchemaDefault = class extends SchemaItem {
763
+ constructor(child, defaultVal, strict = true) {
764
+ super([child, defaultVal, strict]);
765
+ this.child = child;
766
+ this.defaultVal = defaultVal;
767
+ this.strict = strict;
768
+ }
769
+ unwrap() {
770
+ return this.child;
771
+ }
772
+ isOfType(input) {
773
+ return this.child.isOfType(input);
774
+ }
775
+ getSubInputs(input) {
776
+ return [{ item: this.child, value: isNull(input) || !this.strict && !input ? this.defaultVal : input, path: void 0 }];
777
+ }
778
+ };
779
+
780
+ // src/items/file.ts
781
+ var SchemaFile = class extends SchemaItem {
782
+ constructor() {
783
+ super();
784
+ this.addPreProcess((input) => this.isOfType(input) && input.size > 0 ? input : void 0);
785
+ }
786
+ extension(ext, error) {
787
+ this.addValidation({ fn: (input) => input.name.endsWith(ext), error });
788
+ return this;
789
+ }
790
+ maxSize(size, error) {
791
+ this.addValidation({ fn: (file) => file.size <= size, error });
792
+ return this;
793
+ }
794
+ isOfType(input) {
795
+ return input instanceof File;
796
+ }
797
+ };
798
+ __decorateClass([
799
+ parsable()
800
+ ], SchemaFile.prototype, "extension", 1);
801
+ __decorateClass([
802
+ parsable()
803
+ ], SchemaFile.prototype, "maxSize", 1);
804
+
805
+ // src/items/tuple.ts
806
+ var SchemaTuple = class extends SchemaItem {
807
+ constructor(...children) {
808
+ super(children);
809
+ this.children = children;
810
+ }
811
+ isOfType(input) {
812
+ if (!Array.isArray(input) || input.length > this.items.length) {
813
+ return false;
814
+ }
815
+ for (const [index, item] of this.children.entries()) {
816
+ if (item instanceof SchemaItem && !item.isOfType(input[index])) {
817
+ return false;
818
+ }
819
+ }
820
+ return true;
821
+ }
822
+ getSubInputs(input) {
823
+ return Array.isArray(input) ? this.children.map((entry, index) => ({ value: input[index], item: entry, path: index.toString() })) : [];
824
+ }
825
+ };
826
+
827
+ // src/items/nullish.ts
828
+ var SchemaUndefined = class extends SchemaItem {
829
+ isOfType(input) {
830
+ return typeof input === "undefined";
831
+ }
832
+ };
833
+ var SchemaNull = class extends SchemaItem {
834
+ isOfType(input) {
835
+ return input === null;
836
+ }
837
+ };
838
+ var SchemaVoid = class extends SchemaItem {
839
+ isOfType(input) {
840
+ return typeof input === "undefined";
841
+ }
842
+ };
843
+ var SchemaNullish = class extends SchemaItem {
844
+ isOfType(input) {
845
+ return isNull(input);
846
+ }
847
+ };
848
+ var SchemaNever = class extends SchemaItem {
849
+ isOfType(_input) {
850
+ return false;
739
851
  }
740
852
  };
741
853
 
742
854
  // src/Schema.ts
743
- function parceable() {
855
+ function parsable() {
744
856
  return (target, propertyKey, descriptor) => {
745
857
  if (!(propertyKey in target)) {
746
858
  throw new Error("property not set in object");
@@ -757,6 +869,7 @@ function parceable() {
757
869
  var Types = {
758
870
  Any: SchemaAny,
759
871
  Array: SchemaArray,
872
+ Tuple: SchemaTuple,
760
873
  Boolean: SchemaBoolean,
761
874
  Date: SchemaDate,
762
875
  Enum: SchemaEnum,
@@ -765,7 +878,13 @@ var Types = {
765
878
  Object: SchemaObject,
766
879
  Record: SchemaRecord,
767
880
  String: SchemaString,
768
- Union: SchemaUnion
881
+ Union: SchemaUnion,
882
+ Intersection: SchemaIntersection,
883
+ Default: SchemaDefault,
884
+ Undefined: SchemaUndefined,
885
+ Void: SchemaVoid,
886
+ Null: SchemaNull,
887
+ Nullish: SchemaNullish
769
888
  };
770
889
  var _Schema = class _Schema extends SchemaObject {
771
890
  static register(module2) {
@@ -815,9 +934,33 @@ var _Schema = class _Schema extends SchemaObject {
815
934
  static string(...inputs) {
816
935
  return new SchemaString(...inputs);
817
936
  }
937
+ static file(...inputs) {
938
+ return new SchemaFile(...inputs);
939
+ }
818
940
  static union(...inputs) {
819
941
  return new SchemaUnion(...inputs);
820
942
  }
943
+ static intersection(...inputs) {
944
+ return new SchemaIntersection(...inputs);
945
+ }
946
+ static tuple(...inputs) {
947
+ return new SchemaTuple(...inputs);
948
+ }
949
+ static undefined(...inputs) {
950
+ return new SchemaUndefined(...inputs);
951
+ }
952
+ static null(...inputs) {
953
+ return new SchemaNull(...inputs);
954
+ }
955
+ static nullish(...inputs) {
956
+ return new SchemaNullish(...inputs);
957
+ }
958
+ static void(...inputs) {
959
+ return new SchemaVoid(...inputs);
960
+ }
961
+ static never(...inputs) {
962
+ return new SchemaNever(...inputs);
963
+ }
821
964
  static fromJSON(json) {
822
965
  var _a, _b, _c, _d, _e, _f;
823
966
  const fn = this.getModule(json.i);
@@ -870,6 +1013,7 @@ var _Schema = class _Schema extends SchemaObject {
870
1013
  };
871
1014
  _Schema.registeredModules = [
872
1015
  SchemaArray,
1016
+ SchemaTuple,
873
1017
  SchemaBoolean,
874
1018
  SchemaDate,
875
1019
  SchemaEnum,
@@ -878,7 +1022,13 @@ _Schema.registeredModules = [
878
1022
  SchemaObject,
879
1023
  SchemaRecord,
880
1024
  SchemaString,
881
- SchemaUnion
1025
+ SchemaUnion,
1026
+ SchemaIntersection,
1027
+ SchemaDefault,
1028
+ SchemaUndefined,
1029
+ SchemaVoid,
1030
+ SchemaNull,
1031
+ SchemaNullish
882
1032
  ];
883
1033
  var Schema = _Schema;
884
1034
  var s = Schema;
@@ -888,18 +1038,26 @@ var s = Schema;
888
1038
  SchemaArray,
889
1039
  SchemaBoolean,
890
1040
  SchemaDate,
1041
+ SchemaDefault,
891
1042
  SchemaEnum,
1043
+ SchemaFile,
1044
+ SchemaIntersection,
892
1045
  SchemaItem,
893
1046
  SchemaLiteral,
1047
+ SchemaNever,
1048
+ SchemaNull,
894
1049
  SchemaNullable,
1050
+ SchemaNullish,
895
1051
  SchemaNumber,
896
1052
  SchemaObject,
897
1053
  SchemaRecord,
898
1054
  SchemaString,
1055
+ SchemaUndefined,
899
1056
  SchemaUnion,
1057
+ SchemaVoid,
900
1058
  Types,
901
1059
  isNull,
902
- parceable,
1060
+ parsable,
903
1061
  parseForm,
904
1062
  parseFormData,
905
1063
  parseQuery,