@dzeio/schema 0.4.2 → 0.5.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,12 +1,14 @@
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>;
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>;
5
7
  /**
6
8
  * keep public ?
7
9
  */
8
- validations: Array<{
9
- fn: (input: Type) => boolean;
10
+ protected readonly validations: Array<{
11
+ fn: (input: Output) => boolean;
10
12
  error?: string | undefined;
11
13
  }>;
12
14
  /**
@@ -23,7 +25,7 @@ declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type>
23
25
  *
24
26
  * note: The type of the final Pre-Process MUST be valid
25
27
  */
26
- preProcess: Array<(input: unknown) => Type | unknown>;
28
+ protected readonly preProcess: Array<(input: unknown) => Input | unknown>;
27
29
  /**
28
30
  * post process the variable after it being validated
29
31
  *
@@ -31,25 +33,29 @@ declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type>
31
33
  *
32
34
  * note: the type of the final post-process MUST be valid
33
35
  */
34
- postProcess: Array<(input: Type) => Type>;
36
+ protected readonly postProcess: Array<(input: Output) => Output>;
35
37
  /**
36
38
  * list of attributes for custom works
37
39
  */
38
40
  readonly attributes: Array<string>;
39
- private readonly items?;
41
+ protected readonly items?: Array<unknown>;
40
42
  private invalidError;
41
43
  constructor(items?: Array<unknown> | IArguments);
42
- defaultValue(value: Type, strict?: boolean): this;
43
44
  /**
44
45
  * make sure the value is one of the `values`
45
46
  * @param values the values the item MUST contains
46
47
  */
47
- in(...values: Array<Type>): this;
48
+ in(...values: Array<Output>): this;
48
49
  attrs(...attributes: Array<string>): this;
49
50
  attr(...attributes: Array<string>): this;
50
51
  setInvalidError(err: string): this;
51
52
  clone(): this;
53
+ defaultValue(value: Output, strict?: boolean): SchemaDefault<this>;
54
+ default: (value: Output, strict?: boolean) => SchemaDefault<this>;
52
55
  nullable(): SchemaNullable<this>;
56
+ or<Other extends SchemaItem>(other: Other): SchemaUnion<[this, Other]>;
57
+ and<Type extends SchemaItem>(other: Type): SchemaIntersection<[this, Type]>;
58
+ array(): SchemaArray<this>;
53
59
  /**
54
60
  * schemas implementing unwrap can return their child component (mostly the value)
55
61
  *
@@ -58,21 +64,38 @@ declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type>
58
64
  * es2: s.nullable(s.string()) returns s.string()
59
65
  */
60
66
  unwrap?(): SchemaItem;
61
- parse(input: unknown, options?: {
62
- fast?: boolean;
63
- }): ValidationResult<Type>;
67
+ /** Returns the sub values of the input, the schema item to handle them, and the path to it */
68
+ protected getSubInputs(_input: unknown): Array<{
69
+ item: SchemaItem;
70
+ value: unknown;
71
+ path: string | undefined;
72
+ }>;
73
+ /** Sets the "part" of the input that this sub schema handles. Overrides the input with the result */
74
+ protected updateSubItemInput(path: string | undefined, input: unknown, value: unknown): unknown;
75
+ protected parseSubItems(input: unknown, options?: ValidationOptions): {
76
+ input: unknown;
77
+ errors?: Array<ValidationError>;
78
+ };
79
+ parse(input: unknown, options?: ValidationOptions): ValidationResult<Output>;
64
80
  toJSON(): SchemaJSON;
65
- protected addValidation(fn: ((input: Type) => boolean) | {
66
- fn: (input: Type) => boolean;
81
+ addValidation(fn: ((input: Output) => boolean) | {
82
+ fn: (input: Output) => boolean;
67
83
  error?: string;
68
84
  }, 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;
85
+ addPreProcess(fn: (input: unknown) => Input | unknown): this;
86
+ addPostProcess(fn: (input: Output) => Output): this;
87
+ parseJSON(): this;
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>>>;
176
+ protected parseSubItems(input: unknown, options?: ValidationOptions): {
177
+ input: unknown;
178
+ errors?: ValidationError[];
179
+ };
146
180
  isOfType(input: unknown): input is Record<SchemaInfer<Keys>, SchemaInfer<Values>>;
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
+ protected getSubInputs(input: unknown): Array<{
196
+ item: SchemaItem;
197
+ value: unknown;
198
+ path: string | undefined;
199
+ }>;
200
+ unwrap(): Child;
201
+ isOfType(input: unknown): input is Array<Output>;
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
+ protected getSubInputs(input: unknown): Array<{
236
+ item: SchemaItem;
237
+ value: unknown;
238
+ path: string | undefined;
239
+ }>;
240
+ protected parseSubItems(input: unknown, options?: ValidationOptions): {
241
+ input: unknown;
242
+ errors?: Array<ValidationError>;
243
+ };
244
+ isOfType(input: unknown): input is Output | undefined;
204
245
  }
205
246
 
206
247
  declare class SchemaNumber extends SchemaItem<number> {
@@ -244,9 +285,11 @@ 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>>;
288
+ protected getSubInputs(input: unknown): Array<{
289
+ item: SchemaItem;
290
+ value: unknown;
291
+ path: string | undefined;
292
+ }>;
250
293
  isOfType(input: unknown): input is ModelInfer<T>;
251
294
  }
252
295
 
@@ -285,26 +328,94 @@ 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
+ protected parseSubItems(input: unknown, options?: ValidationOptions): {
335
+ input: unknown;
336
+ errors?: Array<ValidationError>;
337
+ };
338
+ isOfType(input: unknown): input is Outputs;
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
+ protected parseSubItems(input: unknown, options?: ValidationOptions): {
349
+ input: unknown;
350
+ errors?: Array<ValidationError>;
351
+ };
352
+ isOfType(input: unknown): input is Outputs;
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
+ protected getSubInputs(input: unknown): Array<{
362
+ item: SchemaItem;
363
+ value: unknown;
364
+ path: string | undefined;
365
+ }>;
366
+ isOfType(input: unknown): input is Output;
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 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"]]>]>;
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
+ protected getSubInputs(input: unknown): Array<{
382
+ item: SchemaItem;
383
+ value: unknown;
384
+ path: string | undefined;
385
+ }>;
386
+ isOfType(input: unknown): input is Outputs;
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
406
  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']>;
407
+ declare function parseFormData<T extends SchemaItem>(schema: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
301
408
  declare function parseForm<T extends SchemaItem>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
302
409
 
303
- declare function parceable(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
410
+ /**
411
+ * Decorator for modifier functions on SchemaItems to save them in the "savedCalls" property for the serialization
412
+ */
413
+ declare function parsable(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
304
414
  type SchemaItemStatic = new (...args: Array<any>) => SchemaItem;
305
415
  declare const Types: {
306
416
  readonly Any: typeof SchemaAny;
307
417
  readonly Array: typeof SchemaArray;
418
+ readonly Tuple: typeof SchemaTuple;
308
419
  readonly Boolean: typeof SchemaBoolean;
309
420
  readonly Date: typeof SchemaDate;
310
421
  readonly Enum: typeof SchemaEnum;
@@ -314,9 +425,15 @@ declare const Types: {
314
425
  readonly Record: typeof SchemaRecord;
315
426
  readonly String: typeof SchemaString;
316
427
  readonly Union: typeof SchemaUnion;
428
+ readonly Intersection: typeof SchemaIntersection;
429
+ readonly Default: typeof SchemaDefault;
430
+ readonly Undefined: typeof SchemaUndefined;
431
+ readonly Void: typeof SchemaVoid;
432
+ readonly Null: typeof SchemaNull;
433
+ readonly Nullish: typeof SchemaNullish;
317
434
  };
318
435
  declare class Schema<T extends Record<string, SchemaItem> = Record<string, SchemaItem>> extends SchemaObject<T> {
319
- private static registeredModules;
436
+ private static readonly registeredModules;
320
437
  static register(module: SchemaItemStatic): void;
321
438
  static getModule(name: string): SchemaItemStatic | undefined;
322
439
  static any(): SchemaAny;
@@ -338,7 +455,15 @@ declare class Schema<T extends Record<string, SchemaItem> = Record<string, Schem
338
455
  * See {@link SchemaString}
339
456
  */
340
457
  static string(...inputs: ConstructorParameters<typeof SchemaString>): SchemaString;
458
+ static file(...inputs: ConstructorParameters<typeof SchemaFile>): SchemaFile;
341
459
  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>;
462
+ static undefined(...inputs: ConstructorParameters<typeof SchemaUndefined>): SchemaUndefined;
463
+ static null(...inputs: ConstructorParameters<typeof SchemaNull>): SchemaNull;
464
+ static nullish(...inputs: ConstructorParameters<typeof SchemaNullish>): SchemaNullish;
465
+ static void(...inputs: ConstructorParameters<typeof SchemaVoid>): SchemaVoid;
466
+ static never(...inputs: ConstructorParameters<typeof SchemaNever>): SchemaNever;
342
467
  static fromJSON(json: SchemaJSON): SchemaItem;
343
468
  static isSchemaJSON(data: unknown): data is SchemaJSON;
344
469
  /**
@@ -360,4 +485,4 @@ declare class Schema<T extends Record<string, SchemaItem> = Record<string, Schem
360
485
  }
361
486
  declare const s: typeof Schema;
362
487
 
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 };
488
+ 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 };