@lucania/schema 2.0.4 → 3.0.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.
@@ -0,0 +1,5 @@
1
+ import { ValidationError } from "./ValidationError";
2
+ import { ValidationPass } from "./ValidationPass";
3
+ export declare class TopLevelValidationError extends ValidationError {
4
+ constructor(pass: ValidationPass);
5
+ }
@@ -4,14 +4,19 @@ import { ValidationError } from "./ValidationError";
4
4
  export declare class ValidationPass {
5
5
  readonly originalSchema: BaseSchemaAny;
6
6
  readonly originalSource: SourceValue<any, boolean, DefaultValue<any>>;
7
+ private _parent;
7
8
  private _path;
8
9
  private _schema;
9
10
  private _source;
10
- constructor(originalSchema: BaseSchemaAny, originalSource: SourceValue<any, boolean, DefaultValue<any>>);
11
+ private _errors;
12
+ constructor(originalSchema: BaseSchemaAny, originalSource: SourceValue<any, boolean, DefaultValue<any>>, parent: ValidationPass | undefined);
11
13
  get path(): string[];
12
14
  get schema(): BaseSchemaAny;
13
15
  get source(): any;
14
16
  next(path: string[], schema: BaseSchemaAny, source: SourceValue<any, boolean, DefaultValue<any>>): ValidationPass;
15
17
  assert(condition: boolean, message: string): void;
16
- getError(message?: string): ValidationError;
18
+ causeError(message?: string): ValidationError;
19
+ addError(error: ValidationError): void;
20
+ get errors(): ValidationError[];
21
+ get topLevel(): boolean;
17
22
  }
package/build/index.js CHANGED
@@ -13,24 +13,34 @@
13
13
  }
14
14
  }
15
15
 
16
+ class TopLevelValidationError extends ValidationError {
17
+ constructor(pass) {
18
+ super(pass, `Encountered ${pass.errors.length} error(s).\n * ${pass.errors.map((error) => error.message).join("\n * ")}`);
19
+ }
20
+ }
21
+
16
22
  class ValidationPass {
17
23
  originalSchema;
18
24
  originalSource;
25
+ _parent;
19
26
  _path;
20
27
  _schema;
21
28
  _source;
22
- constructor(originalSchema, originalSource) {
29
+ _errors;
30
+ constructor(originalSchema, originalSource, parent) {
31
+ this._parent = parent;
23
32
  this.originalSchema = originalSchema;
24
33
  this.originalSource = originalSource;
25
34
  this._path = [];
26
35
  this._schema = originalSchema;
27
36
  this._source = originalSource;
37
+ this._errors = [];
28
38
  }
29
39
  get path() { return this._path; }
30
40
  get schema() { return this._schema; }
31
41
  get source() { return this._source; }
32
42
  next(path, schema, source) {
33
- const nextPass = new ValidationPass(this.originalSchema, this.originalSource);
43
+ const nextPass = new ValidationPass(this.originalSchema, this.originalSource, this);
34
44
  nextPass._path = path;
35
45
  nextPass._source = source;
36
46
  nextPass._schema = schema;
@@ -38,17 +48,31 @@
38
48
  }
39
49
  assert(condition, message) {
40
50
  if (!condition) {
41
- throw this.getError(message);
51
+ throw this.causeError(message);
42
52
  }
43
53
  }
44
- getError(message) {
54
+ causeError(message) {
45
55
  if (message === undefined) {
46
- return this.getError(`Validation failed.`);
56
+ return this.causeError(`Validation failed.`);
47
57
  }
48
58
  else {
49
- return new ValidationError(this, message);
59
+ const error = new ValidationError(this, message);
60
+ this.addError(error);
61
+ if (this._parent !== undefined) {
62
+ this._parent.causeError(error.message);
63
+ }
64
+ return error;
50
65
  }
51
66
  }
67
+ addError(error) {
68
+ this._errors.push(error);
69
+ }
70
+ get errors() {
71
+ return this._errors;
72
+ }
73
+ get topLevel() {
74
+ return this._parent === undefined;
75
+ }
52
76
  }
53
77
 
54
78
  class BaseSchema {
@@ -67,35 +91,49 @@
67
91
  afterAll: []
68
92
  } : additionalValidationPasses;
69
93
  }
70
- validate(source, pass) {
94
+ validate(source, options, pass) {
71
95
  pass = this._ensurePass(source, pass);
72
96
  let result = source;
73
- result = this._executeAdditionalValidator(result, pass, "beforeAll");
74
- result = this._executeAdditionalValidator(result, pass, "beforeDefault");
75
- if (!BaseSchema.isPresent(result) && this.hasDefault()) {
76
- result = this.getDefault(pass);
77
- }
78
- result = this._executeAdditionalValidator(result, pass, "afterDefault");
79
- result = this._executeAdditionalValidator(result, pass, "beforeConversion");
80
- if (BaseSchema.isPresent(result)) {
81
- if (BaseSchema.getType(result) !== this.type) {
82
- result = this.convert(result, pass);
97
+ const presentOptions = {
98
+ collectErrors: options?.collectErrors || false
99
+ };
100
+ try {
101
+ result = this._executeAdditionalValidator(result, pass, "beforeAll");
102
+ result = this._executeAdditionalValidator(result, pass, "beforeDefault");
103
+ if (!BaseSchema.isPresent(result) && this.hasDefault()) {
104
+ result = this.getDefault(pass);
83
105
  }
84
- result = this._validate(result, pass);
85
- result = this._executeAdditionalValidator(result, pass, "afterConversion");
86
- }
87
- else {
88
- if (this._required) {
89
- throw pass.getError(pass.path.length > 0 ? `Missing required value at "${pass.path.join(".")}".` : "Missing required value.");
106
+ result = this._executeAdditionalValidator(result, pass, "afterDefault");
107
+ result = this._executeAdditionalValidator(result, pass, "beforeConversion");
108
+ if (BaseSchema.isPresent(result)) {
109
+ if (BaseSchema.getType(result) !== this.type) {
110
+ result = this.convert(result, pass);
111
+ }
112
+ result = this._validate(result, presentOptions, pass);
113
+ result = this._executeAdditionalValidator(result, pass, "afterConversion");
90
114
  }
91
115
  else {
116
+ if (this._required) {
117
+ throw pass.causeError(pass.path.length > 0 ? `Missing required value at "${pass.path.join(".")}".` : "Missing required value.");
118
+ }
119
+ else {
120
+ result = undefined;
121
+ }
122
+ }
123
+ if (this._required || result !== undefined) {
124
+ result = this._executeAdditionalValidator(result, pass, "afterAll");
125
+ }
126
+ }
127
+ catch (error) {
128
+ if (error instanceof ValidationError && presentOptions.collectErrors) {
92
129
  result = undefined;
93
130
  }
94
- result = this._validate(result, pass);
95
- result = this._executeAdditionalValidator(result, pass, "afterConversion");
131
+ else {
132
+ throw error;
133
+ }
96
134
  }
97
- if (this._required || result !== undefined) {
98
- result = this._executeAdditionalValidator(result, pass, "afterAll");
135
+ if (pass.topLevel && pass.errors.length > 0) {
136
+ throw new TopLevelValidationError(pass);
99
137
  }
100
138
  return result;
101
139
  }
@@ -136,7 +174,7 @@
136
174
  return this._default;
137
175
  }
138
176
  else {
139
- throw pass.getError(`Failed to get default. Invalid default value.`);
177
+ throw pass.causeError(`Failed to get default. Invalid default value.`);
140
178
  }
141
179
  }
142
180
  _getValueDisplay(value) {
@@ -150,7 +188,7 @@
150
188
  };
151
189
  }
152
190
  _getJsonSchemaDescription() {
153
- const pass = new ValidationPass(this, this._default);
191
+ const pass = new ValidationPass(this, this._default, undefined);
154
192
  const descriptionPieces = [];
155
193
  descriptionPieces.push(`A ${this._required ? "required" : "optional"} ${this.type}`);
156
194
  if (this.hasDefault()) {
@@ -173,7 +211,7 @@
173
211
  }
174
212
  _ensurePass(source, pass) {
175
213
  if (pass === undefined) {
176
- pass = new ValidationPass(this, source);
214
+ pass = new ValidationPass(this, source, undefined);
177
215
  }
178
216
  return pass;
179
217
  }
@@ -239,7 +277,7 @@
239
277
 
240
278
  class AnySchema extends BaseSchema {
241
279
  get type() { return "any"; }
242
- _validate(source, pass) {
280
+ _validate(source, options, pass) {
243
281
  return source;
244
282
  }
245
283
  convert(value, pass) {
@@ -260,24 +298,12 @@
260
298
  this.subschema = subschema;
261
299
  }
262
300
  get type() { return "array"; }
263
- // public validate(source: SourceValue<ArraySource<Subschema>, Required, Default>, pass?: ValidationPass):
264
- // ModelValue<ArraySource<Subschema>, ArrayModel<Subschema>, Required, Default> {
265
- // pass = this._ensurePass(source, pass);
266
- // const result: any = super.validate(source, pass);
267
- // if (result !== undefined) {
268
- // for (const key in result) {
269
- // const nestedValue = result[key];
270
- // result[key] = this.subschema.validate(result[key], pass.next([...pass.path, key], this.subschema, nestedValue));
271
- // }
272
- // }
273
- // return result;
274
- // }
275
- _validate(source, pass) {
301
+ _validate(source, options, pass) {
276
302
  const result = [];
277
303
  if (source !== undefined) {
278
304
  for (const key in source) {
279
305
  const nestedValue = source[key];
280
- result[key] = this.subschema.validate(source[key], pass.next([...pass.path, key], this.subschema, nestedValue));
306
+ result[key] = this.subschema.validate(source[key], options, pass.next([...pass.path, key], this.subschema, nestedValue));
281
307
  }
282
308
  }
283
309
  return result;
@@ -290,7 +316,7 @@
290
316
  return [value];
291
317
  }
292
318
  else {
293
- throw pass.getError(`Unable to convert ${BaseSchema.getType(value)} to array.`);
319
+ throw pass.causeError(`Unable to convert ${BaseSchema.getType(value)} to array.`);
294
320
  }
295
321
  }
296
322
  getJsonSchema() {
@@ -310,7 +336,7 @@
310
336
 
311
337
  class BooleanSchema extends BaseSchema {
312
338
  get type() { return "boolean"; }
313
- _validate(source, pass) {
339
+ _validate(source, options, pass) {
314
340
  return source;
315
341
  }
316
342
  convert(value, pass) {
@@ -329,7 +355,7 @@
329
355
  return false;
330
356
  }
331
357
  else {
332
- throw pass.getError(`Unable to convert ${BaseSchema.getType(value)} to boolean.`);
358
+ throw pass.causeError(`Unable to convert ${BaseSchema.getType(value)} to boolean.`);
333
359
  }
334
360
  }
335
361
  clone() {
@@ -346,7 +372,7 @@
346
372
  const StandardDate = globalThis.Date;
347
373
  class DateSchema extends BaseSchema {
348
374
  get type() { return "Date"; }
349
- _validate(source, pass) {
375
+ _validate(source, options, pass) {
350
376
  return source;
351
377
  }
352
378
  convert(value, pass) {
@@ -357,7 +383,7 @@
357
383
  return new StandardDate(value);
358
384
  }
359
385
  else {
360
- throw pass.getError(`Unable to convert ${BaseSchema.getType(value)} to Date.`);
386
+ throw pass.causeError(`Unable to convert ${BaseSchema.getType(value)} to Date.`);
361
387
  }
362
388
  }
363
389
  before(date, message) {
@@ -412,24 +438,12 @@
412
438
  this.subschema = subschema;
413
439
  }
414
440
  get type() { return "object"; }
415
- // public validate(source: SourceValue<DynamicObjectSource<Subschema>, Required, Default>, pass?: ValidationPass):
416
- // ModelValue<DynamicObjectSource<Subschema>, DynamicObjectModel<Subschema>, Required, Default> {
417
- // pass = this._ensurePass(source, pass);
418
- // const result: any = super.validate(source, pass);
419
- // if (result !== undefined) {
420
- // for (const key in result) {
421
- // const nestedValue = result[key];
422
- // result[key] = this.subschema.validate(result[key], pass.next([...pass.path, key], this.subschema, nestedValue));
423
- // }
424
- // }
425
- // return result;
426
- // }
427
- _validate(source, pass) {
441
+ _validate(source, options, pass) {
428
442
  const result = source;
429
443
  if (result !== undefined) {
430
444
  for (const key in result) {
431
445
  const nestedValue = result[key];
432
- result[key] = this.subschema.validate(result[key], pass.next([...pass.path, key], this.subschema, nestedValue));
446
+ result[key] = this.subschema.validate(result[key], options, pass.next([...pass.path, key], this.subschema, nestedValue));
433
447
  }
434
448
  }
435
449
  return result;
@@ -466,11 +480,9 @@
466
480
  this.members = members;
467
481
  }
468
482
  get type() { return "string"; }
469
- _validate(source, pass) {
483
+ _validate(source, options, pass) {
470
484
  const result = source;
471
- if (this._required) {
472
- pass.assert(this.members.includes(result), `"${result}" is not a valid enumeration value (Expected: ${this.members.join(", ")}).`);
473
- }
485
+ pass.assert(this.members.includes(result), `"${result}" is not a valid enumeration value (Expected: ${this.members.join(", ")}).`);
474
486
  return result;
475
487
  }
476
488
  convert(value, pass) {
@@ -490,7 +502,7 @@
490
502
 
491
503
  class NumberSchema extends BaseSchema {
492
504
  get type() { return "number"; }
493
- _validate(source, pass) {
505
+ _validate(source, options, pass) {
494
506
  return source;
495
507
  }
496
508
  convert(value, pass) {
@@ -510,18 +522,18 @@
510
522
  return value.getTime();
511
523
  }
512
524
  else {
513
- throw pass.getError(`Unable to convert ${BaseSchema.getType(value)} to number.`);
525
+ throw pass.causeError(`Unable to convert ${BaseSchema.getType(value)} to number.`);
514
526
  }
515
527
  }
516
528
  min(minimum, message) {
517
529
  return this.custom((model, pass) => {
518
- pass.assert(model >= minimum, message === undefined ? `Number ${model} failed minimum check. (${minimum})` : message);
530
+ pass.assert(model >= minimum, message === undefined ? `Number ${model} failed minimum check. (Must be >= ${minimum})` : message);
519
531
  return model;
520
532
  }, "afterAll");
521
533
  }
522
534
  max(maximum, message) {
523
535
  return this.custom((model, pass) => {
524
- pass.assert(model <= maximum, message === undefined ? `Number ${model} failed maximum check. (${maximum})` : message);
536
+ pass.assert(model <= maximum, message === undefined ? `Number ${model} failed maximum check. (Must be <= ${maximum})` : message);
525
537
  return model;
526
538
  }, "afterAll");
527
539
  }
@@ -552,7 +564,7 @@
552
564
  this.subschema = subschema;
553
565
  }
554
566
  get type() { return "object"; }
555
- _validate(source, pass) {
567
+ _validate(source, options, pass) {
556
568
  const input = source;
557
569
  let output = input;
558
570
  if (typeof input === "object" && input !== null) {
@@ -560,7 +572,7 @@
560
572
  for (const key in this.subschema) {
561
573
  const nestedSchema = this.subschema[key];
562
574
  const nestedValue = input[key];
563
- output[key] = this.subschema[key].validate(input[key], pass.next([...pass.path, key], nestedSchema, nestedValue));
575
+ output[key] = this.subschema[key].validate(input[key], options, pass.next([...pass.path, key], nestedSchema, nestedValue));
564
576
  }
565
577
  }
566
578
  return output;
@@ -631,7 +643,7 @@
631
643
  get type() {
632
644
  return "string";
633
645
  }
634
- _validate(source, pass) {
646
+ _validate(source, options, pass) {
635
647
  let result = source;
636
648
  if (result !== undefined) {
637
649
  let done = false;
@@ -640,7 +652,7 @@
640
652
  const schema = this.schemas[i];
641
653
  try {
642
654
  if (BaseSchema.getType(result) === schema.type) {
643
- result = schema.validate(result, pass);
655
+ result = schema.validate(result, options, pass);
644
656
  done = true;
645
657
  }
646
658
  }
@@ -678,7 +690,7 @@
678
690
  // public required() { return new StringSchema(true, this._default, this._additionalValidationPasses); }
679
691
  // public optional() { return new StringSchema(false, this._default, this._additionalValidationPasses); }
680
692
  // public default<Default extends DefaultValue<StringSource>>(defaultValue: Default) { return new StringSchema(this._required, defaultValue, this._additionalValidationPasses); }
681
- _validate(source, pass) {
693
+ _validate(source, options, pass) {
682
694
  return source;
683
695
  }
684
696
  convert(value, pass) {
@@ -1,9 +1,9 @@
1
1
  import { ValidationPass } from "../error/ValidationPass";
2
- import { DefaultValue, ModelValue } from "../typing/toolbox";
2
+ import { DefaultValue, ModelValue, ValidationOptions } from "../typing/toolbox";
3
3
  import { BaseSchema } from "./BaseSchema";
4
4
  export declare class AnySchema<Required extends boolean, Default extends DefaultValue<any>> extends BaseSchema<any, any, Required, Default> {
5
5
  get type(): string;
6
- protected _validate(source: ModelValue<any, any, Required, Default>, pass: ValidationPass): ModelValue<any, any, Required, Default>;
6
+ protected _validate(source: ModelValue<any, any, Required, Default>, options: ValidationOptions, pass: ValidationPass): ModelValue<any, any, Required, Default>;
7
7
  convert(value: any, pass: ValidationPass): any;
8
8
  getJsonSchema(): object;
9
9
  clone(): AnySchema<Required, Default>;
@@ -1,6 +1,6 @@
1
1
  import { ValidationPass } from "../error/ValidationPass";
2
2
  import { BaseSchemaAny } from "../typing/extended";
3
- import type { AdditionalValidationPasses, DefaultValue, ModelValue, SourceValue } from "../typing/toolbox";
3
+ import type { AdditionalValidationPasses, DefaultValue, ModelValue, SourceValue, ValidationOptions } from "../typing/toolbox";
4
4
  import { BaseSchema } from "./BaseSchema";
5
5
  export type ArraySource<Subschema extends BaseSchemaAny> = (Subschema extends BaseSchema<infer Source, any, infer Required, infer Default> ? (SourceValue<Source, Required, Default>[]) : never);
6
6
  export type ArrayModel<Subschema extends BaseSchemaAny> = (Subschema extends BaseSchema<infer Source, infer Model, infer Required, infer Default> ? (ModelValue<Source, Model, Required, Default>[]) : never);
@@ -8,7 +8,7 @@ export declare class ArraySchema<Subschema extends BaseSchemaAny, Required exten
8
8
  readonly subschema: Subschema;
9
9
  constructor(subschema: Subschema, required: Required, defaultValue: Default, additionalValidationPasses?: AdditionalValidationPasses<ArraySource<Subschema>, ArrayModel<Subschema>>);
10
10
  get type(): string;
11
- protected _validate(source: ModelValue<ArraySource<Subschema>, ArrayModel<Subschema>, Required, Default>, pass: ValidationPass): ModelValue<ArraySource<Subschema>, ArrayModel<Subschema>, Required, Default>;
11
+ protected _validate(source: ModelValue<ArraySource<Subschema>, ArrayModel<Subschema>, Required, Default>, options: ValidationOptions, pass: ValidationPass): ModelValue<ArraySource<Subschema>, ArrayModel<Subschema>, Required, Default>;
12
12
  convert(value: ArraySource<Subschema>, pass: ValidationPass): ArrayModel<Subschema>;
13
13
  getJsonSchema(): object;
14
14
  clone(): ArraySchema<Subschema, Required, Default>;
@@ -1,14 +1,14 @@
1
1
  import { ValidationPass } from "../error/ValidationPass";
2
2
  import { BaseSchemaAny } from "../typing/extended";
3
- import type { AdditionalValidationPasses, AdditionalValidator, AdditionalValidatorAfterType, AdditionalValidatorBeforeType, DefaultValue, EnsureValidator, ModelValue, SourceValue } from "../typing/toolbox";
3
+ import type { AdditionalValidationPasses, AdditionalValidator, AdditionalValidatorAfterType, AdditionalValidatorBeforeType, DefaultValue, EnsureValidator, ModelValue, OptionalValidationOptions, SourceValue, ValidationOptions } from "../typing/toolbox";
4
4
  export declare abstract class BaseSchema<Source, Model, Required extends boolean, Default extends DefaultValue<Source>> {
5
5
  protected readonly _required: Required;
6
6
  protected readonly _default: Default;
7
7
  protected readonly _additionalValidationPasses: AdditionalValidationPasses<Source, Model>;
8
8
  constructor(required: Required, defaultValue: Default, additionalValidationPasses?: AdditionalValidationPasses<Source, Model>);
9
9
  abstract get type(): string;
10
- validate(source: SourceValue<Source, Required, Default>, pass?: ValidationPass): ModelValue<Source, Model, Required, Default>;
11
- protected abstract _validate(source: ModelValue<Source, Model, Required, Default>, pass: ValidationPass): ModelValue<Source, Model, Required, Default>;
10
+ validate(source: SourceValue<Source, Required, Default>, options?: OptionalValidationOptions, pass?: ValidationPass): ModelValue<Source, Model, Required, Default>;
11
+ protected abstract _validate(source: ModelValue<Source, Model, Required, Default>, options: ValidationOptions, pass: ValidationPass): ModelValue<Source, Model, Required, Default>;
12
12
  abstract convert(value: Source, pass: ValidationPass): Model;
13
13
  custom(additionalValidator: AdditionalValidator<SourceValue<Source, Required, Default>>, type: AdditionalValidatorBeforeType): this;
14
14
  custom(additionalValidator: AdditionalValidator<Model>, type: AdditionalValidatorAfterType): this;
@@ -1,10 +1,10 @@
1
1
  import { ValidationPass } from "../error/ValidationPass";
2
- import { DefaultValue, ModelValue } from "../typing/toolbox";
2
+ import { DefaultValue, ModelValue, ValidationOptions } from "../typing/toolbox";
3
3
  import { BaseSchema } from "./BaseSchema";
4
4
  export type BooleanSource = boolean | number | string | null | undefined;
5
5
  export declare class BooleanSchema<Required extends boolean, Default extends DefaultValue<BooleanSource>> extends BaseSchema<BooleanSource, boolean, Required, Default> {
6
6
  get type(): string;
7
- protected _validate(source: ModelValue<BooleanSource, boolean, Required, Default>, pass: ValidationPass): ModelValue<BooleanSource, boolean, Required, Default>;
7
+ protected _validate(source: ModelValue<BooleanSource, boolean, Required, Default>, options: ValidationOptions, pass: ValidationPass): ModelValue<BooleanSource, boolean, Required, Default>;
8
8
  convert(value: BooleanSource, pass: ValidationPass): boolean;
9
9
  clone(): BooleanSchema<Required, Default>;
10
10
  getJsonSchema(): object;
@@ -1,12 +1,12 @@
1
1
  import { ValidationPass } from "../error/ValidationPass";
2
- import { DefaultValue, ModelValue } from "../typing/toolbox";
2
+ import { DefaultValue, ModelValue, ValidationOptions } from "../typing/toolbox";
3
3
  import { BaseSchema } from "./BaseSchema";
4
4
  type StandardDate = globalThis.Date;
5
5
  declare const StandardDate: DateConstructor;
6
6
  export type DateSource = string | number | StandardDate;
7
7
  export declare class DateSchema<Required extends boolean, Default extends DefaultValue<DateSource>> extends BaseSchema<DateSource, StandardDate, Required, Default> {
8
8
  get type(): string;
9
- protected _validate(source: ModelValue<DateSource, Date, Required, Default>, pass: ValidationPass): ModelValue<DateSource, Date, Required, Default>;
9
+ protected _validate(source: ModelValue<DateSource, Date, Required, Default>, options: ValidationOptions, pass: ValidationPass): ModelValue<DateSource, Date, Required, Default>;
10
10
  convert(value: DateSource, pass: ValidationPass): StandardDate;
11
11
  before(date: Date, message?: string): this;
12
12
  after(date: Date, message?: string): this;
@@ -1,6 +1,6 @@
1
1
  import { ValidationPass } from "../error/ValidationPass";
2
2
  import { BaseSchemaAny } from "../typing/extended";
3
- import type { AdditionalValidationPasses, DefaultValue, ModelValue, SourceValue } from "../typing/toolbox";
3
+ import type { AdditionalValidationPasses, DefaultValue, ModelValue, SourceValue, ValidationOptions } from "../typing/toolbox";
4
4
  import { BaseSchema } from "./BaseSchema";
5
5
  export type DynamicObjectSource<Subschema extends BaseSchemaAny> = ({
6
6
  [Key: string]: (Subschema extends BaseSchema<infer Source, any, infer Required, infer Default> ? (SourceValue<Source, Required, Default>) : never);
@@ -12,7 +12,7 @@ export declare class DynamicObjectSchema<Subschema extends BaseSchemaAny, Requir
12
12
  readonly subschema: Subschema;
13
13
  constructor(subschema: Subschema, required: Required, defaultValue: Default, additionalValidationPasses?: AdditionalValidationPasses<DynamicObjectSource<Subschema>, DynamicObjectModel<Subschema>>);
14
14
  get type(): string;
15
- protected _validate(source: ModelValue<DynamicObjectSource<Subschema>, DynamicObjectModel<Subschema>, Required, Default>, pass: ValidationPass): ModelValue<DynamicObjectSource<Subschema>, DynamicObjectModel<Subschema>, Required, Default>;
15
+ protected _validate(source: ModelValue<DynamicObjectSource<Subschema>, DynamicObjectModel<Subschema>, Required, Default>, options: ValidationOptions, pass: ValidationPass): ModelValue<DynamicObjectSource<Subschema>, DynamicObjectModel<Subschema>, Required, Default>;
16
16
  convert(source: DynamicObjectSource<Subschema>, pass: ValidationPass): DynamicObjectModel<Subschema>;
17
17
  getJsonSchema(): object;
18
18
  clone(): DynamicObjectSchema<Subschema, Required, Default>;
@@ -1,11 +1,11 @@
1
1
  import { ValidationPass } from "../error/ValidationPass";
2
- import { AdditionalValidationPasses, DefaultValue, ModelValue } from "../typing/toolbox";
2
+ import { AdditionalValidationPasses, DefaultValue, ModelValue, ValidationOptions } from "../typing/toolbox";
3
3
  import { BaseSchema } from "./BaseSchema";
4
4
  export declare class EnumerationSchema<Members extends string, Required extends boolean, Default extends DefaultValue<Members>> extends BaseSchema<Members, Members, Required, Default> {
5
5
  readonly members: Members[];
6
6
  constructor(members: Members[], required: Required, defaultValue: Default, additionalValidationPasses?: AdditionalValidationPasses<Members, Members>);
7
7
  get type(): string;
8
- protected _validate(source: ModelValue<Members, Members, Required, Default>, pass: ValidationPass): ModelValue<Members, Members, Required, Default>;
8
+ protected _validate(source: ModelValue<Members, Members, Required, Default>, options: ValidationOptions, pass: ValidationPass): ModelValue<Members, Members, Required, Default>;
9
9
  convert(value: Members, pass: ValidationPass): Members;
10
10
  clone(): EnumerationSchema<Members, Required, Default>;
11
11
  getJsonSchema(): object;
@@ -1,10 +1,10 @@
1
1
  import { ValidationPass } from "../error/ValidationPass";
2
- import { DefaultValue, ModelValue } from "../typing/toolbox";
2
+ import { DefaultValue, ModelValue, ValidationOptions } from "../typing/toolbox";
3
3
  import { BaseSchema } from "./BaseSchema";
4
4
  export type NumberSource = number | bigint | string | boolean | null | undefined | Date;
5
5
  export declare class NumberSchema<Required extends boolean, Default extends DefaultValue<NumberSource>> extends BaseSchema<NumberSource, number, Required, Default> {
6
6
  get type(): string;
7
- protected _validate(source: ModelValue<NumberSource, number, Required, Default>, pass: ValidationPass): ModelValue<NumberSource, number, Required, Default>;
7
+ protected _validate(source: ModelValue<NumberSource, number, Required, Default>, options: ValidationOptions, pass: ValidationPass): ModelValue<NumberSource, number, Required, Default>;
8
8
  convert(value: NumberSource, pass: ValidationPass): number;
9
9
  min(minimum: number, message?: string): this;
10
10
  max(maximum: number, message?: string): this;
@@ -1,6 +1,6 @@
1
1
  import { ValidationPass } from "../error/ValidationPass";
2
2
  import { BaseSchemaAny } from "../typing/extended";
3
- import type { AdditionalValidationPasses, DefaultValue, Merge, ModelRequirement, ModelValue, SourceRequirement, SourceValue } from "../typing/toolbox";
3
+ import type { AdditionalValidationPasses, DefaultValue, Merge, ModelRequirement, ModelValue, SourceRequirement, SourceValue, ValidationOptions } from "../typing/toolbox";
4
4
  import { BaseSchema } from "./BaseSchema";
5
5
  export type ObjectSubschema = {
6
6
  [Key: string]: BaseSchemaAny;
@@ -19,7 +19,7 @@ export declare class ObjectSchema<Subschema extends ObjectSubschema, Required ex
19
19
  readonly subschema: Subschema;
20
20
  constructor(subschema: Subschema, required: Required, defaultValue: Default, additionalValidationPasses?: AdditionalValidationPasses<ObjectSource<Subschema>, ObjectModel<Subschema>>);
21
21
  get type(): string;
22
- protected _validate(source: ModelValue<ObjectSource<Subschema>, ObjectModel<Subschema>, Required, Default>, pass: ValidationPass): ModelValue<ObjectSource<Subschema>, ObjectModel<Subschema>, Required, Default>;
22
+ protected _validate(source: ModelValue<ObjectSource<Subschema>, ObjectModel<Subschema>, Required, Default>, options: ValidationOptions, pass: ValidationPass): ModelValue<ObjectSource<Subschema>, ObjectModel<Subschema>, Required, Default>;
23
23
  convert(value: ObjectSource<Subschema>, pass: ValidationPass): ObjectModel<Subschema>;
24
24
  extend<ExtensionSubschema extends ObjectSubschema, ExtensionDefault extends DefaultValue<ObjectSource<ExtensionSubschema>>>(schema: ObjectSchema<ExtensionSubschema, Required, ExtensionDefault>): ObjectSchema<Subschema & ExtensionSubschema, Required, any>;
25
25
  getJsonSchema(): object;
@@ -1,6 +1,6 @@
1
1
  import { ValidationPass } from "../error/ValidationPass";
2
2
  import { BaseSchemaAny } from "../typing/extended";
3
- import { AdditionalValidationPasses, DefaultValue, ModelValue, SourceValue } from "../typing/toolbox";
3
+ import { AdditionalValidationPasses, DefaultValue, ModelValue, SourceValue, ValidationOptions } from "../typing/toolbox";
4
4
  import { BaseSchema } from "./BaseSchema";
5
5
  export type OrSetSchemaSource<MemberSchema extends BaseSchemaAny> = (MemberSchema extends BaseSchema<infer Source, any, infer Required, infer Default> ? SourceValue<Source, Required, Default> : never);
6
6
  export type OrSetSchemaModel<MemberSchema extends BaseSchemaAny> = (MemberSchema extends BaseSchema<infer Source, infer Model, infer Required, infer Default> ? ModelValue<Source, Model, Required, Default> : never);
@@ -8,7 +8,7 @@ export declare class OrSetSchema<MemberSchema extends BaseSchemaAny, Required ex
8
8
  readonly schemas: MemberSchema[];
9
9
  constructor(schemas: MemberSchema[], required: Required, defaultValue: Default, additionalValidationPasses?: AdditionalValidationPasses<OrSetSchemaSource<MemberSchema>, OrSetSchemaModel<MemberSchema>>);
10
10
  get type(): string;
11
- protected _validate(source: ModelValue<OrSetSchemaSource<MemberSchema>, OrSetSchemaModel<MemberSchema>, Required, Default>, pass: ValidationPass): ModelValue<OrSetSchemaSource<MemberSchema>, OrSetSchemaModel<MemberSchema>, Required, Default>;
11
+ protected _validate(source: ModelValue<OrSetSchemaSource<MemberSchema>, OrSetSchemaModel<MemberSchema>, Required, Default>, options: ValidationOptions, pass: ValidationPass): ModelValue<OrSetSchemaSource<MemberSchema>, OrSetSchemaModel<MemberSchema>, Required, Default>;
12
12
  convert(value: OrSetSchemaSource<MemberSchema>, pass: ValidationPass): OrSetSchemaModel<MemberSchema>;
13
13
  clone(): OrSetSchema<MemberSchema, Required, Default>;
14
14
  getJsonSchema(): object;
@@ -1,10 +1,10 @@
1
1
  import { ValidationPass } from "../error/ValidationPass";
2
- import { DefaultValue, ModelValue } from "../typing/toolbox";
2
+ import { DefaultValue, ModelValue, ValidationOptions } from "../typing/toolbox";
3
3
  import { BaseSchema } from "./BaseSchema";
4
4
  export type StringSource = string | number | boolean | null | undefined | Date;
5
5
  export declare class StringSchema<Required extends boolean, Default extends DefaultValue<StringSource>> extends BaseSchema<StringSource, string, Required, Default> {
6
6
  get type(): string;
7
- protected _validate(source: ModelValue<StringSource, string, Required, Default>, pass: ValidationPass): ModelValue<StringSource, string, Required, Default>;
7
+ protected _validate(source: ModelValue<StringSource, string, Required, Default>, options: ValidationOptions, pass: ValidationPass): ModelValue<StringSource, string, Required, Default>;
8
8
  convert(value: StringSource, pass: ValidationPass): string;
9
9
  /**
10
10
  * Trims strings validated by this schema.
@@ -1,6 +1,10 @@
1
1
  import { ValidationPass } from "../error/ValidationPass";
2
2
  import { BaseSchema } from "../schema/BaseSchema";
3
3
  import { BaseSchemaAny } from "./extended";
4
+ export type ValidationOptions = {
5
+ collectErrors: boolean;
6
+ };
7
+ export type OptionalValidationOptions = Partial<ValidationOptions>;
4
8
  export type SourceRequirement<Layout extends BaseSchemaAny> = (Layout extends BaseSchema<any, any, infer Required, infer Default> ? (Required extends true ? (Default extends undefined ? true : false) : (false)) : never);
5
9
  export type SourceValue<Source, Required extends boolean, Default extends DefaultValue<Source>> = (Required extends true ? (undefined extends Default ? (Source) : (Source | undefined)) : Source | undefined);
6
10
  export type ModelRequirement<Layout extends BaseSchemaAny> = (Layout extends BaseSchema<any, any, infer Required, infer Default> ? (Required extends true ? (true) : (Default extends undefined ? false : true)) : never);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lucania/schema",
3
- "version": "2.0.4",
3
+ "version": "3.0.0",
4
4
  "description": "A schema module for compile-time and runtime type checking.",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -37,4 +37,4 @@
37
37
  "tslib": "^2.6.3",
38
38
  "typescript": "^5.4.5"
39
39
  }
40
- }
40
+ }