@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.d.mts CHANGED
@@ -1,14 +1,9 @@
1
1
  import { StandardSchemaV1 } from '@standard-schema/spec';
2
2
 
3
- declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type> {
4
- '~standard': StandardSchemaV1.Props<Type>;
5
- /**
6
- * keep public ?
7
- */
8
- validations: Array<{
9
- fn: (input: Type) => boolean;
10
- error?: string | undefined;
11
- }>;
3
+ declare abstract class SchemaItem<Output = any, Input = Output> implements StandardSchemaV1<Input, Output> {
4
+ readonly _output: Output;
5
+ readonly _input: Input;
6
+ '~standard': StandardSchemaV1.Props<Input, Output>;
12
7
  /**
13
8
  * Function calls saved for serialization
14
9
  */
@@ -16,6 +11,17 @@ declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type>
16
11
  name: string;
17
12
  args: Array<string> | IArguments | undefined;
18
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
+ }>;
19
25
  /**
20
26
  * Pre process the variable for various reasons
21
27
  *
@@ -23,7 +29,7 @@ declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type>
23
29
  *
24
30
  * note: The type of the final Pre-Process MUST be valid
25
31
  */
26
- preProcess: Array<(input: unknown) => Type | unknown>;
32
+ protected readonly preProcess: Array<(input: unknown) => Input | unknown>;
27
33
  /**
28
34
  * post process the variable after it being validated
29
35
  *
@@ -31,25 +37,26 @@ declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type>
31
37
  *
32
38
  * note: the type of the final post-process MUST be valid
33
39
  */
34
- postProcess: Array<(input: Type) => Type>;
35
- /**
36
- * list of attributes for custom works
37
- */
38
- readonly attributes: Array<string>;
39
- private readonly items?;
40
+ protected readonly postProcess: Array<(input: Output) => Output>;
41
+ protected readonly items?: Array<unknown>;
40
42
  private invalidError;
41
43
  constructor(items?: Array<unknown> | IArguments);
42
- defaultValue(value: Type, strict?: boolean): this;
44
+ parseJSON(): this;
43
45
  /**
44
46
  * make sure the value is one of the `values`
45
47
  * @param values the values the item MUST contains
46
48
  */
47
- in(...values: Array<Type>): this;
49
+ in(...values: Array<Output>): this;
48
50
  attrs(...attributes: Array<string>): this;
49
51
  attr(...attributes: Array<string>): this;
50
52
  setInvalidError(err: string): this;
51
53
  clone(): this;
54
+ defaultValue(value: Output, strict?: boolean): SchemaDefault<this>;
55
+ default: (value: Output, strict?: boolean) => SchemaDefault<this>;
52
56
  nullable(): SchemaNullable<this>;
57
+ or<Other extends SchemaItem>(other: Other): SchemaUnion<[this, Other]>;
58
+ and<Type extends SchemaItem>(other: Type): SchemaIntersection<[this, Type]>;
59
+ array(): SchemaArray<this>;
53
60
  /**
54
61
  * schemas implementing unwrap can return their child component (mostly the value)
55
62
  *
@@ -58,21 +65,37 @@ declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type>
58
65
  * es2: s.nullable(s.string()) returns s.string()
59
66
  */
60
67
  unwrap?(): SchemaItem;
61
- parse(input: unknown, options?: {
62
- fast?: boolean;
63
- }): ValidationResult<Type>;
68
+ parse(input: unknown, options?: ValidationOptions): ValidationResult<Output>;
64
69
  toJSON(): SchemaJSON;
65
- protected addValidation(fn: ((input: Type) => boolean) | {
66
- fn: (input: Type) => boolean;
70
+ addValidation(fn: ((input: Output) => boolean) | {
71
+ fn: (input: Output) => boolean;
67
72
  error?: string;
68
73
  }, error?: string): this;
69
- protected addPreProcess(fn: (input: unknown) => Type | unknown): this;
70
- protected addPostProcess(fn: (input: Type) => Type): this;
71
- abstract isOfType(input: unknown): input is Type;
74
+ addPreProcess(fn: (input: unknown) => Input | unknown): this;
75
+ addPostProcess(fn: (input: Output) => Output): this;
76
+ /** Returns the sub values of the input, the schema item to handle them, and the path to it */
77
+ protected getSubInputs(_input: unknown): Array<{
78
+ item: SchemaItem;
79
+ value: unknown;
80
+ path: string | undefined;
81
+ }>;
82
+ /** Sets the "part" of the input that this sub schema handles. Overrides the input with the result */
83
+ protected updateSubItemInput(path: string | undefined, input: unknown, value: unknown): unknown;
84
+ protected parseSubItems(input: unknown, options?: ValidationOptions): {
85
+ input: unknown;
86
+ errors?: Array<ValidationError>;
87
+ };
88
+ abstract isOfType(input: unknown): input is Output;
72
89
  }
73
90
 
74
- type SchemaInfer<Type extends SchemaItem> = Type extends SchemaItem<infer X> ? X : never
91
+ type SchemaInfer<Type extends SchemaItem> = Type['_output']
75
92
  type Infer<Type extends SchemaItem> = SchemaInfer<Type>
93
+ type SchemaInputInfer<Type extends SchemaItem> = Type['_input']
94
+ type InputInfer<Type extends SchemaItem> = SchemaInputInfer<Type>
95
+
96
+ interface ValidationOptions {
97
+ fast?: boolean
98
+ }
76
99
 
77
100
  interface ValidationError {
78
101
  message: string
@@ -100,12 +123,21 @@ type ValidationResultOld<T> = {
100
123
  error: Array<ValidationError>
101
124
  }
102
125
 
126
+ /**
127
+ * JSON Schema used for client side validation
128
+ */
103
129
  interface SchemaJSON {
130
+ /** Id of the schema item (name of the constructor) */
104
131
  i: string
132
+ /** Attributes */
105
133
  a?: Array<string> | undefined
134
+ /** Children schema items */
106
135
  c?: Array<unknown> | undefined
136
+ /** Functions called to modify the behavior of the item */
107
137
  f?: Array<{
138
+ /** Name of the function */
108
139
  n: string
140
+ /** Arguments for the function */
109
141
  a?: Array<unknown> | undefined
110
142
  }> | undefined
111
143
  }
@@ -128,27 +160,29 @@ type ModelInfer$1<M extends Model> = {
128
160
  }
129
161
 
130
162
  declare class SchemaAny extends SchemaItem {
131
- isOfType(input: unknown): input is any;
163
+ isOfType(_input: unknown): _input is any;
132
164
  }
133
165
 
166
+ type DateFormat = 'default' | 'iso8601' | 'yy-mm-dd' | 'jj/mm/yy';
134
167
  declare class SchemaDate extends SchemaItem<Date> {
135
- parseString(format?: 'iso8601' | 'yy-mm-dd' | 'jj/mm/yy'): this;
168
+ parseString(format?: DateFormat): this;
136
169
  isOfType(input: unknown): input is Date;
137
170
  }
138
171
 
139
- declare class SchemaRecord<Keys extends SchemaItem, Values extends SchemaItem> extends SchemaItem<Record<SchemaInfer<Keys>, SchemaInfer<Values>>> {
172
+ declare class SchemaRecord<Keys extends SchemaItem<string | number | symbol>, Values extends SchemaItem> extends SchemaItem<Record<SchemaInfer<Keys>, SchemaInfer<Values>>> {
140
173
  private readonly keys;
141
174
  private readonly values;
142
175
  constructor(keys: Keys, values: Values);
143
- parse(input: unknown, options?: {
144
- fast?: boolean;
145
- }): ValidationResult<Record<SchemaInfer<Keys>, SchemaInfer<Values>>>;
146
176
  isOfType(input: unknown): input is Record<SchemaInfer<Keys>, SchemaInfer<Values>>;
177
+ protected parseSubItems(input: unknown, options?: ValidationOptions): {
178
+ input: unknown;
179
+ errors?: Array<ValidationError>;
180
+ };
147
181
  }
148
182
 
149
- declare class SchemaArray<Type extends SchemaItem> extends SchemaItem<Array<SchemaInfer<Type>>> {
150
- readonly values: Type;
151
- constructor(values: Type);
183
+ declare class SchemaArray<Child extends SchemaItem, Output = SchemaInfer<Child>, Input = SchemaInputInfer<Child>> extends SchemaItem<Array<Output>, Array<Input>> {
184
+ readonly values: Child;
185
+ constructor(values: Child);
152
186
  /**
153
187
  * transform the array so it only contains one of each elements
154
188
  */
@@ -158,11 +192,13 @@ declare class SchemaArray<Type extends SchemaItem> extends SchemaItem<Array<Sche
158
192
  * @param [separator] default:`,` the separator to use
159
193
  */
160
194
  split(separator?: string): this;
161
- parse(input: unknown, options?: {
162
- fast?: boolean;
163
- }): ValidationResult<Array<SchemaInfer<Type>>>;
164
- unwrap(): Type;
165
- isOfType(input: unknown): input is Array<SchemaInfer<Type>>;
195
+ unwrap(): Child;
196
+ isOfType(input: unknown): input is Array<Output>;
197
+ protected getSubInputs(input: unknown): Array<{
198
+ item: SchemaItem;
199
+ value: unknown;
200
+ path: string | undefined;
201
+ }>;
166
202
  }
167
203
 
168
204
  declare class SchemaBoolean extends SchemaItem<boolean> {
@@ -179,8 +215,8 @@ interface EnumLike {
179
215
  [n: number]: string;
180
216
  }
181
217
  declare class SchemaEnum<E extends EnumLike> extends SchemaItem<E[keyof E]> {
182
- private templateEnum;
183
- private type;
218
+ private readonly templateEnum;
219
+ private readonly type;
184
220
  constructor(templateEnum: E);
185
221
  isOfType(input: unknown): input is E[keyof E];
186
222
  }
@@ -191,16 +227,21 @@ declare class SchemaLiteral<Type> extends SchemaItem<Type> {
191
227
  isOfType(input: unknown): input is Type;
192
228
  }
193
229
 
194
- declare class SchemaNullable<Type extends SchemaItem> extends SchemaItem<SchemaInfer<Type> | undefined> {
195
- readonly child: Type;
196
- constructor(child: Type);
197
- falthyAsNull(): this;
198
- unwrap(): Type;
199
- parse(input: unknown, options?: {
200
- fast?: boolean;
201
- }): ValidationResult<SchemaInfer<Type> | undefined>;
202
- isOfType(input: unknown): input is SchemaInfer<Type> | undefined;
203
- private isNull;
230
+ declare class SchemaNullable<Child extends SchemaItem, Output = SchemaInfer<Child>, Input = SchemaInputInfer<Child>> extends SchemaItem<Output | undefined, Input | null | undefined> {
231
+ private readonly child;
232
+ constructor(child: Child);
233
+ falsyAsNull(): this;
234
+ unwrap(): Child;
235
+ isOfType(input: unknown): input is Output | undefined;
236
+ protected getSubInputs(input: unknown): Array<{
237
+ item: SchemaItem;
238
+ value: unknown;
239
+ path: string | undefined;
240
+ }>;
241
+ protected parseSubItems(input: unknown, options?: ValidationOptions): {
242
+ input: unknown;
243
+ errors?: Array<ValidationError>;
244
+ };
204
245
  }
205
246
 
206
247
  declare class SchemaNumber extends SchemaItem<number> {
@@ -244,10 +285,12 @@ declare class SchemaObject<T extends Record<string, SchemaItem> = any> extends S
244
285
  readonly model: T;
245
286
  id: string;
246
287
  constructor(model: T);
247
- parse(input: unknown, options?: {
248
- fast?: boolean;
249
- }): ValidationResult<ModelInfer<T>>;
250
288
  isOfType(input: unknown): input is ModelInfer<T>;
289
+ protected getSubInputs(input: unknown): Array<{
290
+ item: SchemaItem;
291
+ value: unknown;
292
+ path: string | undefined;
293
+ }>;
251
294
  }
252
295
 
253
296
  declare class SchemaString extends SchemaItem<string> {
@@ -285,26 +328,97 @@ declare class SchemaString extends SchemaItem<string> {
285
328
  isOfType(input: unknown): input is string;
286
329
  }
287
330
 
288
- type ItemType<T extends Array<SchemaItem>> = SchemaInfer<T[number]>;
289
- declare class SchemaUnion<T extends Array<SchemaItem>> extends SchemaItem<ItemType<T>> {
290
- private schemas;
291
- constructor(...schemas: T);
292
- parse(input: unknown, options?: {
293
- fast?: boolean;
294
- }): ValidationResult<SchemaInfer<T[number]>>;
295
- isOfType(input: unknown): input is ItemType<T>;
331
+ declare class SchemaUnion<Children extends Array<SchemaItem>, Outputs = SchemaInfer<Children[number]>, Inputs = SchemaInputInfer<Children[number]>> extends SchemaItem<Outputs, Inputs> {
332
+ private readonly schemas;
333
+ constructor(...schemas: Children);
334
+ isOfType(input: unknown): input is Outputs;
335
+ protected parseSubItems(input: unknown, options?: ValidationOptions): {
336
+ input: unknown;
337
+ errors?: Array<ValidationError>;
338
+ };
339
+ }
340
+
341
+ type Prettify<T> = T extends unknown ? {
342
+ [K in keyof T]: T[K];
343
+ } : never;
344
+ type UnionToIntersection<U> = (U extends U ? (x: U) => void : never) extends ((x: infer I) => void) ? I : never;
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
+ private readonly schemas;
347
+ constructor(...schemas: Children);
348
+ isOfType(input: unknown): input is Outputs;
349
+ protected parseSubItems(input: unknown, options?: ValidationOptions): {
350
+ input: unknown;
351
+ errors?: Array<ValidationError>;
352
+ };
353
+ }
354
+
355
+ declare class SchemaDefault<Child extends SchemaItem, Output = SchemaInfer<Child>, Input = SchemaInputInfer<Child>> extends SchemaItem<Output, Input | null | undefined> {
356
+ private readonly child;
357
+ private readonly defaultVal;
358
+ private readonly strict;
359
+ constructor(child: Child, defaultVal: Output, strict?: boolean);
360
+ unwrap(): Child;
361
+ isOfType(input: unknown): input is Output;
362
+ protected getSubInputs(input: unknown): Array<{
363
+ item: SchemaItem;
364
+ value: unknown;
365
+ path: string | undefined;
366
+ }>;
367
+ }
368
+
369
+ declare class SchemaFile extends SchemaItem<File> {
370
+ constructor();
371
+ extension(ext: string, error?: string): this;
372
+ maxSize(size: number, error?: string): this;
373
+ isOfType(input: unknown): input is File;
374
+ }
375
+
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
+ declare class SchemaTuple<Children extends Array<SchemaItem>, Outputs = TupleToOutputs<Children>, Inputs = TupleToInputs<Children>> extends SchemaItem<Outputs, Inputs> {
379
+ private readonly children;
380
+ constructor(...children: Children);
381
+ isOfType(input: unknown): input is Outputs;
382
+ protected getSubInputs(input: unknown): Array<{
383
+ item: SchemaItem;
384
+ value: unknown;
385
+ path: string | undefined;
386
+ }>;
387
+ }
388
+
389
+ declare class SchemaUndefined extends SchemaItem<undefined> {
390
+ isOfType(input: unknown): input is undefined;
391
+ }
392
+ declare class SchemaNull extends SchemaItem<null> {
393
+ isOfType(input: unknown): input is null;
394
+ }
395
+ declare class SchemaVoid extends SchemaItem<void> {
396
+ isOfType(input: unknown): input is void;
397
+ }
398
+ declare class SchemaNullish extends SchemaItem<null | undefined> {
399
+ isOfType(input: unknown): input is null | undefined;
400
+ }
401
+ declare class SchemaNever extends SchemaItem<never> {
402
+ isOfType(_input: unknown): _input is never;
296
403
  }
297
404
 
298
405
  declare function isNull(value: unknown): value is undefined | null;
299
- declare function parseQuery<T extends SchemaItem>(model: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
300
- declare function parseFormData<T extends SchemaItem>(model: T, data: FormData, 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
+ */
410
+ declare function parseFormData<T extends SchemaItem>(schema: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
301
411
  declare function parseForm<T extends SchemaItem>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
302
412
 
303
- declare function parceable(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
413
+ /**
414
+ * Decorator for modifier functions on SchemaItems to save them in the "savedCalls" property for the serialization
415
+ */
416
+ declare function parsable(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
304
417
  type SchemaItemStatic = new (...args: Array<any>) => SchemaItem;
305
418
  declare const Types: {
306
419
  readonly Any: typeof SchemaAny;
307
420
  readonly Array: typeof SchemaArray;
421
+ readonly Tuple: typeof SchemaTuple;
308
422
  readonly Boolean: typeof SchemaBoolean;
309
423
  readonly Date: typeof SchemaDate;
310
424
  readonly Enum: typeof SchemaEnum;
@@ -314,9 +428,15 @@ declare const Types: {
314
428
  readonly Record: typeof SchemaRecord;
315
429
  readonly String: typeof SchemaString;
316
430
  readonly Union: typeof SchemaUnion;
431
+ readonly Intersection: typeof SchemaIntersection;
432
+ readonly Default: typeof SchemaDefault;
433
+ readonly Undefined: typeof SchemaUndefined;
434
+ readonly Void: typeof SchemaVoid;
435
+ readonly Null: typeof SchemaNull;
436
+ readonly Nullish: typeof SchemaNullish;
317
437
  };
318
438
  declare class Schema<T extends Record<string, SchemaItem> = Record<string, SchemaItem>> extends SchemaObject<T> {
319
- private static registeredModules;
439
+ private static readonly registeredModules;
320
440
  static register(module: SchemaItemStatic): void;
321
441
  static getModule(name: string): SchemaItemStatic | undefined;
322
442
  static any(): SchemaAny;
@@ -338,7 +458,15 @@ declare class Schema<T extends Record<string, SchemaItem> = Record<string, Schem
338
458
  * See {@link SchemaString}
339
459
  */
340
460
  static string(...inputs: ConstructorParameters<typeof SchemaString>): SchemaString;
461
+ static file(...inputs: ConstructorParameters<typeof SchemaFile>): SchemaFile;
341
462
  static union<Type extends Array<SchemaItem>>(...inputs: ConstructorParameters<typeof SchemaUnion<Type>>): SchemaUnion<Type>;
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>;
465
+ static undefined(...inputs: ConstructorParameters<typeof SchemaUndefined>): SchemaUndefined;
466
+ static null(...inputs: ConstructorParameters<typeof SchemaNull>): SchemaNull;
467
+ static nullish(...inputs: ConstructorParameters<typeof SchemaNullish>): SchemaNullish;
468
+ static void(...inputs: ConstructorParameters<typeof SchemaVoid>): SchemaVoid;
469
+ static never(...inputs: ConstructorParameters<typeof SchemaNever>): SchemaNever;
342
470
  static fromJSON(json: SchemaJSON): SchemaItem;
343
471
  static isSchemaJSON(data: unknown): data is SchemaJSON;
344
472
  /**
@@ -360,4 +488,4 @@ declare class Schema<T extends Record<string, SchemaItem> = Record<string, Schem
360
488
  }
361
489
  declare const s: typeof Schema;
362
490
 
363
- export { type Infer, type Model, type ModelInfer$1 as ModelInfer, SchemaAny, SchemaArray, SchemaBoolean, SchemaDate, SchemaEnum, type SchemaInfer, SchemaItem, type SchemaItemJSON, type SchemaJSON, SchemaLiteral, SchemaNullable, SchemaNumber, SchemaObject, SchemaRecord, SchemaString, SchemaUnion, Types, type ValidationError, type ValidationResult, type ValidationResultOld, Schema as default, isNull, parceable, parseForm, parseFormData, parseQuery, s };
491
+ export { type Infer, type InputInfer, type Model, type ModelInfer$1 as ModelInfer, SchemaAny, SchemaArray, SchemaBoolean, SchemaDate, SchemaDefault, SchemaEnum, SchemaFile, type SchemaInfer, type SchemaInputInfer, SchemaIntersection, SchemaItem, type SchemaItemJSON, type SchemaJSON, SchemaLiteral, SchemaNever, SchemaNull, SchemaNullable, SchemaNullish, SchemaNumber, SchemaObject, SchemaRecord, SchemaString, SchemaUndefined, SchemaUnion, SchemaVoid, Types, type ValidationError, type ValidationOptions, type ValidationResult, type ValidationResultOld, Schema as default, isNull, parsable, parseForm, parseFormData, parseQuery, s };