@dzeio/schema 0.0.1
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 +332 -0
- package/dist/Schema.d.ts +332 -0
- package/dist/Schema.js +810 -0
- package/dist/Schema.mjs +769 -0
- package/package.json +42 -0
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
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
|
+
}>;
|
|
12
|
+
/**
|
|
13
|
+
* Function calls saved for serialization
|
|
14
|
+
*/
|
|
15
|
+
savedCalls: Array<{
|
|
16
|
+
name: string;
|
|
17
|
+
args: Array<string> | IArguments | undefined;
|
|
18
|
+
}>;
|
|
19
|
+
/**
|
|
20
|
+
* Pre process the variable for various reasons
|
|
21
|
+
*
|
|
22
|
+
* It will execute every pre-process sequentially in order of being added
|
|
23
|
+
*
|
|
24
|
+
* note: The type of the final Pre-Process MUST be valid
|
|
25
|
+
*/
|
|
26
|
+
preProcess: Array<(input: unknown) => Type | unknown>;
|
|
27
|
+
/**
|
|
28
|
+
* post process the variable after it being validated
|
|
29
|
+
*
|
|
30
|
+
* it will execute each post-processes sequentially
|
|
31
|
+
*
|
|
32
|
+
* note: the type of the final post-process MUST be valid
|
|
33
|
+
*/
|
|
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
|
+
private invalidError;
|
|
41
|
+
constructor(items?: Array<unknown> | IArguments);
|
|
42
|
+
attrs(...attributes: Array<string>): this;
|
|
43
|
+
attr(...attributes: Array<string>): this;
|
|
44
|
+
setInvalidError(err: string): this;
|
|
45
|
+
clone(): this;
|
|
46
|
+
/**
|
|
47
|
+
* schemas implementing unwrap can return their child component (mostly the value)
|
|
48
|
+
*
|
|
49
|
+
* ex: s.record(s.number(), s.string()) returns s.string()
|
|
50
|
+
*
|
|
51
|
+
* es2: s.nullable(s.string()) returns s.string()
|
|
52
|
+
*/
|
|
53
|
+
unwrap?(): SchemaItem;
|
|
54
|
+
parse(input: unknown, options?: {
|
|
55
|
+
fast?: boolean;
|
|
56
|
+
}): ValidationResult<Type>;
|
|
57
|
+
toJSON(): SchemaJSON;
|
|
58
|
+
protected addValidation(fn: ((input: Type) => boolean) | {
|
|
59
|
+
fn: (input: Type) => boolean;
|
|
60
|
+
error?: string;
|
|
61
|
+
}, error?: string): this;
|
|
62
|
+
protected addPreProcess(fn: (input: unknown) => Type | unknown): this;
|
|
63
|
+
protected addPostProcess(fn: (input: Type) => Type): this;
|
|
64
|
+
abstract isOfType(input: unknown): input is Type;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
type SchemaInfer<Type extends SchemaItem> = Type extends SchemaItem<infer X> ? X : never
|
|
68
|
+
type Infer<Type extends SchemaItem> = SchemaInfer<Type>
|
|
69
|
+
|
|
70
|
+
interface ValidationError {
|
|
71
|
+
message: string
|
|
72
|
+
field?: string
|
|
73
|
+
value?: unknown
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
type ValidationResult<T> = {
|
|
77
|
+
object: T
|
|
78
|
+
valid: true
|
|
79
|
+
errors?: undefined
|
|
80
|
+
} | {
|
|
81
|
+
valid: false
|
|
82
|
+
object?: (T extends object ? Partial<T> : T) | undefined
|
|
83
|
+
errors: Array<ValidationError>
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface SchemaJSON {
|
|
87
|
+
i: string
|
|
88
|
+
a?: Array<string> | undefined
|
|
89
|
+
c?: Array<unknown> | undefined
|
|
90
|
+
f?: Array<{
|
|
91
|
+
n: string
|
|
92
|
+
a?: Array<unknown> | undefined
|
|
93
|
+
}> | undefined
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @deprecated use `SchemaJSON`
|
|
98
|
+
*/
|
|
99
|
+
type SchemaItemJSON = SchemaJSON
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @deprecated use `Record<string, SchemaItem>`
|
|
103
|
+
*/
|
|
104
|
+
type Model = Record<string, SchemaItem>
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @deprecated
|
|
108
|
+
*/
|
|
109
|
+
type ModelInfer$1<M extends Model> = {
|
|
110
|
+
[key in keyof M]: SchemaInfer<M[key]>
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
declare class SchemaDate extends SchemaItem<Date> {
|
|
114
|
+
isOfType(input: unknown): input is Date;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
declare class SchemaRecord<Keys extends SchemaItem, Values extends SchemaItem> extends SchemaItem<Record<SchemaInfer<Keys>, SchemaInfer<Values>>> {
|
|
118
|
+
private readonly keys;
|
|
119
|
+
private readonly values;
|
|
120
|
+
constructor(keys: Keys, values: Values);
|
|
121
|
+
parse(input: unknown, options?: {
|
|
122
|
+
fast?: boolean;
|
|
123
|
+
}): ValidationResult<Record<SchemaInfer<Keys>, SchemaInfer<Values>>>;
|
|
124
|
+
isOfType(input: unknown): input is Record<SchemaInfer<Keys>, SchemaInfer<Values>>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
declare class SchemaArray<Type extends SchemaItem> extends SchemaItem<Array<SchemaInfer<Type>>> {
|
|
128
|
+
readonly values: Type;
|
|
129
|
+
constructor(values: Type);
|
|
130
|
+
/**
|
|
131
|
+
* transform the array so it only contains one of each elements
|
|
132
|
+
*/
|
|
133
|
+
unique(): this;
|
|
134
|
+
parse(input: unknown, options?: {
|
|
135
|
+
fast?: boolean;
|
|
136
|
+
}): ValidationResult<Array<SchemaInfer<Type>>>;
|
|
137
|
+
unwrap(): Type;
|
|
138
|
+
isOfType(input: unknown): input is Array<SchemaInfer<Type>>;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
declare class SchemaBoolean extends SchemaItem<boolean> {
|
|
142
|
+
/**
|
|
143
|
+
* @param [trueValue='true'] the truhty value (default to `'true'`)
|
|
144
|
+
* @param [falseValue='false'] the falthy value (default to `'false'`)
|
|
145
|
+
*/
|
|
146
|
+
parseString(trueValue?: string, falseValue?: string): this;
|
|
147
|
+
isOfType(input: unknown): input is boolean;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
interface EnumLike {
|
|
151
|
+
[k: string]: string | number;
|
|
152
|
+
[n: number]: string;
|
|
153
|
+
}
|
|
154
|
+
declare class SchemaEnum<E extends EnumLike> extends SchemaItem<E[keyof E]> {
|
|
155
|
+
private templateEnum;
|
|
156
|
+
private type;
|
|
157
|
+
constructor(templateEnum: E);
|
|
158
|
+
isOfType(input: unknown): input is E[keyof E];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
declare class SchemaLiteral<Type> extends SchemaItem<Type> {
|
|
162
|
+
private readonly value;
|
|
163
|
+
constructor(value: Type);
|
|
164
|
+
isOfType(input: unknown): input is Type;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
declare class SchemaNullable<Type extends SchemaItem> extends SchemaItem<SchemaInfer<Type> | undefined> {
|
|
168
|
+
readonly child: Type;
|
|
169
|
+
constructor(child: Type);
|
|
170
|
+
unwrap(): Type;
|
|
171
|
+
parse(input: unknown, options?: {
|
|
172
|
+
fast?: boolean;
|
|
173
|
+
}): ValidationResult<SchemaInfer<Type> | undefined>;
|
|
174
|
+
isOfType(input: unknown): input is SchemaInfer<Type> | undefined;
|
|
175
|
+
private isNull;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
declare class SchemaNumber extends SchemaItem<number> {
|
|
179
|
+
/**
|
|
180
|
+
* validate that the number is less or equal than {@link value}
|
|
181
|
+
* @param value the maxumum value (inclusive)
|
|
182
|
+
* @param message the message sent if not valid
|
|
183
|
+
*/
|
|
184
|
+
lte(value: number, message?: string): this;
|
|
185
|
+
/**
|
|
186
|
+
* validate that the number is more or equal than {@link value}
|
|
187
|
+
* @param value the minimum value (inclusive)
|
|
188
|
+
* @param message the message sent if not valid
|
|
189
|
+
*/
|
|
190
|
+
gte(value: number, message?: string): this;
|
|
191
|
+
/**
|
|
192
|
+
* validate that the number is less than {@link value}
|
|
193
|
+
* @param value the maxumum value (exclusive)
|
|
194
|
+
* @param message the message sent if not valid
|
|
195
|
+
*/
|
|
196
|
+
lt(value: number, message?: string): this;
|
|
197
|
+
/**
|
|
198
|
+
* validate that the number is more than {@link value}
|
|
199
|
+
* @param value the minimum value (exclusive)
|
|
200
|
+
* @param message the message sent if not valid
|
|
201
|
+
*/
|
|
202
|
+
gt(value: number, message?: string): this;
|
|
203
|
+
/**
|
|
204
|
+
* Try to parse strings before validating
|
|
205
|
+
*/
|
|
206
|
+
parseString(): this;
|
|
207
|
+
isOfType(input: unknown): input is number;
|
|
208
|
+
min(...params: Parameters<SchemaNumber['gte']>): this;
|
|
209
|
+
max(...params: Parameters<SchemaNumber['lte']>): this;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
type ModelInfer<M extends Record<string, SchemaItem>> = {
|
|
213
|
+
[key in keyof M]: SchemaInfer<M[key]>;
|
|
214
|
+
};
|
|
215
|
+
declare class SchemaObject<T extends Record<string, SchemaItem> = any> extends SchemaItem<ModelInfer<T>> {
|
|
216
|
+
readonly model: T;
|
|
217
|
+
id: string;
|
|
218
|
+
constructor(model: T);
|
|
219
|
+
parse(input: unknown, options?: {
|
|
220
|
+
fast?: boolean;
|
|
221
|
+
}): ValidationResult<ModelInfer<T>>;
|
|
222
|
+
isOfType(input: unknown): input is ModelInfer<T>;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
declare class SchemaString extends SchemaItem<string> {
|
|
226
|
+
/**
|
|
227
|
+
* force the input text to be a minimum of `value` size
|
|
228
|
+
* @param value the minimum length of the text
|
|
229
|
+
* @param message the message to display on an error
|
|
230
|
+
*/
|
|
231
|
+
min(value: number, message?: string): this;
|
|
232
|
+
/**
|
|
233
|
+
* transform the final text to the defined casing
|
|
234
|
+
* @param casing the final casing
|
|
235
|
+
*/
|
|
236
|
+
toCasing(casing: 'lower' | 'upper'): this;
|
|
237
|
+
/**
|
|
238
|
+
* force the input text to be a maximum of `value` size
|
|
239
|
+
* @param value the maximum length of the text
|
|
240
|
+
* @param message the message to display on an error
|
|
241
|
+
*/
|
|
242
|
+
max(value: number, message?: string): this;
|
|
243
|
+
/**
|
|
244
|
+
* the value must not be empty (`''`)
|
|
245
|
+
* @param message
|
|
246
|
+
* @returns
|
|
247
|
+
*/
|
|
248
|
+
notEmpty(message?: string): this;
|
|
249
|
+
/**
|
|
250
|
+
* force the input text to respect a Regexp
|
|
251
|
+
* @param regex the regex to validate against
|
|
252
|
+
* @param message the message to display on an error
|
|
253
|
+
*/
|
|
254
|
+
regex(regex: RegExp, message?: string): this;
|
|
255
|
+
minLength(value: number, message?: string): this;
|
|
256
|
+
maxLength(value: number, message?: string): this;
|
|
257
|
+
isOfType(input: unknown): input is string;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
type ItemType<T extends Array<SchemaItem>> = SchemaInfer<T[number]>;
|
|
261
|
+
declare class SchemaUnion<T extends Array<SchemaItem>> extends SchemaItem<ItemType<T>> {
|
|
262
|
+
private schemas;
|
|
263
|
+
constructor(...schemas: T);
|
|
264
|
+
parse(input: unknown, options?: {
|
|
265
|
+
fast?: boolean;
|
|
266
|
+
}): ValidationResult<SchemaInfer<T[number]>>;
|
|
267
|
+
isOfType(input: unknown): input is ItemType<T>;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
declare function parseQuery<T extends SchemaItem>(model: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
|
|
271
|
+
declare function parseFormData<T extends SchemaObject>(model: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
|
|
272
|
+
declare function parseForm<T extends SchemaObject>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
|
|
273
|
+
|
|
274
|
+
declare function parceable(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
|
|
275
|
+
type SchemaItemStatic = new (...args: Array<any>) => SchemaItem;
|
|
276
|
+
declare const Types: {
|
|
277
|
+
readonly Array: typeof SchemaArray;
|
|
278
|
+
readonly Boolean: typeof SchemaBoolean;
|
|
279
|
+
readonly Date: typeof SchemaDate;
|
|
280
|
+
readonly Enum: typeof SchemaEnum;
|
|
281
|
+
readonly Literal: typeof SchemaLiteral;
|
|
282
|
+
readonly Nullable: typeof SchemaNullable;
|
|
283
|
+
readonly Object: typeof SchemaObject;
|
|
284
|
+
readonly Record: typeof SchemaRecord;
|
|
285
|
+
readonly String: typeof SchemaString;
|
|
286
|
+
readonly Union: typeof SchemaUnion;
|
|
287
|
+
};
|
|
288
|
+
declare class Schema<T extends Record<string, SchemaItem> = Record<string, SchemaItem>> extends SchemaObject<T> {
|
|
289
|
+
private static registeredModules;
|
|
290
|
+
static register(module: SchemaItemStatic): void;
|
|
291
|
+
static getModule(name: string): SchemaItemStatic | undefined;
|
|
292
|
+
static array<Type extends SchemaItem>(...inputs: ConstructorParameters<typeof SchemaArray<Type>>): SchemaArray<Type>;
|
|
293
|
+
static boolean(...inputs: ConstructorParameters<typeof SchemaBoolean>): SchemaBoolean;
|
|
294
|
+
static date(...inputs: ConstructorParameters<typeof SchemaDate>): SchemaDate;
|
|
295
|
+
static enum<Type extends EnumLike>(...inputs: ConstructorParameters<typeof SchemaEnum<Type>>): SchemaEnum<Type>;
|
|
296
|
+
/**
|
|
297
|
+
*
|
|
298
|
+
* @param input the literal value (note: append `as const` else the typing won't work correctly)
|
|
299
|
+
* @returns
|
|
300
|
+
*/
|
|
301
|
+
static literal<Type>(input: Type): SchemaLiteral<Type>;
|
|
302
|
+
static nullable<Type extends SchemaItem>(...inputs: ConstructorParameters<typeof SchemaNullable<Type>>): SchemaNullable<Type>;
|
|
303
|
+
static number(...inputs: ConstructorParameters<typeof SchemaNumber>): SchemaNumber;
|
|
304
|
+
static object<Type extends Record<string, SchemaItem>>(...inputs: ConstructorParameters<typeof SchemaObject<Type>>): SchemaObject<Type>;
|
|
305
|
+
static record<Keys extends SchemaItem, Values extends SchemaItem>(...inputs: ConstructorParameters<typeof SchemaRecord<Keys, Values>>): SchemaRecord<Keys, Values>;
|
|
306
|
+
/**
|
|
307
|
+
* See {@link SchemaString}
|
|
308
|
+
*/
|
|
309
|
+
static string(...inputs: ConstructorParameters<typeof SchemaString>): SchemaString;
|
|
310
|
+
static union<Type extends Array<SchemaItem>>(...inputs: ConstructorParameters<typeof SchemaUnion<Type>>): SchemaUnion<Type>;
|
|
311
|
+
static fromJSON(json: SchemaJSON): SchemaItem;
|
|
312
|
+
static isSchemaJSON(data: unknown): data is SchemaJSON;
|
|
313
|
+
/**
|
|
314
|
+
* @deprecated use helper `parseQuery`
|
|
315
|
+
*/
|
|
316
|
+
validateQuery(query: URLSearchParams, fast?: boolean): ReturnType<this["parse"]>;
|
|
317
|
+
/**
|
|
318
|
+
* @deprecated use `parse`
|
|
319
|
+
*/
|
|
320
|
+
validate(input: unknown, fast?: boolean): ValidationResult<{ [key in keyof T]: SchemaInfer<T[key]>; }>;
|
|
321
|
+
/**
|
|
322
|
+
* @deprecated use helper `parseForm`
|
|
323
|
+
*/
|
|
324
|
+
validateForm(form: HTMLFormElement, fast?: boolean): ReturnType<this["parse"]>;
|
|
325
|
+
/**
|
|
326
|
+
* @deprecated use helper `parseFormData`
|
|
327
|
+
*/
|
|
328
|
+
validateFormData(data: FormData, fast?: boolean): ReturnType<this["parse"]>;
|
|
329
|
+
}
|
|
330
|
+
declare const s: typeof Schema;
|
|
331
|
+
|
|
332
|
+
export { type Infer, type Model, type ModelInfer$1 as ModelInfer, SchemaArray, SchemaBoolean, SchemaDate, SchemaEnum, type SchemaInfer, SchemaItem, type SchemaItemJSON, type SchemaJSON, SchemaLiteral, SchemaNullable, SchemaNumber, SchemaObject, SchemaRecord, SchemaString, SchemaUnion, Types, type ValidationError, type ValidationResult, Schema as default, parceable, parseForm, parseFormData, parseQuery, s };
|
package/dist/Schema.d.ts
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
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
|
+
}>;
|
|
12
|
+
/**
|
|
13
|
+
* Function calls saved for serialization
|
|
14
|
+
*/
|
|
15
|
+
savedCalls: Array<{
|
|
16
|
+
name: string;
|
|
17
|
+
args: Array<string> | IArguments | undefined;
|
|
18
|
+
}>;
|
|
19
|
+
/**
|
|
20
|
+
* Pre process the variable for various reasons
|
|
21
|
+
*
|
|
22
|
+
* It will execute every pre-process sequentially in order of being added
|
|
23
|
+
*
|
|
24
|
+
* note: The type of the final Pre-Process MUST be valid
|
|
25
|
+
*/
|
|
26
|
+
preProcess: Array<(input: unknown) => Type | unknown>;
|
|
27
|
+
/**
|
|
28
|
+
* post process the variable after it being validated
|
|
29
|
+
*
|
|
30
|
+
* it will execute each post-processes sequentially
|
|
31
|
+
*
|
|
32
|
+
* note: the type of the final post-process MUST be valid
|
|
33
|
+
*/
|
|
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
|
+
private invalidError;
|
|
41
|
+
constructor(items?: Array<unknown> | IArguments);
|
|
42
|
+
attrs(...attributes: Array<string>): this;
|
|
43
|
+
attr(...attributes: Array<string>): this;
|
|
44
|
+
setInvalidError(err: string): this;
|
|
45
|
+
clone(): this;
|
|
46
|
+
/**
|
|
47
|
+
* schemas implementing unwrap can return their child component (mostly the value)
|
|
48
|
+
*
|
|
49
|
+
* ex: s.record(s.number(), s.string()) returns s.string()
|
|
50
|
+
*
|
|
51
|
+
* es2: s.nullable(s.string()) returns s.string()
|
|
52
|
+
*/
|
|
53
|
+
unwrap?(): SchemaItem;
|
|
54
|
+
parse(input: unknown, options?: {
|
|
55
|
+
fast?: boolean;
|
|
56
|
+
}): ValidationResult<Type>;
|
|
57
|
+
toJSON(): SchemaJSON;
|
|
58
|
+
protected addValidation(fn: ((input: Type) => boolean) | {
|
|
59
|
+
fn: (input: Type) => boolean;
|
|
60
|
+
error?: string;
|
|
61
|
+
}, error?: string): this;
|
|
62
|
+
protected addPreProcess(fn: (input: unknown) => Type | unknown): this;
|
|
63
|
+
protected addPostProcess(fn: (input: Type) => Type): this;
|
|
64
|
+
abstract isOfType(input: unknown): input is Type;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
type SchemaInfer<Type extends SchemaItem> = Type extends SchemaItem<infer X> ? X : never
|
|
68
|
+
type Infer<Type extends SchemaItem> = SchemaInfer<Type>
|
|
69
|
+
|
|
70
|
+
interface ValidationError {
|
|
71
|
+
message: string
|
|
72
|
+
field?: string
|
|
73
|
+
value?: unknown
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
type ValidationResult<T> = {
|
|
77
|
+
object: T
|
|
78
|
+
valid: true
|
|
79
|
+
errors?: undefined
|
|
80
|
+
} | {
|
|
81
|
+
valid: false
|
|
82
|
+
object?: (T extends object ? Partial<T> : T) | undefined
|
|
83
|
+
errors: Array<ValidationError>
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface SchemaJSON {
|
|
87
|
+
i: string
|
|
88
|
+
a?: Array<string> | undefined
|
|
89
|
+
c?: Array<unknown> | undefined
|
|
90
|
+
f?: Array<{
|
|
91
|
+
n: string
|
|
92
|
+
a?: Array<unknown> | undefined
|
|
93
|
+
}> | undefined
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @deprecated use `SchemaJSON`
|
|
98
|
+
*/
|
|
99
|
+
type SchemaItemJSON = SchemaJSON
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @deprecated use `Record<string, SchemaItem>`
|
|
103
|
+
*/
|
|
104
|
+
type Model = Record<string, SchemaItem>
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @deprecated
|
|
108
|
+
*/
|
|
109
|
+
type ModelInfer$1<M extends Model> = {
|
|
110
|
+
[key in keyof M]: SchemaInfer<M[key]>
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
declare class SchemaDate extends SchemaItem<Date> {
|
|
114
|
+
isOfType(input: unknown): input is Date;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
declare class SchemaRecord<Keys extends SchemaItem, Values extends SchemaItem> extends SchemaItem<Record<SchemaInfer<Keys>, SchemaInfer<Values>>> {
|
|
118
|
+
private readonly keys;
|
|
119
|
+
private readonly values;
|
|
120
|
+
constructor(keys: Keys, values: Values);
|
|
121
|
+
parse(input: unknown, options?: {
|
|
122
|
+
fast?: boolean;
|
|
123
|
+
}): ValidationResult<Record<SchemaInfer<Keys>, SchemaInfer<Values>>>;
|
|
124
|
+
isOfType(input: unknown): input is Record<SchemaInfer<Keys>, SchemaInfer<Values>>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
declare class SchemaArray<Type extends SchemaItem> extends SchemaItem<Array<SchemaInfer<Type>>> {
|
|
128
|
+
readonly values: Type;
|
|
129
|
+
constructor(values: Type);
|
|
130
|
+
/**
|
|
131
|
+
* transform the array so it only contains one of each elements
|
|
132
|
+
*/
|
|
133
|
+
unique(): this;
|
|
134
|
+
parse(input: unknown, options?: {
|
|
135
|
+
fast?: boolean;
|
|
136
|
+
}): ValidationResult<Array<SchemaInfer<Type>>>;
|
|
137
|
+
unwrap(): Type;
|
|
138
|
+
isOfType(input: unknown): input is Array<SchemaInfer<Type>>;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
declare class SchemaBoolean extends SchemaItem<boolean> {
|
|
142
|
+
/**
|
|
143
|
+
* @param [trueValue='true'] the truhty value (default to `'true'`)
|
|
144
|
+
* @param [falseValue='false'] the falthy value (default to `'false'`)
|
|
145
|
+
*/
|
|
146
|
+
parseString(trueValue?: string, falseValue?: string): this;
|
|
147
|
+
isOfType(input: unknown): input is boolean;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
interface EnumLike {
|
|
151
|
+
[k: string]: string | number;
|
|
152
|
+
[n: number]: string;
|
|
153
|
+
}
|
|
154
|
+
declare class SchemaEnum<E extends EnumLike> extends SchemaItem<E[keyof E]> {
|
|
155
|
+
private templateEnum;
|
|
156
|
+
private type;
|
|
157
|
+
constructor(templateEnum: E);
|
|
158
|
+
isOfType(input: unknown): input is E[keyof E];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
declare class SchemaLiteral<Type> extends SchemaItem<Type> {
|
|
162
|
+
private readonly value;
|
|
163
|
+
constructor(value: Type);
|
|
164
|
+
isOfType(input: unknown): input is Type;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
declare class SchemaNullable<Type extends SchemaItem> extends SchemaItem<SchemaInfer<Type> | undefined> {
|
|
168
|
+
readonly child: Type;
|
|
169
|
+
constructor(child: Type);
|
|
170
|
+
unwrap(): Type;
|
|
171
|
+
parse(input: unknown, options?: {
|
|
172
|
+
fast?: boolean;
|
|
173
|
+
}): ValidationResult<SchemaInfer<Type> | undefined>;
|
|
174
|
+
isOfType(input: unknown): input is SchemaInfer<Type> | undefined;
|
|
175
|
+
private isNull;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
declare class SchemaNumber extends SchemaItem<number> {
|
|
179
|
+
/**
|
|
180
|
+
* validate that the number is less or equal than {@link value}
|
|
181
|
+
* @param value the maxumum value (inclusive)
|
|
182
|
+
* @param message the message sent if not valid
|
|
183
|
+
*/
|
|
184
|
+
lte(value: number, message?: string): this;
|
|
185
|
+
/**
|
|
186
|
+
* validate that the number is more or equal than {@link value}
|
|
187
|
+
* @param value the minimum value (inclusive)
|
|
188
|
+
* @param message the message sent if not valid
|
|
189
|
+
*/
|
|
190
|
+
gte(value: number, message?: string): this;
|
|
191
|
+
/**
|
|
192
|
+
* validate that the number is less than {@link value}
|
|
193
|
+
* @param value the maxumum value (exclusive)
|
|
194
|
+
* @param message the message sent if not valid
|
|
195
|
+
*/
|
|
196
|
+
lt(value: number, message?: string): this;
|
|
197
|
+
/**
|
|
198
|
+
* validate that the number is more than {@link value}
|
|
199
|
+
* @param value the minimum value (exclusive)
|
|
200
|
+
* @param message the message sent if not valid
|
|
201
|
+
*/
|
|
202
|
+
gt(value: number, message?: string): this;
|
|
203
|
+
/**
|
|
204
|
+
* Try to parse strings before validating
|
|
205
|
+
*/
|
|
206
|
+
parseString(): this;
|
|
207
|
+
isOfType(input: unknown): input is number;
|
|
208
|
+
min(...params: Parameters<SchemaNumber['gte']>): this;
|
|
209
|
+
max(...params: Parameters<SchemaNumber['lte']>): this;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
type ModelInfer<M extends Record<string, SchemaItem>> = {
|
|
213
|
+
[key in keyof M]: SchemaInfer<M[key]>;
|
|
214
|
+
};
|
|
215
|
+
declare class SchemaObject<T extends Record<string, SchemaItem> = any> extends SchemaItem<ModelInfer<T>> {
|
|
216
|
+
readonly model: T;
|
|
217
|
+
id: string;
|
|
218
|
+
constructor(model: T);
|
|
219
|
+
parse(input: unknown, options?: {
|
|
220
|
+
fast?: boolean;
|
|
221
|
+
}): ValidationResult<ModelInfer<T>>;
|
|
222
|
+
isOfType(input: unknown): input is ModelInfer<T>;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
declare class SchemaString extends SchemaItem<string> {
|
|
226
|
+
/**
|
|
227
|
+
* force the input text to be a minimum of `value` size
|
|
228
|
+
* @param value the minimum length of the text
|
|
229
|
+
* @param message the message to display on an error
|
|
230
|
+
*/
|
|
231
|
+
min(value: number, message?: string): this;
|
|
232
|
+
/**
|
|
233
|
+
* transform the final text to the defined casing
|
|
234
|
+
* @param casing the final casing
|
|
235
|
+
*/
|
|
236
|
+
toCasing(casing: 'lower' | 'upper'): this;
|
|
237
|
+
/**
|
|
238
|
+
* force the input text to be a maximum of `value` size
|
|
239
|
+
* @param value the maximum length of the text
|
|
240
|
+
* @param message the message to display on an error
|
|
241
|
+
*/
|
|
242
|
+
max(value: number, message?: string): this;
|
|
243
|
+
/**
|
|
244
|
+
* the value must not be empty (`''`)
|
|
245
|
+
* @param message
|
|
246
|
+
* @returns
|
|
247
|
+
*/
|
|
248
|
+
notEmpty(message?: string): this;
|
|
249
|
+
/**
|
|
250
|
+
* force the input text to respect a Regexp
|
|
251
|
+
* @param regex the regex to validate against
|
|
252
|
+
* @param message the message to display on an error
|
|
253
|
+
*/
|
|
254
|
+
regex(regex: RegExp, message?: string): this;
|
|
255
|
+
minLength(value: number, message?: string): this;
|
|
256
|
+
maxLength(value: number, message?: string): this;
|
|
257
|
+
isOfType(input: unknown): input is string;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
type ItemType<T extends Array<SchemaItem>> = SchemaInfer<T[number]>;
|
|
261
|
+
declare class SchemaUnion<T extends Array<SchemaItem>> extends SchemaItem<ItemType<T>> {
|
|
262
|
+
private schemas;
|
|
263
|
+
constructor(...schemas: T);
|
|
264
|
+
parse(input: unknown, options?: {
|
|
265
|
+
fast?: boolean;
|
|
266
|
+
}): ValidationResult<SchemaInfer<T[number]>>;
|
|
267
|
+
isOfType(input: unknown): input is ItemType<T>;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
declare function parseQuery<T extends SchemaItem>(model: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
|
|
271
|
+
declare function parseFormData<T extends SchemaObject>(model: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
|
|
272
|
+
declare function parseForm<T extends SchemaObject>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
|
|
273
|
+
|
|
274
|
+
declare function parceable(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
|
|
275
|
+
type SchemaItemStatic = new (...args: Array<any>) => SchemaItem;
|
|
276
|
+
declare const Types: {
|
|
277
|
+
readonly Array: typeof SchemaArray;
|
|
278
|
+
readonly Boolean: typeof SchemaBoolean;
|
|
279
|
+
readonly Date: typeof SchemaDate;
|
|
280
|
+
readonly Enum: typeof SchemaEnum;
|
|
281
|
+
readonly Literal: typeof SchemaLiteral;
|
|
282
|
+
readonly Nullable: typeof SchemaNullable;
|
|
283
|
+
readonly Object: typeof SchemaObject;
|
|
284
|
+
readonly Record: typeof SchemaRecord;
|
|
285
|
+
readonly String: typeof SchemaString;
|
|
286
|
+
readonly Union: typeof SchemaUnion;
|
|
287
|
+
};
|
|
288
|
+
declare class Schema<T extends Record<string, SchemaItem> = Record<string, SchemaItem>> extends SchemaObject<T> {
|
|
289
|
+
private static registeredModules;
|
|
290
|
+
static register(module: SchemaItemStatic): void;
|
|
291
|
+
static getModule(name: string): SchemaItemStatic | undefined;
|
|
292
|
+
static array<Type extends SchemaItem>(...inputs: ConstructorParameters<typeof SchemaArray<Type>>): SchemaArray<Type>;
|
|
293
|
+
static boolean(...inputs: ConstructorParameters<typeof SchemaBoolean>): SchemaBoolean;
|
|
294
|
+
static date(...inputs: ConstructorParameters<typeof SchemaDate>): SchemaDate;
|
|
295
|
+
static enum<Type extends EnumLike>(...inputs: ConstructorParameters<typeof SchemaEnum<Type>>): SchemaEnum<Type>;
|
|
296
|
+
/**
|
|
297
|
+
*
|
|
298
|
+
* @param input the literal value (note: append `as const` else the typing won't work correctly)
|
|
299
|
+
* @returns
|
|
300
|
+
*/
|
|
301
|
+
static literal<Type>(input: Type): SchemaLiteral<Type>;
|
|
302
|
+
static nullable<Type extends SchemaItem>(...inputs: ConstructorParameters<typeof SchemaNullable<Type>>): SchemaNullable<Type>;
|
|
303
|
+
static number(...inputs: ConstructorParameters<typeof SchemaNumber>): SchemaNumber;
|
|
304
|
+
static object<Type extends Record<string, SchemaItem>>(...inputs: ConstructorParameters<typeof SchemaObject<Type>>): SchemaObject<Type>;
|
|
305
|
+
static record<Keys extends SchemaItem, Values extends SchemaItem>(...inputs: ConstructorParameters<typeof SchemaRecord<Keys, Values>>): SchemaRecord<Keys, Values>;
|
|
306
|
+
/**
|
|
307
|
+
* See {@link SchemaString}
|
|
308
|
+
*/
|
|
309
|
+
static string(...inputs: ConstructorParameters<typeof SchemaString>): SchemaString;
|
|
310
|
+
static union<Type extends Array<SchemaItem>>(...inputs: ConstructorParameters<typeof SchemaUnion<Type>>): SchemaUnion<Type>;
|
|
311
|
+
static fromJSON(json: SchemaJSON): SchemaItem;
|
|
312
|
+
static isSchemaJSON(data: unknown): data is SchemaJSON;
|
|
313
|
+
/**
|
|
314
|
+
* @deprecated use helper `parseQuery`
|
|
315
|
+
*/
|
|
316
|
+
validateQuery(query: URLSearchParams, fast?: boolean): ReturnType<this["parse"]>;
|
|
317
|
+
/**
|
|
318
|
+
* @deprecated use `parse`
|
|
319
|
+
*/
|
|
320
|
+
validate(input: unknown, fast?: boolean): ValidationResult<{ [key in keyof T]: SchemaInfer<T[key]>; }>;
|
|
321
|
+
/**
|
|
322
|
+
* @deprecated use helper `parseForm`
|
|
323
|
+
*/
|
|
324
|
+
validateForm(form: HTMLFormElement, fast?: boolean): ReturnType<this["parse"]>;
|
|
325
|
+
/**
|
|
326
|
+
* @deprecated use helper `parseFormData`
|
|
327
|
+
*/
|
|
328
|
+
validateFormData(data: FormData, fast?: boolean): ReturnType<this["parse"]>;
|
|
329
|
+
}
|
|
330
|
+
declare const s: typeof Schema;
|
|
331
|
+
|
|
332
|
+
export { type Infer, type Model, type ModelInfer$1 as ModelInfer, SchemaArray, SchemaBoolean, SchemaDate, SchemaEnum, type SchemaInfer, SchemaItem, type SchemaItemJSON, type SchemaJSON, SchemaLiteral, SchemaNullable, SchemaNumber, SchemaObject, SchemaRecord, SchemaString, SchemaUnion, Types, type ValidationError, type ValidationResult, Schema as default, parceable, parseForm, parseFormData, parseQuery, s };
|