@dzeio/schema 0.5.0 → 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.d.mts CHANGED
@@ -4,13 +4,6 @@ declare abstract class SchemaItem<Output = any, Input = Output> implements Stand
4
4
  readonly _output: Output;
5
5
  readonly _input: Input;
6
6
  '~standard': StandardSchemaV1.Props<Input, Output>;
7
- /**
8
- * keep public ?
9
- */
10
- protected readonly validations: Array<{
11
- fn: (input: Output) => boolean;
12
- error?: string | undefined;
13
- }>;
14
7
  /**
15
8
  * Function calls saved for serialization
16
9
  */
@@ -18,6 +11,17 @@ declare abstract class SchemaItem<Output = any, Input = Output> implements Stand
18
11
  name: string;
19
12
  args: Array<string> | IArguments | undefined;
20
13
  }>;
14
+ /**
15
+ * list of attributes for custom works
16
+ */
17
+ readonly attributes: Array<string>;
18
+ /**
19
+ * keep public ?
20
+ */
21
+ protected readonly validations: Array<{
22
+ fn: (input: Output) => boolean;
23
+ error?: string | undefined;
24
+ }>;
21
25
  /**
22
26
  * Pre process the variable for various reasons
23
27
  *
@@ -34,13 +38,10 @@ declare abstract class SchemaItem<Output = any, Input = Output> implements Stand
34
38
  * note: the type of the final post-process MUST be valid
35
39
  */
36
40
  protected readonly postProcess: Array<(input: Output) => Output>;
37
- /**
38
- * list of attributes for custom works
39
- */
40
- readonly attributes: Array<string>;
41
41
  protected readonly items?: Array<unknown>;
42
42
  private invalidError;
43
43
  constructor(items?: Array<unknown> | IArguments);
44
+ parseJSON(): this;
44
45
  /**
45
46
  * make sure the value is one of the `values`
46
47
  * @param values the values the item MUST contains
@@ -64,6 +65,14 @@ declare abstract class SchemaItem<Output = any, Input = Output> implements Stand
64
65
  * es2: s.nullable(s.string()) returns s.string()
65
66
  */
66
67
  unwrap?(): SchemaItem;
68
+ parse(input: unknown, options?: ValidationOptions): ValidationResult<Output>;
69
+ toJSON(): SchemaJSON;
70
+ addValidation(fn: ((input: Output) => boolean) | {
71
+ fn: (input: Output) => boolean;
72
+ error?: string;
73
+ }, error?: string): this;
74
+ addPreProcess(fn: (input: unknown) => Input | unknown): this;
75
+ addPostProcess(fn: (input: Output) => Output): this;
67
76
  /** Returns the sub values of the input, the schema item to handle them, and the path to it */
68
77
  protected getSubInputs(_input: unknown): Array<{
69
78
  item: SchemaItem;
@@ -76,21 +85,12 @@ declare abstract class SchemaItem<Output = any, Input = Output> implements Stand
76
85
  input: unknown;
77
86
  errors?: Array<ValidationError>;
78
87
  };
79
- parse(input: unknown, options?: ValidationOptions): ValidationResult<Output>;
80
- toJSON(): SchemaJSON;
81
- addValidation(fn: ((input: Output) => boolean) | {
82
- fn: (input: Output) => boolean;
83
- error?: string;
84
- }, error?: string): this;
85
- addPreProcess(fn: (input: unknown) => Input | unknown): this;
86
- addPostProcess(fn: (input: Output) => Output): this;
87
- parseJSON(): this;
88
88
  abstract isOfType(input: unknown): input is Output;
89
89
  }
90
90
 
91
- type SchemaInfer<Type extends SchemaItem> = Type["_output"]
91
+ type SchemaInfer<Type extends SchemaItem> = Type['_output']
92
92
  type Infer<Type extends SchemaItem> = SchemaInfer<Type>
93
- type SchemaInputInfer<Type extends SchemaItem> = Type["_input"]
93
+ type SchemaInputInfer<Type extends SchemaItem> = Type['_input']
94
94
  type InputInfer<Type extends SchemaItem> = SchemaInputInfer<Type>
95
95
 
96
96
  interface ValidationOptions {
@@ -173,11 +173,11 @@ declare class SchemaRecord<Keys extends SchemaItem<string | number | symbol>, Va
173
173
  private readonly keys;
174
174
  private readonly values;
175
175
  constructor(keys: Keys, values: Values);
176
+ isOfType(input: unknown): input is Record<SchemaInfer<Keys>, SchemaInfer<Values>>;
176
177
  protected parseSubItems(input: unknown, options?: ValidationOptions): {
177
178
  input: unknown;
178
- errors?: ValidationError[];
179
+ errors?: Array<ValidationError>;
179
180
  };
180
- isOfType(input: unknown): input is Record<SchemaInfer<Keys>, SchemaInfer<Values>>;
181
181
  }
182
182
 
183
183
  declare class SchemaArray<Child extends SchemaItem, Output = SchemaInfer<Child>, Input = SchemaInputInfer<Child>> extends SchemaItem<Array<Output>, Array<Input>> {
@@ -192,13 +192,13 @@ declare class SchemaArray<Child extends SchemaItem, Output = SchemaInfer<Child>,
192
192
  * @param [separator] default:`,` the separator to use
193
193
  */
194
194
  split(separator?: string): this;
195
+ unwrap(): Child;
196
+ isOfType(input: unknown): input is Array<Output>;
195
197
  protected getSubInputs(input: unknown): Array<{
196
198
  item: SchemaItem;
197
199
  value: unknown;
198
200
  path: string | undefined;
199
201
  }>;
200
- unwrap(): Child;
201
- isOfType(input: unknown): input is Array<Output>;
202
202
  }
203
203
 
204
204
  declare class SchemaBoolean extends SchemaItem<boolean> {
@@ -232,6 +232,7 @@ declare class SchemaNullable<Child extends SchemaItem, Output = SchemaInfer<Chil
232
232
  constructor(child: Child);
233
233
  falsyAsNull(): this;
234
234
  unwrap(): Child;
235
+ isOfType(input: unknown): input is Output | undefined;
235
236
  protected getSubInputs(input: unknown): Array<{
236
237
  item: SchemaItem;
237
238
  value: unknown;
@@ -241,7 +242,6 @@ declare class SchemaNullable<Child extends SchemaItem, Output = SchemaInfer<Chil
241
242
  input: unknown;
242
243
  errors?: Array<ValidationError>;
243
244
  };
244
- isOfType(input: unknown): input is Output | undefined;
245
245
  }
246
246
 
247
247
  declare class SchemaNumber extends SchemaItem<number> {
@@ -285,12 +285,12 @@ declare class SchemaObject<T extends Record<string, SchemaItem> = any> extends S
285
285
  readonly model: T;
286
286
  id: string;
287
287
  constructor(model: T);
288
+ isOfType(input: unknown): input is ModelInfer<T>;
288
289
  protected getSubInputs(input: unknown): Array<{
289
290
  item: SchemaItem;
290
291
  value: unknown;
291
292
  path: string | undefined;
292
293
  }>;
293
- isOfType(input: unknown): input is ModelInfer<T>;
294
294
  }
295
295
 
296
296
  declare class SchemaString extends SchemaItem<string> {
@@ -331,11 +331,11 @@ declare class SchemaString extends SchemaItem<string> {
331
331
  declare class SchemaUnion<Children extends Array<SchemaItem>, Outputs = SchemaInfer<Children[number]>, Inputs = SchemaInputInfer<Children[number]>> extends SchemaItem<Outputs, Inputs> {
332
332
  private readonly schemas;
333
333
  constructor(...schemas: Children);
334
+ isOfType(input: unknown): input is Outputs;
334
335
  protected parseSubItems(input: unknown, options?: ValidationOptions): {
335
336
  input: unknown;
336
337
  errors?: Array<ValidationError>;
337
338
  };
338
- isOfType(input: unknown): input is Outputs;
339
339
  }
340
340
 
341
341
  type Prettify<T> = T extends unknown ? {
@@ -345,11 +345,11 @@ type UnionToIntersection<U> = (U extends U ? (x: U) => void : never) extends ((x
345
345
  declare class SchemaIntersection<Children extends Array<SchemaItem>, Outputs = Prettify<UnionToIntersection<SchemaInfer<Children[number]>>>, Inputs = Prettify<UnionToIntersection<SchemaInfer<Children[number]>>>> extends SchemaItem<Outputs, Inputs> {
346
346
  private readonly schemas;
347
347
  constructor(...schemas: Children);
348
+ isOfType(input: unknown): input is Outputs;
348
349
  protected parseSubItems(input: unknown, options?: ValidationOptions): {
349
350
  input: unknown;
350
351
  errors?: Array<ValidationError>;
351
352
  };
352
- isOfType(input: unknown): input is Outputs;
353
353
  }
354
354
 
355
355
  declare class SchemaDefault<Child extends SchemaItem, Output = SchemaInfer<Child>, Input = SchemaInputInfer<Child>> extends SchemaItem<Output, Input | null | undefined> {
@@ -358,12 +358,12 @@ declare class SchemaDefault<Child extends SchemaItem, Output = SchemaInfer<Child
358
358
  private readonly strict;
359
359
  constructor(child: Child, defaultVal: Output, strict?: boolean);
360
360
  unwrap(): Child;
361
+ isOfType(input: unknown): input is Output;
361
362
  protected getSubInputs(input: unknown): Array<{
362
363
  item: SchemaItem;
363
364
  value: unknown;
364
365
  path: string | undefined;
365
366
  }>;
366
- isOfType(input: unknown): input is Output;
367
367
  }
368
368
 
369
369
  declare class SchemaFile extends SchemaItem<File> {
@@ -373,17 +373,17 @@ declare class SchemaFile extends SchemaItem<File> {
373
373
  isOfType(input: unknown): input is File;
374
374
  }
375
375
 
376
- type TupleToOutputs<Tuple extends Array<SchemaItem>, Result extends any[] = []> = number extends Tuple["length"] ? Array<SchemaInfer<Tuple[number]>> : Tuple[Result["length"]] extends undefined ? Result : TupleToOutputs<Tuple, [...Result, SchemaInfer<Tuple[Result["length"]]>]>;
377
- type TupleToInputs<Tuple extends Array<SchemaItem>, Result extends any[] = []> = number extends Tuple["length"] ? Array<SchemaInputInfer<Tuple[number]>> : Tuple[Result["length"]] extends undefined ? Result : TupleToInputs<Tuple, [...Result, SchemaInputInfer<Tuple[Result["length"]]>]>;
376
+ type TupleToOutputs<Tuple extends Array<SchemaItem>, Result extends Array<any> = []> = number extends Tuple['length'] ? Array<SchemaInfer<Tuple[number]>> : Tuple[Result['length']] extends undefined ? Result : TupleToOutputs<Tuple, [...Result, SchemaInfer<Tuple[Result['length']]>]>;
377
+ type TupleToInputs<Tuple extends Array<SchemaItem>, Result extends Array<any> = []> = number extends Tuple['length'] ? Array<SchemaInputInfer<Tuple[number]>> : Tuple[Result['length']] extends undefined ? Result : TupleToInputs<Tuple, [...Result, SchemaInputInfer<Tuple[Result['length']]>]>;
378
378
  declare class SchemaTuple<Children extends Array<SchemaItem>, Outputs = TupleToOutputs<Children>, Inputs = TupleToInputs<Children>> extends SchemaItem<Outputs, Inputs> {
379
379
  private readonly children;
380
380
  constructor(...children: Children);
381
+ isOfType(input: unknown): input is Outputs;
381
382
  protected getSubInputs(input: unknown): Array<{
382
383
  item: SchemaItem;
383
384
  value: unknown;
384
385
  path: string | undefined;
385
386
  }>;
386
- isOfType(input: unknown): input is Outputs;
387
387
  }
388
388
 
389
389
  declare class SchemaUndefined extends SchemaItem<undefined> {
@@ -403,7 +403,10 @@ declare class SchemaNever extends SchemaItem<never> {
403
403
  }
404
404
 
405
405
  declare function isNull(value: unknown): value is undefined | null;
406
- declare function parseQuery<T extends SchemaItem>(model: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
406
+ declare function parseQuery<T extends SchemaItem>(schema: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
407
+ /**
408
+ * (Note: be carefull : missing key/value associated to schema "s.boolean().nullable" or "s.boolean()" will be transform into false)
409
+ */
407
410
  declare function parseFormData<T extends SchemaItem>(schema: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
408
411
  declare function parseForm<T extends SchemaItem>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
409
412
 
@@ -457,8 +460,8 @@ declare class Schema<T extends Record<string, SchemaItem> = Record<string, Schem
457
460
  static string(...inputs: ConstructorParameters<typeof SchemaString>): SchemaString;
458
461
  static file(...inputs: ConstructorParameters<typeof SchemaFile>): SchemaFile;
459
462
  static union<Type extends Array<SchemaItem>>(...inputs: ConstructorParameters<typeof SchemaUnion<Type>>): SchemaUnion<Type>;
460
- static intersection<Type extends SchemaItem[]>(...inputs: ConstructorParameters<typeof SchemaIntersection<Type>>): SchemaIntersection<Type>;
461
- static tuple<Items extends SchemaItem[]>(...inputs: ConstructorParameters<typeof SchemaTuple<Items>>): SchemaTuple<Items>;
463
+ static intersection<Type extends Array<SchemaItem>>(...inputs: ConstructorParameters<typeof SchemaIntersection<Type>>): SchemaIntersection<Type>;
464
+ static tuple<Items extends Array<SchemaItem>>(...inputs: ConstructorParameters<typeof SchemaTuple<Items>>): SchemaTuple<Items>;
462
465
  static undefined(...inputs: ConstructorParameters<typeof SchemaUndefined>): SchemaUndefined;
463
466
  static null(...inputs: ConstructorParameters<typeof SchemaNull>): SchemaNull;
464
467
  static nullish(...inputs: ConstructorParameters<typeof SchemaNullish>): SchemaNullish;
package/dist/Schema.d.ts CHANGED
@@ -4,13 +4,6 @@ declare abstract class SchemaItem<Output = any, Input = Output> implements Stand
4
4
  readonly _output: Output;
5
5
  readonly _input: Input;
6
6
  '~standard': StandardSchemaV1.Props<Input, Output>;
7
- /**
8
- * keep public ?
9
- */
10
- protected readonly validations: Array<{
11
- fn: (input: Output) => boolean;
12
- error?: string | undefined;
13
- }>;
14
7
  /**
15
8
  * Function calls saved for serialization
16
9
  */
@@ -18,6 +11,17 @@ declare abstract class SchemaItem<Output = any, Input = Output> implements Stand
18
11
  name: string;
19
12
  args: Array<string> | IArguments | undefined;
20
13
  }>;
14
+ /**
15
+ * list of attributes for custom works
16
+ */
17
+ readonly attributes: Array<string>;
18
+ /**
19
+ * keep public ?
20
+ */
21
+ protected readonly validations: Array<{
22
+ fn: (input: Output) => boolean;
23
+ error?: string | undefined;
24
+ }>;
21
25
  /**
22
26
  * Pre process the variable for various reasons
23
27
  *
@@ -34,13 +38,10 @@ declare abstract class SchemaItem<Output = any, Input = Output> implements Stand
34
38
  * note: the type of the final post-process MUST be valid
35
39
  */
36
40
  protected readonly postProcess: Array<(input: Output) => Output>;
37
- /**
38
- * list of attributes for custom works
39
- */
40
- readonly attributes: Array<string>;
41
41
  protected readonly items?: Array<unknown>;
42
42
  private invalidError;
43
43
  constructor(items?: Array<unknown> | IArguments);
44
+ parseJSON(): this;
44
45
  /**
45
46
  * make sure the value is one of the `values`
46
47
  * @param values the values the item MUST contains
@@ -64,6 +65,14 @@ declare abstract class SchemaItem<Output = any, Input = Output> implements Stand
64
65
  * es2: s.nullable(s.string()) returns s.string()
65
66
  */
66
67
  unwrap?(): SchemaItem;
68
+ parse(input: unknown, options?: ValidationOptions): ValidationResult<Output>;
69
+ toJSON(): SchemaJSON;
70
+ addValidation(fn: ((input: Output) => boolean) | {
71
+ fn: (input: Output) => boolean;
72
+ error?: string;
73
+ }, error?: string): this;
74
+ addPreProcess(fn: (input: unknown) => Input | unknown): this;
75
+ addPostProcess(fn: (input: Output) => Output): this;
67
76
  /** Returns the sub values of the input, the schema item to handle them, and the path to it */
68
77
  protected getSubInputs(_input: unknown): Array<{
69
78
  item: SchemaItem;
@@ -76,21 +85,12 @@ declare abstract class SchemaItem<Output = any, Input = Output> implements Stand
76
85
  input: unknown;
77
86
  errors?: Array<ValidationError>;
78
87
  };
79
- parse(input: unknown, options?: ValidationOptions): ValidationResult<Output>;
80
- toJSON(): SchemaJSON;
81
- addValidation(fn: ((input: Output) => boolean) | {
82
- fn: (input: Output) => boolean;
83
- error?: string;
84
- }, error?: string): this;
85
- addPreProcess(fn: (input: unknown) => Input | unknown): this;
86
- addPostProcess(fn: (input: Output) => Output): this;
87
- parseJSON(): this;
88
88
  abstract isOfType(input: unknown): input is Output;
89
89
  }
90
90
 
91
- type SchemaInfer<Type extends SchemaItem> = Type["_output"]
91
+ type SchemaInfer<Type extends SchemaItem> = Type['_output']
92
92
  type Infer<Type extends SchemaItem> = SchemaInfer<Type>
93
- type SchemaInputInfer<Type extends SchemaItem> = Type["_input"]
93
+ type SchemaInputInfer<Type extends SchemaItem> = Type['_input']
94
94
  type InputInfer<Type extends SchemaItem> = SchemaInputInfer<Type>
95
95
 
96
96
  interface ValidationOptions {
@@ -173,11 +173,11 @@ declare class SchemaRecord<Keys extends SchemaItem<string | number | symbol>, Va
173
173
  private readonly keys;
174
174
  private readonly values;
175
175
  constructor(keys: Keys, values: Values);
176
+ isOfType(input: unknown): input is Record<SchemaInfer<Keys>, SchemaInfer<Values>>;
176
177
  protected parseSubItems(input: unknown, options?: ValidationOptions): {
177
178
  input: unknown;
178
- errors?: ValidationError[];
179
+ errors?: Array<ValidationError>;
179
180
  };
180
- isOfType(input: unknown): input is Record<SchemaInfer<Keys>, SchemaInfer<Values>>;
181
181
  }
182
182
 
183
183
  declare class SchemaArray<Child extends SchemaItem, Output = SchemaInfer<Child>, Input = SchemaInputInfer<Child>> extends SchemaItem<Array<Output>, Array<Input>> {
@@ -192,13 +192,13 @@ declare class SchemaArray<Child extends SchemaItem, Output = SchemaInfer<Child>,
192
192
  * @param [separator] default:`,` the separator to use
193
193
  */
194
194
  split(separator?: string): this;
195
+ unwrap(): Child;
196
+ isOfType(input: unknown): input is Array<Output>;
195
197
  protected getSubInputs(input: unknown): Array<{
196
198
  item: SchemaItem;
197
199
  value: unknown;
198
200
  path: string | undefined;
199
201
  }>;
200
- unwrap(): Child;
201
- isOfType(input: unknown): input is Array<Output>;
202
202
  }
203
203
 
204
204
  declare class SchemaBoolean extends SchemaItem<boolean> {
@@ -232,6 +232,7 @@ declare class SchemaNullable<Child extends SchemaItem, Output = SchemaInfer<Chil
232
232
  constructor(child: Child);
233
233
  falsyAsNull(): this;
234
234
  unwrap(): Child;
235
+ isOfType(input: unknown): input is Output | undefined;
235
236
  protected getSubInputs(input: unknown): Array<{
236
237
  item: SchemaItem;
237
238
  value: unknown;
@@ -241,7 +242,6 @@ declare class SchemaNullable<Child extends SchemaItem, Output = SchemaInfer<Chil
241
242
  input: unknown;
242
243
  errors?: Array<ValidationError>;
243
244
  };
244
- isOfType(input: unknown): input is Output | undefined;
245
245
  }
246
246
 
247
247
  declare class SchemaNumber extends SchemaItem<number> {
@@ -285,12 +285,12 @@ declare class SchemaObject<T extends Record<string, SchemaItem> = any> extends S
285
285
  readonly model: T;
286
286
  id: string;
287
287
  constructor(model: T);
288
+ isOfType(input: unknown): input is ModelInfer<T>;
288
289
  protected getSubInputs(input: unknown): Array<{
289
290
  item: SchemaItem;
290
291
  value: unknown;
291
292
  path: string | undefined;
292
293
  }>;
293
- isOfType(input: unknown): input is ModelInfer<T>;
294
294
  }
295
295
 
296
296
  declare class SchemaString extends SchemaItem<string> {
@@ -331,11 +331,11 @@ declare class SchemaString extends SchemaItem<string> {
331
331
  declare class SchemaUnion<Children extends Array<SchemaItem>, Outputs = SchemaInfer<Children[number]>, Inputs = SchemaInputInfer<Children[number]>> extends SchemaItem<Outputs, Inputs> {
332
332
  private readonly schemas;
333
333
  constructor(...schemas: Children);
334
+ isOfType(input: unknown): input is Outputs;
334
335
  protected parseSubItems(input: unknown, options?: ValidationOptions): {
335
336
  input: unknown;
336
337
  errors?: Array<ValidationError>;
337
338
  };
338
- isOfType(input: unknown): input is Outputs;
339
339
  }
340
340
 
341
341
  type Prettify<T> = T extends unknown ? {
@@ -345,11 +345,11 @@ type UnionToIntersection<U> = (U extends U ? (x: U) => void : never) extends ((x
345
345
  declare class SchemaIntersection<Children extends Array<SchemaItem>, Outputs = Prettify<UnionToIntersection<SchemaInfer<Children[number]>>>, Inputs = Prettify<UnionToIntersection<SchemaInfer<Children[number]>>>> extends SchemaItem<Outputs, Inputs> {
346
346
  private readonly schemas;
347
347
  constructor(...schemas: Children);
348
+ isOfType(input: unknown): input is Outputs;
348
349
  protected parseSubItems(input: unknown, options?: ValidationOptions): {
349
350
  input: unknown;
350
351
  errors?: Array<ValidationError>;
351
352
  };
352
- isOfType(input: unknown): input is Outputs;
353
353
  }
354
354
 
355
355
  declare class SchemaDefault<Child extends SchemaItem, Output = SchemaInfer<Child>, Input = SchemaInputInfer<Child>> extends SchemaItem<Output, Input | null | undefined> {
@@ -358,12 +358,12 @@ declare class SchemaDefault<Child extends SchemaItem, Output = SchemaInfer<Child
358
358
  private readonly strict;
359
359
  constructor(child: Child, defaultVal: Output, strict?: boolean);
360
360
  unwrap(): Child;
361
+ isOfType(input: unknown): input is Output;
361
362
  protected getSubInputs(input: unknown): Array<{
362
363
  item: SchemaItem;
363
364
  value: unknown;
364
365
  path: string | undefined;
365
366
  }>;
366
- isOfType(input: unknown): input is Output;
367
367
  }
368
368
 
369
369
  declare class SchemaFile extends SchemaItem<File> {
@@ -373,17 +373,17 @@ declare class SchemaFile extends SchemaItem<File> {
373
373
  isOfType(input: unknown): input is File;
374
374
  }
375
375
 
376
- type TupleToOutputs<Tuple extends Array<SchemaItem>, Result extends any[] = []> = number extends Tuple["length"] ? Array<SchemaInfer<Tuple[number]>> : Tuple[Result["length"]] extends undefined ? Result : TupleToOutputs<Tuple, [...Result, SchemaInfer<Tuple[Result["length"]]>]>;
377
- type TupleToInputs<Tuple extends Array<SchemaItem>, Result extends any[] = []> = number extends Tuple["length"] ? Array<SchemaInputInfer<Tuple[number]>> : Tuple[Result["length"]] extends undefined ? Result : TupleToInputs<Tuple, [...Result, SchemaInputInfer<Tuple[Result["length"]]>]>;
376
+ type TupleToOutputs<Tuple extends Array<SchemaItem>, Result extends Array<any> = []> = number extends Tuple['length'] ? Array<SchemaInfer<Tuple[number]>> : Tuple[Result['length']] extends undefined ? Result : TupleToOutputs<Tuple, [...Result, SchemaInfer<Tuple[Result['length']]>]>;
377
+ type TupleToInputs<Tuple extends Array<SchemaItem>, Result extends Array<any> = []> = number extends Tuple['length'] ? Array<SchemaInputInfer<Tuple[number]>> : Tuple[Result['length']] extends undefined ? Result : TupleToInputs<Tuple, [...Result, SchemaInputInfer<Tuple[Result['length']]>]>;
378
378
  declare class SchemaTuple<Children extends Array<SchemaItem>, Outputs = TupleToOutputs<Children>, Inputs = TupleToInputs<Children>> extends SchemaItem<Outputs, Inputs> {
379
379
  private readonly children;
380
380
  constructor(...children: Children);
381
+ isOfType(input: unknown): input is Outputs;
381
382
  protected getSubInputs(input: unknown): Array<{
382
383
  item: SchemaItem;
383
384
  value: unknown;
384
385
  path: string | undefined;
385
386
  }>;
386
- isOfType(input: unknown): input is Outputs;
387
387
  }
388
388
 
389
389
  declare class SchemaUndefined extends SchemaItem<undefined> {
@@ -403,7 +403,10 @@ declare class SchemaNever extends SchemaItem<never> {
403
403
  }
404
404
 
405
405
  declare function isNull(value: unknown): value is undefined | null;
406
- declare function parseQuery<T extends SchemaItem>(model: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
406
+ declare function parseQuery<T extends SchemaItem>(schema: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
407
+ /**
408
+ * (Note: be carefull : missing key/value associated to schema "s.boolean().nullable" or "s.boolean()" will be transform into false)
409
+ */
407
410
  declare function parseFormData<T extends SchemaItem>(schema: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
408
411
  declare function parseForm<T extends SchemaItem>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
409
412
 
@@ -457,8 +460,8 @@ declare class Schema<T extends Record<string, SchemaItem> = Record<string, Schem
457
460
  static string(...inputs: ConstructorParameters<typeof SchemaString>): SchemaString;
458
461
  static file(...inputs: ConstructorParameters<typeof SchemaFile>): SchemaFile;
459
462
  static union<Type extends Array<SchemaItem>>(...inputs: ConstructorParameters<typeof SchemaUnion<Type>>): SchemaUnion<Type>;
460
- static intersection<Type extends SchemaItem[]>(...inputs: ConstructorParameters<typeof SchemaIntersection<Type>>): SchemaIntersection<Type>;
461
- static tuple<Items extends SchemaItem[]>(...inputs: ConstructorParameters<typeof SchemaTuple<Items>>): SchemaTuple<Items>;
463
+ static intersection<Type extends Array<SchemaItem>>(...inputs: ConstructorParameters<typeof SchemaIntersection<Type>>): SchemaIntersection<Type>;
464
+ static tuple<Items extends Array<SchemaItem>>(...inputs: ConstructorParameters<typeof SchemaTuple<Items>>): SchemaTuple<Items>;
462
465
  static undefined(...inputs: ConstructorParameters<typeof SchemaUndefined>): SchemaUndefined;
463
466
  static null(...inputs: ConstructorParameters<typeof SchemaNull>): SchemaNull;
464
467
  static nullish(...inputs: ConstructorParameters<typeof SchemaNullish>): SchemaNullish;
package/dist/Schema.js CHANGED
@@ -84,14 +84,18 @@ var _SchemaItem = class _SchemaItem {
84
84
  };
85
85
  }
86
86
  };
87
- /**
88
- * keep public ?
89
- */
90
- this.validations = [];
91
87
  /**
92
88
  * Function calls saved for serialization
93
89
  */
94
90
  this.savedCalls = [];
91
+ /**
92
+ * list of attributes for custom works
93
+ */
94
+ this.attributes = [];
95
+ /**
96
+ * keep public ?
97
+ */
98
+ this.validations = [];
95
99
  /**
96
100
  * Pre process the variable for various reasons
97
101
  *
@@ -109,16 +113,23 @@ var _SchemaItem = class _SchemaItem {
109
113
  * note: the type of the final post-process MUST be valid
110
114
  */
111
115
  this.postProcess = [];
112
- /**
113
- * list of attributes for custom works
114
- */
115
- this.attributes = [];
116
116
  this.invalidError = "the field is invalid";
117
+ // eslint-disable-next-line @typescript-eslint/member-ordering, @typescript-eslint/unbound-method
117
118
  this.default = this.defaultValue;
118
119
  if (items && items.length > 0) {
119
120
  this.items = Array.isArray(items) ? items : Array.from(items);
120
121
  }
121
122
  }
123
+ parseJSON() {
124
+ this.addPreProcess((input) => {
125
+ try {
126
+ return typeof input === "string" ? JSON.parse(input) : input;
127
+ } catch {
128
+ return input;
129
+ }
130
+ });
131
+ return this;
132
+ }
122
133
  in(...values) {
123
134
  this.addValidation((input) => values.includes(input));
124
135
  return this;
@@ -152,37 +163,7 @@ var _SchemaItem = class _SchemaItem {
152
163
  array() {
153
164
  return new SchemaArray(this);
154
165
  }
155
- /** Returns the sub values of the input, the schema item to handle them, and the path to it */
156
- getSubInputs(_input) {
157
- return [];
158
- }
159
- /** Sets the "part" of the input that this sub schema handles. Overrides the input with the result */
160
- updateSubItemInput(path, input, value) {
161
- if (path && (0, import_object_util.isObject)(input)) {
162
- (0, import_object_util.objectSet)(input, path.split("."), value);
163
- return input;
164
- } else {
165
- return value;
166
- }
167
- }
168
- parseSubItems(input, options) {
169
- const result = { errors: [], input };
170
- for (const { item, value, path } of this.getSubInputs(result.input)) {
171
- const parsed = item.parse(value, options);
172
- if (parsed.valid) {
173
- result.input = this.updateSubItemInput(path, result.input, parsed.object);
174
- } else {
175
- for (const error of parsed.errors) {
176
- error.field = error.field ? `${path}.${error.field}` : path;
177
- result.errors.push(error);
178
- }
179
- if (options == null ? void 0 : options.fast) {
180
- break;
181
- }
182
- }
183
- }
184
- return result;
185
- }
166
+ // eslint-disable-next-line complexity
186
167
  parse(input, options) {
187
168
  var _a, _b;
188
169
  for (const preProcess of this.preProcess) {
@@ -252,23 +233,44 @@ var _SchemaItem = class _SchemaItem {
252
233
  this.postProcess.push(fn);
253
234
  return this;
254
235
  }
255
- parseJSON() {
256
- this.addPreProcess((input) => {
257
- try {
258
- return typeof input === "string" ? JSON.parse(input) : input;
259
- } catch {
260
- return input;
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
+ }
261
263
  }
262
- });
263
- return this;
264
+ }
265
+ return result;
264
266
  }
265
267
  };
266
268
  __decorateClass([
267
269
  parsable()
268
- ], _SchemaItem.prototype, "in", 1);
270
+ ], _SchemaItem.prototype, "parseJSON", 1);
269
271
  __decorateClass([
270
272
  parsable()
271
- ], _SchemaItem.prototype, "parseJSON", 1);
273
+ ], _SchemaItem.prototype, "in", 1);
272
274
  var SchemaItem = _SchemaItem;
273
275
 
274
276
  // src/items/array.ts
@@ -285,14 +287,14 @@ var SchemaArray = class extends SchemaItem {
285
287
  this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
286
288
  return this;
287
289
  }
288
- getSubInputs(input) {
289
- return Array.isArray(input) ? input.map((e, i) => ({ item: this.values, value: e, path: i.toString() })) : [];
290
- }
291
290
  unwrap() {
292
291
  return this.values;
293
292
  }
294
293
  isOfType(input) {
295
- return Array.isArray(input) && input.every((e) => this.values.isOfType(e));
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() })) : [];
296
298
  }
297
299
  };
298
300
  __decorateClass([
@@ -340,6 +342,9 @@ var SchemaNullable = class extends SchemaItem {
340
342
  unwrap() {
341
343
  return this.child;
342
344
  }
345
+ isOfType(input) {
346
+ return typeof input === "undefined" || this.child.isOfType(input);
347
+ }
343
348
  getSubInputs(input) {
344
349
  return [{ value: input, item: this.child, path: void 0 }];
345
350
  }
@@ -349,9 +354,6 @@ var SchemaNullable = class extends SchemaItem {
349
354
  }
350
355
  return super.parseSubItems(input, options);
351
356
  }
352
- isOfType(input) {
353
- return typeof input === "undefined" || this.child.isOfType(input);
354
- }
355
357
  };
356
358
  __decorateClass([
357
359
  parsable()
@@ -365,14 +367,11 @@ var SchemaObject = class extends SchemaItem {
365
367
  this.model = model;
366
368
  this.id = "object";
367
369
  }
368
- getSubInputs(input) {
369
- return (0, import_object_util2.isObject)(input) ? (0, import_object_util2.objectMap)(
370
- input,
371
- (v, k) => this.model[k] ? { item: this.model[k], value: v, path: k.toString() } : void 0
372
- ).filter((e) => !isNull(e)) : [];
373
- }
374
370
  isOfType(input) {
375
- return (0, import_object_util2.isObject)(input) && !(0, import_object_util2.objectFind)(this.model, (item, k) => !item.isOfType(input[k]));
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() })) : [];
376
375
  }
377
376
  };
378
377
 
@@ -380,19 +379,23 @@ var SchemaObject = class extends SchemaItem {
380
379
  function isNull(value) {
381
380
  return typeof value === "undefined" || value === null;
382
381
  }
383
- function parseQuery(model, query, opts) {
382
+ function parseQuery(schema, query, opts) {
384
383
  const record = {};
385
384
  for (const [key, value] of query) {
386
- const hasMultipleValues = query.getAll(key).length > 1;
387
- (0, import_object_util3.objectSet)(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), hasMultipleValues ? query.getAll(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);
388
389
  }
389
- return model.parse(record, opts);
390
+ return schema.parse(record, opts);
390
391
  }
391
392
  function parseFormData(schema, data, opts) {
392
393
  const record = {};
393
394
  for (const [key, value] of data) {
394
- const hasMultipleValues = data.getAll(key).length > 1;
395
- (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);
396
399
  }
397
400
  const handleBoolean = (value, keys) => {
398
401
  var _a;
@@ -425,6 +428,27 @@ function parseFormData(schema, data, opts) {
425
428
  function parseForm(model, form, opts) {
426
429
  return parseFormData(model, new FormData(form), opts);
427
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
+ }
428
452
 
429
453
  // src/items/any.ts
430
454
  var SchemaAny = class extends SchemaItem {
@@ -440,7 +464,7 @@ var SchemaDate = class extends SchemaItem {
440
464
  if (typeof input !== "string") {
441
465
  return input;
442
466
  }
443
- let date = void 0;
467
+ let date;
444
468
  switch (format) {
445
469
  case "default": {
446
470
  date = new Date(input);
@@ -487,6 +511,9 @@ var SchemaRecord = class extends SchemaItem {
487
511
  this.keys = keys;
488
512
  this.values = values;
489
513
  }
514
+ isOfType(input) {
515
+ return (0, import_object_util4.isObject)(input) && Object.prototype.toString.call(input) === "[object Object]";
516
+ }
490
517
  parseSubItems(input, options) {
491
518
  const result = { input, errors: [] };
492
519
  if ((0, import_object_util4.isObject)(input)) {
@@ -509,9 +536,6 @@ var SchemaRecord = class extends SchemaItem {
509
536
  }
510
537
  return result;
511
538
  }
512
- isOfType(input) {
513
- return (0, import_object_util4.isObject)(input) && Object.prototype.toString.call(input) === "[object Object]";
514
- }
515
539
  };
516
540
 
517
541
  // src/items/enum.ts
@@ -585,7 +609,7 @@ var SchemaNumber = class extends SchemaItem {
585
609
  return this;
586
610
  }
587
611
  parseString() {
588
- this.addPreProcess((input) => typeof input === "string" ? parseFloat(input) : input);
612
+ this.addPreProcess((input) => typeof input === "string" ? Number.parseFloat(input) : input);
589
613
  return this;
590
614
  }
591
615
  isOfType(input) {
@@ -689,6 +713,9 @@ var SchemaUnion = class extends SchemaItem {
689
713
  super(schemas);
690
714
  this.schemas = schemas;
691
715
  }
716
+ isOfType(input) {
717
+ return this.schemas.some((it) => it.isOfType(input));
718
+ }
692
719
  parseSubItems(input, options) {
693
720
  const result = { input, errors: [] };
694
721
  for (const schema of this.schemas) {
@@ -701,9 +728,6 @@ var SchemaUnion = class extends SchemaItem {
701
728
  }
702
729
  return result;
703
730
  }
704
- isOfType(input) {
705
- return this.schemas.some((it) => it.isOfType(input));
706
- }
707
731
  };
708
732
 
709
733
  // src/items/intersection.ts
@@ -713,6 +737,9 @@ var SchemaIntersection = class extends SchemaItem {
713
737
  super(schemas);
714
738
  this.schemas = schemas;
715
739
  }
740
+ isOfType(input) {
741
+ return this.schemas.every((it) => it.isOfType(input));
742
+ }
716
743
  parseSubItems(input, options) {
717
744
  const result = { input, errors: [] };
718
745
  for (const schema of this.schemas) {
@@ -729,9 +756,6 @@ var SchemaIntersection = class extends SchemaItem {
729
756
  }
730
757
  return result;
731
758
  }
732
- isOfType(input) {
733
- return this.schemas.every((it) => it.isOfType(input));
734
- }
735
759
  };
736
760
 
737
761
  // src/items/default.ts
@@ -745,12 +769,12 @@ var SchemaDefault = class extends SchemaItem {
745
769
  unwrap() {
746
770
  return this.child;
747
771
  }
748
- getSubInputs(input) {
749
- return [{ item: this.child, value: isNull(input) || !this.strict && !input ? this.defaultVal : input, path: void 0 }];
750
- }
751
772
  isOfType(input) {
752
773
  return this.child.isOfType(input);
753
774
  }
775
+ getSubInputs(input) {
776
+ return [{ item: this.child, value: isNull(input) || !this.strict && !input ? this.defaultVal : input, path: void 0 }];
777
+ }
754
778
  };
755
779
 
756
780
  // src/items/file.ts
@@ -760,10 +784,7 @@ var SchemaFile = class extends SchemaItem {
760
784
  this.addPreProcess((input) => this.isOfType(input) && input.size > 0 ? input : void 0);
761
785
  }
762
786
  extension(ext, error) {
763
- this.addValidation({ fn: (input) => {
764
- var _a;
765
- return (_a = input.name) == null ? void 0 : _a.endsWith(ext);
766
- }, error });
787
+ this.addValidation({ fn: (input) => input.name.endsWith(ext), error });
767
788
  return this;
768
789
  }
769
790
  maxSize(size, error) {
@@ -787,20 +808,20 @@ var SchemaTuple = class extends SchemaItem {
787
808
  super(children);
788
809
  this.children = children;
789
810
  }
790
- getSubInputs(input) {
791
- return Array.isArray(input) ? this.children.map((e, i) => ({ value: input[i], item: e, path: i.toString() })) : [];
792
- }
793
811
  isOfType(input) {
794
812
  if (!Array.isArray(input) || input.length > this.items.length) {
795
813
  return false;
796
814
  }
797
- for (const [i, item] of this.children.entries()) {
798
- if (item instanceof SchemaItem && !item.isOfType(input[i])) {
815
+ for (const [index, item] of this.children.entries()) {
816
+ if (item instanceof SchemaItem && !item.isOfType(input[index])) {
799
817
  return false;
800
818
  }
801
819
  }
802
820
  return true;
803
821
  }
822
+ getSubInputs(input) {
823
+ return Array.isArray(input) ? this.children.map((entry, index) => ({ value: input[index], item: entry, path: index.toString() })) : [];
824
+ }
804
825
  };
805
826
 
806
827
  // src/items/nullish.ts
package/dist/Schema.mjs CHANGED
@@ -10,7 +10,7 @@ var __decorateClass = (decorators, target, key, kind) => {
10
10
  };
11
11
 
12
12
  // src/helpers.ts
13
- import { objectGet, objectLoop, objectSet as objectSet2 } from "@dzeio/object-util";
13
+ import { objectClone, objectGet, objectLoop, objectSet as objectSet2 } from "@dzeio/object-util";
14
14
 
15
15
  // src/SchemaItem.ts
16
16
  import { isObject, objectClean, objectSet } from "@dzeio/object-util";
@@ -33,14 +33,18 @@ var _SchemaItem = class _SchemaItem {
33
33
  };
34
34
  }
35
35
  };
36
- /**
37
- * keep public ?
38
- */
39
- this.validations = [];
40
36
  /**
41
37
  * Function calls saved for serialization
42
38
  */
43
39
  this.savedCalls = [];
40
+ /**
41
+ * list of attributes for custom works
42
+ */
43
+ this.attributes = [];
44
+ /**
45
+ * keep public ?
46
+ */
47
+ this.validations = [];
44
48
  /**
45
49
  * Pre process the variable for various reasons
46
50
  *
@@ -58,16 +62,23 @@ var _SchemaItem = class _SchemaItem {
58
62
  * note: the type of the final post-process MUST be valid
59
63
  */
60
64
  this.postProcess = [];
61
- /**
62
- * list of attributes for custom works
63
- */
64
- this.attributes = [];
65
65
  this.invalidError = "the field is invalid";
66
+ // eslint-disable-next-line @typescript-eslint/member-ordering, @typescript-eslint/unbound-method
66
67
  this.default = this.defaultValue;
67
68
  if (items && items.length > 0) {
68
69
  this.items = Array.isArray(items) ? items : Array.from(items);
69
70
  }
70
71
  }
72
+ parseJSON() {
73
+ this.addPreProcess((input) => {
74
+ try {
75
+ return typeof input === "string" ? JSON.parse(input) : input;
76
+ } catch {
77
+ return input;
78
+ }
79
+ });
80
+ return this;
81
+ }
71
82
  in(...values) {
72
83
  this.addValidation((input) => values.includes(input));
73
84
  return this;
@@ -101,37 +112,7 @@ var _SchemaItem = class _SchemaItem {
101
112
  array() {
102
113
  return new SchemaArray(this);
103
114
  }
104
- /** Returns the sub values of the input, the schema item to handle them, and the path to it */
105
- getSubInputs(_input) {
106
- return [];
107
- }
108
- /** Sets the "part" of the input that this sub schema handles. Overrides the input with the result */
109
- updateSubItemInput(path, input, value) {
110
- if (path && isObject(input)) {
111
- objectSet(input, path.split("."), value);
112
- return input;
113
- } else {
114
- return value;
115
- }
116
- }
117
- parseSubItems(input, options) {
118
- const result = { errors: [], input };
119
- for (const { item, value, path } of this.getSubInputs(result.input)) {
120
- const parsed = item.parse(value, options);
121
- if (parsed.valid) {
122
- result.input = this.updateSubItemInput(path, result.input, parsed.object);
123
- } else {
124
- for (const error of parsed.errors) {
125
- error.field = error.field ? `${path}.${error.field}` : path;
126
- result.errors.push(error);
127
- }
128
- if (options == null ? void 0 : options.fast) {
129
- break;
130
- }
131
- }
132
- }
133
- return result;
134
- }
115
+ // eslint-disable-next-line complexity
135
116
  parse(input, options) {
136
117
  var _a, _b;
137
118
  for (const preProcess of this.preProcess) {
@@ -201,23 +182,44 @@ var _SchemaItem = class _SchemaItem {
201
182
  this.postProcess.push(fn);
202
183
  return this;
203
184
  }
204
- parseJSON() {
205
- this.addPreProcess((input) => {
206
- try {
207
- return typeof input === "string" ? JSON.parse(input) : input;
208
- } catch {
209
- return input;
185
+ /** Returns the sub values of the input, the schema item to handle them, and the path to it */
186
+ getSubInputs(_input) {
187
+ return [];
188
+ }
189
+ /** Sets the "part" of the input that this sub schema handles. Overrides the input with the result */
190
+ updateSubItemInput(path, input, value) {
191
+ if (path && isObject(input)) {
192
+ objectSet(input, path.split("."), value);
193
+ return input;
194
+ } else {
195
+ return value;
196
+ }
197
+ }
198
+ parseSubItems(input, options) {
199
+ const result = { errors: [], input };
200
+ for (const { item, value, path } of this.getSubInputs(result.input)) {
201
+ const parsed = item.parse(value, options);
202
+ if (parsed.valid) {
203
+ result.input = this.updateSubItemInput(path, result.input, parsed.object);
204
+ } else {
205
+ for (const error of parsed.errors) {
206
+ error.field = error.field ? `${path}.${error.field}` : path;
207
+ result.errors.push(error);
208
+ }
209
+ if (options == null ? void 0 : options.fast) {
210
+ break;
211
+ }
210
212
  }
211
- });
212
- return this;
213
+ }
214
+ return result;
213
215
  }
214
216
  };
215
217
  __decorateClass([
216
218
  parsable()
217
- ], _SchemaItem.prototype, "in", 1);
219
+ ], _SchemaItem.prototype, "parseJSON", 1);
218
220
  __decorateClass([
219
221
  parsable()
220
- ], _SchemaItem.prototype, "parseJSON", 1);
222
+ ], _SchemaItem.prototype, "in", 1);
221
223
  var SchemaItem = _SchemaItem;
222
224
 
223
225
  // src/items/array.ts
@@ -234,14 +236,14 @@ var SchemaArray = class extends SchemaItem {
234
236
  this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
235
237
  return this;
236
238
  }
237
- getSubInputs(input) {
238
- return Array.isArray(input) ? input.map((e, i) => ({ item: this.values, value: e, path: i.toString() })) : [];
239
- }
240
239
  unwrap() {
241
240
  return this.values;
242
241
  }
243
242
  isOfType(input) {
244
- return Array.isArray(input) && input.every((e) => this.values.isOfType(e));
243
+ return Array.isArray(input) && input.every((entry) => this.values.isOfType(entry));
244
+ }
245
+ getSubInputs(input) {
246
+ return Array.isArray(input) ? input.map((entry, index) => ({ item: this.values, value: entry, path: index.toString() })) : [];
245
247
  }
246
248
  };
247
249
  __decorateClass([
@@ -289,6 +291,9 @@ var SchemaNullable = class extends SchemaItem {
289
291
  unwrap() {
290
292
  return this.child;
291
293
  }
294
+ isOfType(input) {
295
+ return typeof input === "undefined" || this.child.isOfType(input);
296
+ }
292
297
  getSubInputs(input) {
293
298
  return [{ value: input, item: this.child, path: void 0 }];
294
299
  }
@@ -298,9 +303,6 @@ var SchemaNullable = class extends SchemaItem {
298
303
  }
299
304
  return super.parseSubItems(input, options);
300
305
  }
301
- isOfType(input) {
302
- return typeof input === "undefined" || this.child.isOfType(input);
303
- }
304
306
  };
305
307
  __decorateClass([
306
308
  parsable()
@@ -314,14 +316,11 @@ var SchemaObject = class extends SchemaItem {
314
316
  this.model = model;
315
317
  this.id = "object";
316
318
  }
317
- getSubInputs(input) {
318
- return isObject2(input) ? objectMap(
319
- input,
320
- (v, k) => this.model[k] ? { item: this.model[k], value: v, path: k.toString() } : void 0
321
- ).filter((e) => !isNull(e)) : [];
322
- }
323
319
  isOfType(input) {
324
- return isObject2(input) && !objectFind(this.model, (item, k) => !item.isOfType(input[k]));
320
+ return isObject2(input) && !objectFind(this.model, (item, key) => !item.isOfType(input[key]));
321
+ }
322
+ getSubInputs(input) {
323
+ return isObject2(input) ? objectMap(this.model, (schema, key) => ({ item: schema, value: input[key], path: key.toString() })) : [];
325
324
  }
326
325
  };
327
326
 
@@ -329,19 +328,23 @@ var SchemaObject = class extends SchemaItem {
329
328
  function isNull(value) {
330
329
  return typeof value === "undefined" || value === null;
331
330
  }
332
- function parseQuery(model, query, opts) {
331
+ function parseQuery(schema, query, opts) {
333
332
  const record = {};
334
333
  for (const [key, value] of query) {
335
- const hasMultipleValues = query.getAll(key).length > 1;
336
- objectSet2(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), hasMultipleValues ? query.getAll(key) : value);
334
+ const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
335
+ const schemaItem = getSchemaItemAtPath(schema, keys);
336
+ const finalValue = schemaItem instanceof SchemaArray ? query.getAll(key) : value;
337
+ objectSet2(record, keys, finalValue);
337
338
  }
338
- return model.parse(record, opts);
339
+ return schema.parse(record, opts);
339
340
  }
340
341
  function parseFormData(schema, data, opts) {
341
342
  const record = {};
342
343
  for (const [key, value] of data) {
343
- const hasMultipleValues = data.getAll(key).length > 1;
344
- objectSet2(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), hasMultipleValues ? data.getAll(key) : value);
344
+ const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
345
+ const schemaItem = getSchemaItemAtPath(schema, keys);
346
+ const finalValue = schemaItem instanceof SchemaArray ? data.getAll(key) : value;
347
+ objectSet2(record, keys, finalValue);
345
348
  }
346
349
  const handleBoolean = (value, keys) => {
347
350
  var _a;
@@ -374,6 +377,27 @@ function parseFormData(schema, data, opts) {
374
377
  function parseForm(model, form, opts) {
375
378
  return parseFormData(model, new FormData(form), opts);
376
379
  }
380
+ function getSchemaItemAtPath(schema, path) {
381
+ let current = schema;
382
+ const internalPath = objectClone(path);
383
+ for (const key of path) {
384
+ if (!current) {
385
+ return void 0;
386
+ }
387
+ internalPath.shift();
388
+ while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
389
+ current = current.unwrap();
390
+ }
391
+ if (current instanceof SchemaObject) {
392
+ current = current.model[key];
393
+ current = getSchemaItemAtPath(current, internalPath);
394
+ }
395
+ }
396
+ while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
397
+ current = current.unwrap();
398
+ }
399
+ return current;
400
+ }
377
401
 
378
402
  // src/items/any.ts
379
403
  var SchemaAny = class extends SchemaItem {
@@ -389,7 +413,7 @@ var SchemaDate = class extends SchemaItem {
389
413
  if (typeof input !== "string") {
390
414
  return input;
391
415
  }
392
- let date = void 0;
416
+ let date;
393
417
  switch (format) {
394
418
  case "default": {
395
419
  date = new Date(input);
@@ -436,6 +460,9 @@ var SchemaRecord = class extends SchemaItem {
436
460
  this.keys = keys;
437
461
  this.values = values;
438
462
  }
463
+ isOfType(input) {
464
+ return isObject3(input) && Object.prototype.toString.call(input) === "[object Object]";
465
+ }
439
466
  parseSubItems(input, options) {
440
467
  const result = { input, errors: [] };
441
468
  if (isObject3(input)) {
@@ -458,9 +485,6 @@ var SchemaRecord = class extends SchemaItem {
458
485
  }
459
486
  return result;
460
487
  }
461
- isOfType(input) {
462
- return isObject3(input) && Object.prototype.toString.call(input) === "[object Object]";
463
- }
464
488
  };
465
489
 
466
490
  // src/items/enum.ts
@@ -534,7 +558,7 @@ var SchemaNumber = class extends SchemaItem {
534
558
  return this;
535
559
  }
536
560
  parseString() {
537
- this.addPreProcess((input) => typeof input === "string" ? parseFloat(input) : input);
561
+ this.addPreProcess((input) => typeof input === "string" ? Number.parseFloat(input) : input);
538
562
  return this;
539
563
  }
540
564
  isOfType(input) {
@@ -638,6 +662,9 @@ var SchemaUnion = class extends SchemaItem {
638
662
  super(schemas);
639
663
  this.schemas = schemas;
640
664
  }
665
+ isOfType(input) {
666
+ return this.schemas.some((it) => it.isOfType(input));
667
+ }
641
668
  parseSubItems(input, options) {
642
669
  const result = { input, errors: [] };
643
670
  for (const schema of this.schemas) {
@@ -650,9 +677,6 @@ var SchemaUnion = class extends SchemaItem {
650
677
  }
651
678
  return result;
652
679
  }
653
- isOfType(input) {
654
- return this.schemas.some((it) => it.isOfType(input));
655
- }
656
680
  };
657
681
 
658
682
  // src/items/intersection.ts
@@ -662,6 +686,9 @@ var SchemaIntersection = class extends SchemaItem {
662
686
  super(schemas);
663
687
  this.schemas = schemas;
664
688
  }
689
+ isOfType(input) {
690
+ return this.schemas.every((it) => it.isOfType(input));
691
+ }
665
692
  parseSubItems(input, options) {
666
693
  const result = { input, errors: [] };
667
694
  for (const schema of this.schemas) {
@@ -678,9 +705,6 @@ var SchemaIntersection = class extends SchemaItem {
678
705
  }
679
706
  return result;
680
707
  }
681
- isOfType(input) {
682
- return this.schemas.every((it) => it.isOfType(input));
683
- }
684
708
  };
685
709
 
686
710
  // src/items/default.ts
@@ -694,12 +718,12 @@ var SchemaDefault = class extends SchemaItem {
694
718
  unwrap() {
695
719
  return this.child;
696
720
  }
697
- getSubInputs(input) {
698
- return [{ item: this.child, value: isNull(input) || !this.strict && !input ? this.defaultVal : input, path: void 0 }];
699
- }
700
721
  isOfType(input) {
701
722
  return this.child.isOfType(input);
702
723
  }
724
+ getSubInputs(input) {
725
+ return [{ item: this.child, value: isNull(input) || !this.strict && !input ? this.defaultVal : input, path: void 0 }];
726
+ }
703
727
  };
704
728
 
705
729
  // src/items/file.ts
@@ -709,10 +733,7 @@ var SchemaFile = class extends SchemaItem {
709
733
  this.addPreProcess((input) => this.isOfType(input) && input.size > 0 ? input : void 0);
710
734
  }
711
735
  extension(ext, error) {
712
- this.addValidation({ fn: (input) => {
713
- var _a;
714
- return (_a = input.name) == null ? void 0 : _a.endsWith(ext);
715
- }, error });
736
+ this.addValidation({ fn: (input) => input.name.endsWith(ext), error });
716
737
  return this;
717
738
  }
718
739
  maxSize(size, error) {
@@ -736,20 +757,20 @@ var SchemaTuple = class extends SchemaItem {
736
757
  super(children);
737
758
  this.children = children;
738
759
  }
739
- getSubInputs(input) {
740
- return Array.isArray(input) ? this.children.map((e, i) => ({ value: input[i], item: e, path: i.toString() })) : [];
741
- }
742
760
  isOfType(input) {
743
761
  if (!Array.isArray(input) || input.length > this.items.length) {
744
762
  return false;
745
763
  }
746
- for (const [i, item] of this.children.entries()) {
747
- if (item instanceof SchemaItem && !item.isOfType(input[i])) {
764
+ for (const [index, item] of this.children.entries()) {
765
+ if (item instanceof SchemaItem && !item.isOfType(input[index])) {
748
766
  return false;
749
767
  }
750
768
  }
751
769
  return true;
752
770
  }
771
+ getSubInputs(input) {
772
+ return Array.isArray(input) ? this.children.map((entry, index) => ({ value: input[index], item: entry, path: index.toString() })) : [];
773
+ }
753
774
  };
754
775
 
755
776
  // src/items/nullish.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dzeio/schema",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "dependencies": {
5
5
  "@dzeio/object-util": "^1.9.2"
6
6
  },