@aomex/common-validator 3.30.7 → 3.30.8-alpha.131cf1c.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/index.d.ts +1180 -858
- package/dist/index.js +1536 -1477
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,871 +1,1193 @@
|
|
|
1
|
-
import { OpenAPI, Union2Intersection } from
|
|
2
|
-
import
|
|
3
|
-
import stream
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
type Any<T> = Accepted<T> | Denied;
|
|
7
|
-
interface Accepted<T> {
|
|
8
|
-
data: T;
|
|
9
|
-
}
|
|
10
|
-
interface Denied {
|
|
11
|
-
errors: string[];
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
declare const ValidateResult: {
|
|
15
|
-
accept: <T>(data: T) => ValidateResult.Accepted<T>;
|
|
16
|
-
deny: (message: string) => ValidateResult.Denied;
|
|
17
|
-
noError: <T>(result: ValidateResult.Any<T>) => result is ValidateResult.Accepted<T>;
|
|
1
|
+
import { OpenAPI, Union2Intersection } from "@aomex/internal-tools";
|
|
2
|
+
import Decimal from "decimal.js";
|
|
3
|
+
import stream from "node:stream";
|
|
4
|
+
type HasPublicOptional = {
|
|
5
|
+
optional: () => any;
|
|
18
6
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
declare namespace Validator {
|
|
23
|
-
class TDefault {
|
|
24
|
-
private _DEF_;
|
|
25
|
-
}
|
|
26
|
-
class TOptional {
|
|
27
|
-
private _OPT_;
|
|
28
|
-
}
|
|
29
|
-
class TObject {
|
|
30
|
-
private _OBJ_;
|
|
31
|
-
}
|
|
32
|
-
type ConvertOptional<T> = TDefault extends T ? ConvertOptionalTo<T, never> : ConvertOptionalTo<T, undefined>;
|
|
33
|
-
type ConvertOptionalTo<T, K extends undefined | never> = T extends TOptional ? K : T extends TDefault ? never : T extends TObject ? {
|
|
34
|
-
[K: string]: unknown;
|
|
35
|
-
} : T;
|
|
36
|
-
interface TransformFn<T, T1> {
|
|
37
|
-
(data: ConvertOptional<T>): Promise<T1> | T1;
|
|
38
|
-
}
|
|
39
|
-
type ParameterOrFn<T> = ConvertOptionalTo<T, never> | (() => ConvertOptionalTo<T, never>);
|
|
40
|
-
type Infer<T> = T extends Validator<infer Type> ? Validator.ConvertOptional<Type> : T extends {
|
|
41
|
-
[K: string]: Validator;
|
|
42
|
-
} ? keyof T extends never ? {} : EmptyObjectToUnknown<{
|
|
43
|
-
[K in keyof T as true extends HasUndefined<T[K]> ? never : K]: Infer<T[K]>;
|
|
44
|
-
}> & EmptyObjectToUnknown<{
|
|
45
|
-
[K in keyof T as true extends HasUndefined<T[K]> ? K : never]?: Infer<T[K]>;
|
|
46
|
-
}> : never;
|
|
47
|
-
type EmptyObjectToUnknown<T extends object> = keyof T extends never ? unknown : T;
|
|
48
|
-
type HasUndefined<T> = T extends Validator<infer Type> ? undefined extends Validator.ConvertOptional<Type> ? true : false : false;
|
|
49
|
-
type HasNull<T> = T extends Validator<infer Type> ? (null extends Type ? true : false) : false;
|
|
50
|
-
type HasDefault<T> = T extends Validator<infer Type> ? Validator.TDefault extends Type ? true : false : false;
|
|
51
|
-
type PartialOpenAPISchema = Pick<OpenAPI.SchemaObject, 'title' | 'description' | 'deprecated' | 'example' | 'externalDocs'>;
|
|
52
|
-
type DocumentMergeMode = 'merge' | 'replace';
|
|
53
|
-
interface Options<Type> {
|
|
54
|
-
defaultValue?: Type | (() => Type);
|
|
55
|
-
required: boolean;
|
|
56
|
-
nullable: boolean;
|
|
57
|
-
transform?: (value: any) => Promise<any> | any;
|
|
58
|
-
docs?: PartialOpenAPISchema;
|
|
59
|
-
strict: boolean;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
declare abstract class Validator<T = unknown> {
|
|
63
|
-
protected readonly SubClass: new (...args: any[]) => Validator;
|
|
64
|
-
constructor();
|
|
65
|
-
protected readonly config: Validator.Options<T>;
|
|
66
|
-
static toDocument(validator: Validator): OpenAPI.ParameterBaseObject;
|
|
67
|
-
/**
|
|
68
|
-
* @param is 是否开启,默认值:`true`
|
|
69
|
-
*/
|
|
70
|
-
protected strict(is?: boolean): this;
|
|
71
|
-
/**
|
|
72
|
-
* 扩展openapi的配置
|
|
73
|
-
* - **replace:** 本次设置直接覆盖原来的扩展配置
|
|
74
|
-
* - **merge:** 本次设置通过`Object.assign`的形式合并到原来的扩展配置
|
|
75
|
-
* @param mode 默认值:`merge`
|
|
76
|
-
*/
|
|
77
|
-
protected docs(docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode): this;
|
|
78
|
-
/**
|
|
79
|
-
* 值如果是 `undefined, null, ''`或者没传入,以上情况会直接转换成`undefined`
|
|
80
|
-
*
|
|
81
|
-
* 注意:如果执行了`default(...)`,则无需再执行`optional()`
|
|
82
|
-
*
|
|
83
|
-
* 注意:如果设置了`nullable()`,则`null`不做处理
|
|
84
|
-
*/
|
|
85
|
-
protected optional(): Validator;
|
|
86
|
-
/**
|
|
87
|
-
* 把`null`识别成合法的值
|
|
88
|
-
* @see optional
|
|
89
|
-
*/
|
|
90
|
-
protected nullable(): Validator;
|
|
91
|
-
/**
|
|
92
|
-
* 数据验证成功最后会触发该方法,你可以把数据转换成任何格式
|
|
93
|
-
* ```typescript
|
|
94
|
-
* // 最终返回类型:string
|
|
95
|
-
* rule.string()
|
|
96
|
-
* // 最终返回类型:Array<string>
|
|
97
|
-
* rule.string().transform((value) => [value])
|
|
98
|
-
* ```
|
|
99
|
-
*/
|
|
100
|
-
protected transform<T1>(fn: (value: any) => Promise<T1> | T1): Validator;
|
|
101
|
-
/**
|
|
102
|
-
* 如果值为空或者没传入,则使用该默认值,而且无需验证
|
|
103
|
-
*
|
|
104
|
-
* 注意:如果执行了`default(...)`,则无需再执行`optional()`
|
|
105
|
-
*/
|
|
106
|
-
protected default(value: any): Validator;
|
|
107
|
-
protected validate(value: any, key?: string, label?: string): Promise<ValidateResult.Any<any>>;
|
|
108
|
-
protected isEmpty(value: any): boolean;
|
|
109
|
-
protected isValidNull(value: any): boolean;
|
|
110
|
-
protected getDefaultValue<T = any>(value: any): T | undefined;
|
|
111
|
-
protected abstract validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>> | ValidateResult.Any<any>;
|
|
112
|
-
protected copy(): Validator;
|
|
113
|
-
protected copyConfig(prev: Validator): this;
|
|
114
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
declare namespace BaseStringValidator {
|
|
118
|
-
interface Options<T> extends Validator.Options<T> {
|
|
119
|
-
trim?: boolean;
|
|
120
|
-
pattern?: RegExp;
|
|
121
|
-
lengthRange?: LengthRange;
|
|
122
|
-
}
|
|
123
|
-
interface LengthRange {
|
|
124
|
-
min?: number;
|
|
125
|
-
max?: number;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
declare abstract class BaseStringValidator<T = string> extends Validator<T> {
|
|
129
|
-
protected config: BaseStringValidator.Options<T>;
|
|
130
|
-
/**
|
|
131
|
-
* 删除两边空格后再进行验证
|
|
132
|
-
*/
|
|
133
|
-
protected trim(is?: boolean): this;
|
|
134
|
-
protected length(exactLength: number): this;
|
|
135
|
-
protected length(range: BaseStringValidator.LengthRange): this;
|
|
136
|
-
protected match(pattern: RegExp): this;
|
|
137
|
-
protected isEmpty(value: any): boolean;
|
|
138
|
-
protected getTrimValue(value: string): string;
|
|
139
|
-
protected validateValue(value: any, key: string, label: string): ValidateResult.Any<any> | Promise<ValidateResult.Any<any>>;
|
|
140
|
-
protected abstract validateString(value: string, key: string, label: string): ValidateResult.Any<any> | Promise<ValidateResult.Any<any>>;
|
|
141
|
-
protected copy: () => BaseStringValidator<T>;
|
|
142
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
declare namespace BaseNumberValidator {
|
|
146
|
-
interface Options<T = false> extends Validator.Options<T> {
|
|
147
|
-
min?: number;
|
|
148
|
-
minInclusive?: boolean;
|
|
149
|
-
max?: number;
|
|
150
|
-
maxInclusive?: boolean;
|
|
151
|
-
precision?: number;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
declare abstract class BaseNumberValidator<T = number> extends Validator<T> {
|
|
155
|
-
protected config: BaseNumberValidator.Options<T>;
|
|
156
|
-
/**
|
|
157
|
-
* 开启/关闭严格模式,开启后有以下限制:
|
|
158
|
-
* - 字符串不再尝试转换为数字
|
|
159
|
-
*/
|
|
160
|
-
strict: (is?: boolean) => this;
|
|
161
|
-
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
162
|
-
/**
|
|
163
|
-
* 最小范围限制
|
|
164
|
-
* @param min 最小值
|
|
165
|
-
* @param inclusive 是否包含最小值,默认值:`true`
|
|
166
|
-
*/
|
|
167
|
-
min(min: number, inclusive?: boolean): this;
|
|
168
|
-
/**
|
|
169
|
-
* 最大范围限制
|
|
170
|
-
* @param min 最大值
|
|
171
|
-
* @param inclusive 是否包含最大值,默认值:`true`
|
|
172
|
-
*/
|
|
173
|
-
max(max: number, inclusive?: boolean): this;
|
|
174
|
-
/**
|
|
175
|
-
* 小数点保留位数,使用`.toFixed()`剔除多余的小数
|
|
176
|
-
*/
|
|
177
|
-
protected precision(decimals: number): this;
|
|
178
|
-
protected validateValue(num: number, key: string, label: string): ValidateResult.Any<number>;
|
|
179
|
-
protected abstract validateNumber(num: number, key: string, label: string): ValidateResult.Any<number>;
|
|
180
|
-
protected copy: () => BaseNumberValidator<T>;
|
|
181
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
declare class ValidateDeniedError extends Error {
|
|
185
|
-
protected readonly errors: ValidateResult.Denied['errors'];
|
|
186
|
-
constructor(message: string | undefined, errors?: ValidateResult.Denied['errors']);
|
|
187
|
-
getValidateErrors(): string[];
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
type ValidatorToken = Validator | {
|
|
191
|
-
[key: string]: Validator;
|
|
7
|
+
type HasPublicRequired = {
|
|
8
|
+
required: () => any;
|
|
192
9
|
};
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
declare namespace
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
setFalsyValues(values: any[]): BooleanValidator<T>;
|
|
351
|
-
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<boolean>;
|
|
352
|
-
protected copy: () => BooleanValidator<T>;
|
|
353
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
declare namespace BufferValidator {
|
|
357
|
-
interface Options<T = Buffer> extends Validator.Options<T> {
|
|
358
|
-
fromEncodings?: Encodings[];
|
|
359
|
-
}
|
|
360
|
-
type Encodings = 'base64' | 'hex';
|
|
361
|
-
}
|
|
362
|
-
declare class BufferValidator<T = Buffer> extends Validator<T> {
|
|
363
|
-
protected config: BufferValidator.Options<T>;
|
|
364
|
-
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
365
|
-
optional: () => BufferValidator<T | Validator.TOptional>;
|
|
366
|
-
nullable: () => BufferValidator<T | null>;
|
|
367
|
-
default: (buffer: Buffer) => BufferValidator<T | Validator.TDefault>;
|
|
368
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
369
|
-
/**
|
|
370
|
-
* 尝试从其他类型恢复为buffer类型:
|
|
371
|
-
*
|
|
372
|
-
* - `hex` 从十六进制字符串恢复
|
|
373
|
-
* - `base64` 从base64字符串恢复
|
|
374
|
-
*/
|
|
375
|
-
parseFrom(...encodings: BufferValidator.Encodings[]): this;
|
|
376
|
-
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<Buffer>;
|
|
377
|
-
protected copy: () => this;
|
|
378
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
declare namespace DateValidator {
|
|
382
|
-
interface Options<T = Date> extends Validator.Options<T> {
|
|
383
|
-
min?: () => Date;
|
|
384
|
-
minInclusive?: boolean;
|
|
385
|
-
max?: () => Date;
|
|
386
|
-
maxInclusive?: boolean;
|
|
387
|
-
parseFromTimestamp?: boolean;
|
|
388
|
-
formats?: string[];
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
declare class DateValidator<T = Date> extends Validator<T> {
|
|
392
|
-
protected config: DateValidator.Options<T>;
|
|
393
|
-
constructor(formats?: string[]);
|
|
394
|
-
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
395
|
-
optional: () => DateValidator<T | Validator.TOptional>;
|
|
396
|
-
nullable: () => DateValidator<T | null>;
|
|
397
|
-
default: (date: Validator.ParameterOrFn<Date>) => DateValidator<T | Validator.TDefault>;
|
|
398
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
399
|
-
min(freshDate: () => Date, inclusive?: boolean): DateValidator<T>;
|
|
400
|
-
max(freshDate: () => Date, inclusive?: boolean): DateValidator<T>;
|
|
401
|
-
/**
|
|
402
|
-
* 尝试把时间戳数字解析成时间对象。默认已开启
|
|
403
|
-
*
|
|
404
|
-
* 支持如下格式:
|
|
405
|
-
* - 13位:1711257956199
|
|
406
|
-
* - 14位:1711257956.199
|
|
407
|
-
* - 10位:1711257956
|
|
408
|
-
*/
|
|
409
|
-
parseFromTimestamp(is?: boolean): this;
|
|
410
|
-
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<Date>;
|
|
411
|
-
protected toDate(value: any): false | Date;
|
|
412
|
-
protected compare(date: Date): boolean;
|
|
413
|
-
protected copy: () => this;
|
|
414
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
declare namespace EmailValidator {
|
|
418
|
-
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
declare class EmailValidator<T = string> extends BaseStringValidator<T> {
|
|
422
|
-
protected config: EmailValidator.Options<T>;
|
|
423
|
-
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
424
|
-
optional: () => EmailValidator<T | Validator.TOptional>;
|
|
425
|
-
nullable: () => EmailValidator<T | null>;
|
|
426
|
-
default: (email: Validator.ParameterOrFn<T>) => EmailValidator<T | Validator.TDefault>;
|
|
427
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
428
|
-
match: (pattern: RegExp) => this;
|
|
429
|
-
protected validateString(email: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
430
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
declare namespace EnumValidator {
|
|
434
|
-
interface Options<T> extends Validator.Options<T> {
|
|
435
|
-
ranges: T[];
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
declare class EnumValidator<const T = never> extends Validator<T> {
|
|
439
|
-
protected config: EnumValidator.Options<T>;
|
|
440
|
-
constructor(ranges?: T[]);
|
|
441
|
-
/**
|
|
442
|
-
* 开启/关闭严格模式。开启后有如下限制:
|
|
443
|
-
* - 枚举中包含数字时,如果传递的值为字符串类型,则不再转换为数字后再对比
|
|
444
|
-
* - 枚举中包含布尔值时,如果传递的值为字符串类型,则不再转换为布尔值后再对比
|
|
445
|
-
*/
|
|
446
|
-
strict: (is?: boolean) => this;
|
|
447
|
-
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
448
|
-
optional: () => EnumValidator<T | Validator.TOptional>;
|
|
449
|
-
nullable: () => EnumValidator<T | null>;
|
|
450
|
-
default: (value: Validator.ParameterOrFn<T>) => EnumValidator<T | Validator.TDefault>;
|
|
451
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
452
|
-
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<any>;
|
|
453
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
declare namespace HashValidator {
|
|
457
|
-
type Algorithm = keyof (typeof HashValidator)['algorithmLength'];
|
|
458
|
-
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
459
|
-
algorithm: HashValidator.Algorithm;
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
declare class HashValidator<T = string> extends BaseStringValidator<T> {
|
|
10
|
+
type PartialBlockedByTransform = '部分字段携带了 transform,无法调用 partial();可改为指定其中不含 transform 的字段';
|
|
11
|
+
type RequiredBlockedByTransform = '部分字段携带了 transform,无法调用 required();可改为指定其中不含 transform 的字段';
|
|
12
|
+
type PartialRules<T> = { [K in keyof T]: T[K] extends {
|
|
13
|
+
optional: () => infer R;
|
|
14
|
+
} ? R extends Validator ? R : never : never; };
|
|
15
|
+
type RequiredRules<T> = { [K in keyof T]: T[K] extends {
|
|
16
|
+
required: () => infer R;
|
|
17
|
+
} ? R extends Validator ? R : never : never; };
|
|
18
|
+
type PartialableKey<T> = { [P in keyof T]-?: T[P] extends HasPublicOptional ? P & string : never; }[keyof T];
|
|
19
|
+
type RequireableKey<T> = { [P in keyof T]-?: T[P] extends HasPublicRequired ? P & string : never; }[keyof T];
|
|
20
|
+
export declare class ValidatorSchema<T extends Record<string, Validator<any>>> {
|
|
21
|
+
protected readonly rules: T;
|
|
22
|
+
/**
|
|
23
|
+
* 可选的 OpenAPI 具名组件名;`pick` / `omit` / `extend` / `partial` / `required` 不会带上该名称
|
|
24
|
+
*/
|
|
25
|
+
protected readonly name?: string | undefined;
|
|
26
|
+
constructor(rules: T,
|
|
27
|
+
/**
|
|
28
|
+
* 可选的 OpenAPI 具名组件名;`pick` / `omit` / `extend` / `partial` / `required` 不会带上该名称
|
|
29
|
+
*/
|
|
30
|
+
name?: string | undefined);
|
|
31
|
+
/**
|
|
32
|
+
* 为 schema 指定 OpenAPI 文档中的具名组件名,字段定义不变。
|
|
33
|
+
* 生成文档时会按该名称引用;未指定则不单独抽成组件。
|
|
34
|
+
* 名称不会传到 `pick` / `omit` 等派生结果;经 `rule.object(schema)` / `toValidator` 转换后写入 `refName`。
|
|
35
|
+
*
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const userSchema = schema({ id: rule.int(), name: rule.string() }).asName('User');
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
asName(name: string): ValidatorSchema<T>;
|
|
41
|
+
/**
|
|
42
|
+
* 只保留指定字段,得到新的 schema。
|
|
43
|
+
*
|
|
44
|
+
* ```typescript
|
|
45
|
+
* schema.pick('id', 'name');
|
|
46
|
+
* schema.pick(['id', 'name']);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
pick<K extends keyof T>(...keys: K[]): ValidatorSchema<Pick<T, K>>;
|
|
50
|
+
pick<K extends keyof T>(keys: readonly K[]): ValidatorSchema<Pick<T, K>>;
|
|
51
|
+
/**
|
|
52
|
+
* 去掉指定字段,得到新的 schema。
|
|
53
|
+
*
|
|
54
|
+
* ```typescript
|
|
55
|
+
* schema.omit('id', 'name');
|
|
56
|
+
* schema.omit(['id', 'name']);
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
omit<K extends keyof T>(...keys: K[]): ValidatorSchema<Omit<T, K>>;
|
|
60
|
+
omit<K extends keyof T>(keys: readonly K[]): ValidatorSchema<Omit<T, K>>;
|
|
61
|
+
/**
|
|
62
|
+
* 追加或覆盖字段,同名以传入的为准。
|
|
63
|
+
* 可传入普通字段表,或另一个 schema。
|
|
64
|
+
*
|
|
65
|
+
* ```typescript
|
|
66
|
+
* userSchema.extend({ phone: rule.string() });
|
|
67
|
+
* userSchema.extend(extraSchema);
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
extend<U extends { [K in keyof U]: ValidatorToken; }>(fields: U): ValidatorSchema<Omit<T, keyof U> & NormalizeFieldTable<U>>;
|
|
71
|
+
extend<U extends { [K in keyof U]: Validator; }>(other: ValidatorSchema<U>): ValidatorSchema<Omit<T, keyof U> & { [K in keyof U]: U[K]; }>;
|
|
72
|
+
/**
|
|
73
|
+
* 把全部字段变成可选。
|
|
74
|
+
* 若有字段使用了 `transform`,不能无参调用;可改成 `partial('字段名')` 只处理没有 `transform` 的字段。
|
|
75
|
+
*/
|
|
76
|
+
partial(this: [T] extends [{ [K in keyof T]: T[K] extends HasPublicOptional ? T[K] : never; }] ? this : PartialBlockedByTransform): ValidatorSchema<PartialRules<T>>;
|
|
77
|
+
/**
|
|
78
|
+
* 把指定字段变成可选,只能选没有 `transform` 的字段。
|
|
79
|
+
*
|
|
80
|
+
* ```typescript
|
|
81
|
+
* schema.partial('name', 'email');
|
|
82
|
+
* schema.partial(['name', 'email']);
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
partial<K extends PartialableKey<T>>(...keys: [K, ...K[]]): ValidatorSchema<Omit<T, K> & PartialRules<Pick<T, K>>>;
|
|
86
|
+
partial<K extends PartialableKey<T>>(keys: readonly [K, ...K[]]): ValidatorSchema<Omit<T, K> & PartialRules<Pick<T, K>>>;
|
|
87
|
+
/**
|
|
88
|
+
* 把全部字段变成必填(`partial()` 的反向)。
|
|
89
|
+
* 若有字段使用了 `transform`,不能无参调用;可改成 `required('字段名')` 只处理没有 `transform` 的字段。
|
|
90
|
+
* 已有 `default` 的字段调用时保持不变(缺省仍走默认值)。
|
|
91
|
+
*/
|
|
92
|
+
required(this: [T] extends [{ [K in keyof T]: T[K] extends HasPublicRequired ? T[K] : never; }] ? this : RequiredBlockedByTransform): ValidatorSchema<RequiredRules<T>>;
|
|
93
|
+
/**
|
|
94
|
+
* 把指定字段变成必填,只能选没有 `transform` 的字段。
|
|
95
|
+
* 已有 `default` 的字段调用时保持不变。
|
|
96
|
+
*
|
|
97
|
+
* ```typescript
|
|
98
|
+
* schema.required('name', 'email');
|
|
99
|
+
* schema.required(['name', 'email']);
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
required<K extends RequireableKey<T>>(...keys: [K, ...K[]]): ValidatorSchema<Omit<T, K> & RequiredRules<Pick<T, K>>>;
|
|
103
|
+
required<K extends RequireableKey<T>>(keys: readonly [K, ...K[]]): ValidatorSchema<Omit<T, K> & RequiredRules<Pick<T, K>>>;
|
|
104
|
+
/**
|
|
105
|
+
* 无参(rest 长度为 0)表示全部字段。
|
|
106
|
+
* 必须在 normalizeKeys 之前判断:`fn([])` 的 rest 长度是 1,解包后才变成空数组。
|
|
107
|
+
*/
|
|
108
|
+
protected mapSelected(keys: string[] | [readonly string[]], map: (validator: Validator) => Validator): ValidatorSchema<Record<string, Validator<unknown>>>;
|
|
109
|
+
}
|
|
110
|
+
export declare namespace ValidateResult {
|
|
111
|
+
type Any<T> = Accepted<T> | Denied;
|
|
112
|
+
interface Accepted<T> {
|
|
113
|
+
data: T;
|
|
114
|
+
}
|
|
115
|
+
interface Denied {
|
|
116
|
+
errors: string[];
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* `validate(..., { throwOnError: false })` 的返回形态。
|
|
120
|
+
* 请用 `errors === null` 收窄,勿用 `data === null`(合法值也可能为 `null`)。
|
|
121
|
+
*/
|
|
122
|
+
type Safe<T> = {
|
|
123
|
+
data: T;
|
|
124
|
+
errors: null;
|
|
125
|
+
} | {
|
|
126
|
+
data: null;
|
|
127
|
+
errors: string[];
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
export declare const ValidateResult: {
|
|
131
|
+
accept: <T>(data: T) => ValidateResult.Accepted<T>;
|
|
132
|
+
deny: (message: string) => ValidateResult.Denied;
|
|
133
|
+
noError: <T>(result: ValidateResult.Any<T>) => result is ValidateResult.Accepted<T>;
|
|
134
|
+
};
|
|
135
|
+
export declare namespace Validator {
|
|
136
|
+
class TDefault {
|
|
137
|
+
private _DEF_;
|
|
138
|
+
}
|
|
139
|
+
class TOptional {
|
|
140
|
+
private _OPT_;
|
|
141
|
+
}
|
|
142
|
+
class TObject {
|
|
143
|
+
private _OBJ_;
|
|
144
|
+
}
|
|
145
|
+
type ConvertOptional<T> = TDefault extends T ? ConvertOptionalTo<T, never> : ConvertOptionalTo<T, undefined>;
|
|
146
|
+
type ConvertOptionalTo<T, K extends undefined | never> = T extends TOptional ? K : T extends TDefault ? never : T extends TObject ? {
|
|
147
|
+
[K: string]: unknown;
|
|
148
|
+
} : T;
|
|
149
|
+
interface TransformFn<T, T1> {
|
|
150
|
+
(data: ConvertOptional<T>): Promise<T1> | T1;
|
|
151
|
+
}
|
|
152
|
+
type ParameterOrFn<T> = ConvertOptionalTo<T, never> | (() => ConvertOptionalTo<T, never>);
|
|
153
|
+
type Infer<T> = T extends ValidatorSchema<infer U> ? Infer<U> : T extends Validator<infer Type> ? Validator.ConvertOptional<Type> : T extends object ? keyof T extends never ? {} : EmptyObjectToUnknown<{ [K in keyof T as true extends HasUndefined<T[K]> ? never : K]: Infer<T[K]>; }> & EmptyObjectToUnknown<{ [K in keyof T as true extends HasUndefined<T[K]> ? K : never]?: Infer<T[K]>; }> : never;
|
|
154
|
+
type EmptyObjectToUnknown<T extends object> = keyof T extends never ? unknown : T;
|
|
155
|
+
type HasUndefined<T> = T extends Validator<infer Type> ? undefined extends Validator.ConvertOptional<Type> ? true : false : false;
|
|
156
|
+
type HasNull<T> = T extends Validator<infer Type> ? (null extends Type ? true : false) : false;
|
|
157
|
+
type HasDefault<T> = T extends Validator<infer Type> ? Validator.TDefault extends Type ? true : false : false;
|
|
158
|
+
type PartialOpenAPISchema = Pick<OpenAPI.SchemaObject, 'title' | 'description' | 'deprecated' | 'example' | 'externalDocs'>;
|
|
159
|
+
type DocumentMergeMode = 'merge' | 'replace';
|
|
160
|
+
interface Options<Type> {
|
|
161
|
+
defaultValue?: Type | (() => Type);
|
|
162
|
+
required: boolean;
|
|
163
|
+
nullable: boolean;
|
|
164
|
+
transform?: (value: any) => Promise<any> | any;
|
|
165
|
+
docs?: PartialOpenAPISchema;
|
|
166
|
+
strict: boolean;
|
|
463
167
|
/**
|
|
464
|
-
*
|
|
168
|
+
* - `string`:具名组件
|
|
169
|
+
* - `true`:按内容 MD5 命名
|
|
170
|
+
* - `undefined`:不生成 `x-ref`
|
|
465
171
|
*/
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
172
|
+
refName?: string | true;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
export interface TransformedValidator<T> extends Validator<T> {}
|
|
176
|
+
export declare abstract class Validator<T = unknown> {
|
|
177
|
+
protected readonly SubClass: new (...args: any[]) => Validator;
|
|
178
|
+
constructor();
|
|
179
|
+
protected readonly config: Validator.Options<T>;
|
|
180
|
+
/**
|
|
181
|
+
* 判断验证器(含嵌套)中是否包含指定类型
|
|
182
|
+
*/
|
|
183
|
+
static has(source: Validator | Record<string, Validator>, Type: abstract new (...args: any[]) => object): boolean;
|
|
184
|
+
/**
|
|
185
|
+
* @param is 是否开启,默认值:`true`
|
|
186
|
+
*/
|
|
187
|
+
protected strict(is?: boolean): this;
|
|
188
|
+
/**
|
|
189
|
+
* 扩展openapi的配置
|
|
190
|
+
* - **replace:** 本次设置直接覆盖原来的扩展配置
|
|
191
|
+
* - **merge:** 本次设置通过`Object.assign`的形式合并到原来的扩展配置
|
|
192
|
+
* @param mode 默认值:`merge`
|
|
193
|
+
*/
|
|
194
|
+
protected docs(docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode): this;
|
|
195
|
+
/**
|
|
196
|
+
* 值如果是 `undefined, null, ''`或者没传入,以上情况会直接转换成`undefined`
|
|
197
|
+
*
|
|
198
|
+
* 注意:如果执行了`default(...)`,则无需再执行`optional()`
|
|
199
|
+
*
|
|
200
|
+
* 注意:如果设置了`nullable()`,则`null`不做处理
|
|
201
|
+
*/
|
|
202
|
+
protected optional(): Validator;
|
|
203
|
+
/**
|
|
204
|
+
* 将字段设为必填(`optional()` 的反向)。
|
|
205
|
+
*
|
|
206
|
+
* 若已设置 `default(...)`,则原样返回:缺省仍由默认值补齐,无需也不应清掉 default。
|
|
207
|
+
*/
|
|
208
|
+
protected required(): Validator;
|
|
209
|
+
/**
|
|
210
|
+
* 把`null`识别成合法的值
|
|
211
|
+
* @see optional
|
|
212
|
+
*/
|
|
213
|
+
protected nullable(): Validator;
|
|
214
|
+
/**
|
|
215
|
+
* 数据验证成功最后会触发该方法,你可以把数据转换成任何格式
|
|
216
|
+
* ```typescript
|
|
217
|
+
* // 最终返回类型:string
|
|
218
|
+
* rule.string()
|
|
219
|
+
* // 最终返回类型:Array<string>
|
|
220
|
+
* rule.string().transform((value) => [value])
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
protected transform<T1>(fn: (value: any) => Promise<T1> | T1): Validator;
|
|
224
|
+
/**
|
|
225
|
+
* 如果值为空或者没传入,则使用该默认值,而且无需验证
|
|
226
|
+
*
|
|
227
|
+
* 注意:如果执行了`default(...)`,则无需再执行`optional()`
|
|
228
|
+
*/
|
|
229
|
+
protected default(value: any): Validator;
|
|
230
|
+
protected validate(value: any, key?: string, label?: string): Promise<ValidateResult.Any<any>>;
|
|
231
|
+
protected isEmpty(value: any): boolean;
|
|
232
|
+
protected isValidNull(value: any): boolean;
|
|
233
|
+
protected getDefaultValue<T = any>(value: any): T | undefined;
|
|
234
|
+
protected abstract validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>> | ValidateResult.Any<any>;
|
|
235
|
+
protected copy(): Validator;
|
|
236
|
+
protected copyConfig(prev: Validator): this;
|
|
237
|
+
/**
|
|
238
|
+
* 直接嵌套的子验证器,供 `Validator.has` 递归探测
|
|
239
|
+
*/
|
|
240
|
+
protected nested(): Validator[];
|
|
241
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
242
|
+
}
|
|
243
|
+
export declare namespace BaseStringValidator {
|
|
244
|
+
interface Options<T> extends Validator.Options<T> {
|
|
245
|
+
trim?: boolean;
|
|
246
|
+
pattern?: RegExp;
|
|
247
|
+
lengthRange?: LengthRange;
|
|
248
|
+
}
|
|
249
|
+
interface LengthRange {
|
|
250
|
+
min?: number;
|
|
251
|
+
max?: number;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
export declare abstract class BaseStringValidator<T = string> extends Validator<T> {
|
|
255
|
+
protected config: BaseStringValidator.Options<T>;
|
|
256
|
+
/**
|
|
257
|
+
* 删除两边空格后再进行验证
|
|
258
|
+
*/
|
|
259
|
+
protected trim(is?: boolean): this;
|
|
260
|
+
protected length(exactLength: number): this;
|
|
261
|
+
protected length(range: BaseStringValidator.LengthRange): this;
|
|
262
|
+
protected match(pattern: RegExp): this;
|
|
263
|
+
protected isEmpty(value: any): boolean;
|
|
264
|
+
protected getTrimValue(value: string): string;
|
|
265
|
+
protected validateValue(value: any, key: string, label: string): ValidateResult.Any<any> | Promise<ValidateResult.Any<any>>;
|
|
266
|
+
protected abstract validateString(value: string, key: string, label: string): ValidateResult.Any<any> | Promise<ValidateResult.Any<any>>;
|
|
267
|
+
protected copy: () => BaseStringValidator<T>;
|
|
268
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
269
|
+
}
|
|
270
|
+
export declare namespace BaseNumberValidator {
|
|
271
|
+
interface Options<T = false> extends Validator.Options<T> {
|
|
272
|
+
min?: number;
|
|
273
|
+
minInclusive?: boolean;
|
|
274
|
+
max?: number;
|
|
275
|
+
maxInclusive?: boolean;
|
|
276
|
+
precision?: number;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
export declare abstract class BaseNumberValidator<T = number> extends Validator<T> {
|
|
280
|
+
protected config: BaseNumberValidator.Options<T>;
|
|
281
|
+
/**
|
|
282
|
+
* 开启/关闭严格模式,开启后有以下限制:
|
|
283
|
+
* - 字符串不再尝试转换为数字
|
|
284
|
+
*/
|
|
285
|
+
strict: (is?: boolean) => this;
|
|
286
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
287
|
+
/**
|
|
288
|
+
* 最小范围限制
|
|
289
|
+
* @param min 最小值
|
|
290
|
+
* @param inclusive 是否包含最小值,默认值:`true`
|
|
291
|
+
*/
|
|
292
|
+
min(min: number, inclusive?: boolean): this;
|
|
293
|
+
/**
|
|
294
|
+
* 最大范围限制
|
|
295
|
+
* @param min 最大值
|
|
296
|
+
* @param inclusive 是否包含最大值,默认值:`true`
|
|
297
|
+
*/
|
|
298
|
+
max(max: number, inclusive?: boolean): this;
|
|
299
|
+
/**
|
|
300
|
+
* 小数点保留位数,使用`.toFixed()`剔除多余的小数
|
|
301
|
+
*/
|
|
302
|
+
protected precision(decimals: number): this;
|
|
303
|
+
protected validateValue(num: number, key: string, label: string): ValidateResult.Any<number>;
|
|
304
|
+
protected abstract validateNumber(num: number, key: string, label: string): ValidateResult.Any<number>;
|
|
305
|
+
protected copy: () => BaseNumberValidator<T>;
|
|
306
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
307
|
+
}
|
|
308
|
+
export declare class ValidateDeniedError extends Error {
|
|
309
|
+
protected readonly errors: ValidateResult.Denied['errors'];
|
|
310
|
+
constructor(message: string | undefined, errors?: ValidateResult.Denied['errors']);
|
|
311
|
+
getValidateErrors(): string[];
|
|
312
|
+
}
|
|
313
|
+
export declare namespace AllOfValidator {
|
|
314
|
+
interface Options<T> extends Validator.Options<T> {
|
|
315
|
+
validators: Validator[];
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
export declare class AllOfValidator<T = never> extends Validator<T> {
|
|
319
|
+
protected config: AllOfValidator.Options<T>;
|
|
320
|
+
constructor(validators: [Validator, ...Validator[]]);
|
|
321
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
322
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
323
|
+
optional: () => AllOfValidator<T | Validator.TOptional>;
|
|
324
|
+
required: () => AllOfValidator<Exclude<T, Validator.TOptional>>;
|
|
325
|
+
nullable: () => AllOfValidator<T | null>;
|
|
326
|
+
protected isEmpty(_: any): boolean;
|
|
327
|
+
protected validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>>;
|
|
328
|
+
protected copyConfig(prev: AllOfValidator): this;
|
|
329
|
+
protected nested(): Validator[];
|
|
330
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
331
|
+
}
|
|
332
|
+
export declare namespace AnyValidator {
|
|
333
|
+
type Type = number | string | boolean | any[] | {
|
|
334
|
+
[K: string]: unknown;
|
|
335
|
+
} | bigint | Buffer;
|
|
336
|
+
}
|
|
337
|
+
export declare class AnyValidator<T = AnyValidator.Type> extends Validator<T> {
|
|
338
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
339
|
+
optional: () => AnyValidator<T | Validator.TOptional>;
|
|
340
|
+
required: () => AnyValidator<Exclude<T, Validator.TOptional>>;
|
|
341
|
+
nullable: () => AnyValidator<T | null>;
|
|
342
|
+
default: (anyValue: Validator.ParameterOrFn<T>) => AnyValidator<T | Validator.TDefault>;
|
|
343
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
344
|
+
protected validateValue(value: any): ValidateResult.Any<any>;
|
|
345
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
346
|
+
}
|
|
347
|
+
export declare namespace AnyOfValidator {
|
|
348
|
+
interface Options<T> extends Validator.Options<T> {
|
|
349
|
+
validators: Validator[];
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
export declare class AnyOfValidator<T = never> extends Validator<T> {
|
|
353
|
+
protected config: AnyOfValidator.Options<T>;
|
|
354
|
+
constructor(validators: [Validator, ...Validator[]]);
|
|
355
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
356
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
357
|
+
optional: () => AnyOfValidator<T | Validator.TOptional>;
|
|
358
|
+
required: () => AnyOfValidator<Exclude<T, Validator.TOptional>>;
|
|
359
|
+
nullable: () => AnyOfValidator<T | null>;
|
|
360
|
+
protected isEmpty(_: any): boolean;
|
|
361
|
+
protected validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>>;
|
|
362
|
+
protected copyConfig(prev: AnyOfValidator): this;
|
|
363
|
+
protected nested(): Validator[];
|
|
364
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
365
|
+
}
|
|
366
|
+
export declare namespace ArrayValidator {
|
|
367
|
+
type ItemOf<T> = Validator.ConvertOptional<T> extends (infer U)[] ? U : never;
|
|
368
|
+
interface Options<T> extends Validator.Options<T> {
|
|
369
|
+
itemValidator?: Validator;
|
|
370
|
+
lengthRange: {
|
|
371
|
+
min?: number;
|
|
372
|
+
max?: number;
|
|
480
373
|
};
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
optional: () => HashValidator<T | Validator.TOptional>;
|
|
486
|
-
nullable: () => HashValidator<T | null>;
|
|
487
|
-
default: (algorithm: Validator.ParameterOrFn<T>) => HashValidator<T | Validator.TDefault>;
|
|
488
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
489
|
-
protected validateString(hash: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
490
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
declare class IntValidator<T = number> extends BaseNumberValidator<T> {
|
|
494
|
-
protected config: BaseNumberValidator.Options<T>;
|
|
495
|
-
optional: () => IntValidator<T | Validator.TOptional>;
|
|
496
|
-
nullable: () => IntValidator<T | null>;
|
|
497
|
-
default: (integer: Validator.ParameterOrFn<T>) => IntValidator<T | Validator.TDefault>;
|
|
498
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
499
|
-
protected validateNumber(num: number, _key: string, label: string): ValidateResult.Any<number>;
|
|
500
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
declare namespace IpValidator {
|
|
504
|
-
type Version = 'v4' | 'v6';
|
|
505
|
-
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
506
|
-
ipVersion: IpValidator.Version[];
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
declare class IpValidator<T = string> extends BaseStringValidator<T> {
|
|
510
|
-
protected config: IpValidator.Options<T>;
|
|
511
|
-
constructor(version: IpValidator.Version[]);
|
|
512
|
-
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
513
|
-
optional: () => IpValidator<T | Validator.TOptional>;
|
|
514
|
-
nullable: () => IpValidator<T | null>;
|
|
515
|
-
default: (ip: Validator.ParameterOrFn<T>) => IpValidator<T | Validator.TDefault>;
|
|
516
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
517
|
-
match: (pattern: RegExp) => this;
|
|
518
|
-
protected validateString(ip: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
519
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
declare namespace NumberValidator {
|
|
523
|
-
interface Options<T = false> extends BaseNumberValidator.Options<T> {
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
declare class NumberValidator<T = number> extends BaseNumberValidator<T> {
|
|
527
|
-
protected config: NumberValidator.Options<T>;
|
|
528
|
-
optional: () => NumberValidator<T | Validator.TOptional>;
|
|
529
|
-
nullable: () => NumberValidator<T | null>;
|
|
530
|
-
default: (number: Validator.ParameterOrFn<T>) => NumberValidator<T | Validator.TDefault>;
|
|
531
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
532
|
-
precision: (decimals: number) => this;
|
|
533
|
-
protected validateNumber(num: number): ValidateResult.Any<number>;
|
|
534
|
-
protected copy: () => this;
|
|
535
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
declare namespace ObjectValidator {
|
|
539
|
-
type Property = Record<string, Validator>;
|
|
540
|
-
interface Options<T> extends Validator.Options<T> {
|
|
541
|
-
properties?: Property;
|
|
542
|
-
stringToObject?: boolean;
|
|
543
|
-
keepAdditional?: boolean | {
|
|
544
|
-
key?: RegExp[];
|
|
545
|
-
value?: Validator;
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
declare class ObjectValidator<T = Validator.TObject> extends Validator<T> {
|
|
550
|
-
protected config: ObjectValidator.Options<T>;
|
|
551
|
-
constructor(properties?: ObjectValidator.Property);
|
|
552
|
-
/**
|
|
553
|
-
* 开启严格模式后:
|
|
554
|
-
* - 不从字符串解析出对象,除非通过`parseFromString()`手动指定
|
|
555
|
-
*/
|
|
556
|
-
strict: (is?: boolean) => this;
|
|
557
|
-
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
558
|
-
optional: () => ObjectValidator<T | Validator.TOptional>;
|
|
559
|
-
nullable: () => ObjectValidator<T | null>;
|
|
560
|
-
default: (object: Validator.ParameterOrFn<T>) => ObjectValidator<T | Validator.TDefault>;
|
|
561
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
562
|
-
/**
|
|
563
|
-
* 尝试使用`JSON.parse`把字符串转换为对象,在非严格模式下默认开启
|
|
564
|
-
*/
|
|
565
|
-
parseFromString(is?: boolean): ObjectValidator<T>;
|
|
566
|
-
/**
|
|
567
|
-
* 保留未被验证的键值对。对象中包含动态字段时,无法使用具体的结构体描述出对象的组成。
|
|
568
|
-
*
|
|
569
|
-
* 有以下几种保留规则:
|
|
570
|
-
* 1. 不提供参数,则保留所有的属性和值;
|
|
571
|
-
* 2. 提供`key`,则保留 键 能匹配正则表达式的键值对;
|
|
572
|
-
* 3. 提供`value`,则保留 值 能匹配验证器的键值对,同时值会根据验证器逻辑做相应的修正;
|
|
573
|
-
* 4. 提供`key`和`value`,则保留能同时满足规则2和3的键值对。
|
|
574
|
-
*
|
|
575
|
-
* ```typescript
|
|
576
|
-
* const v = rule.object();
|
|
577
|
-
*
|
|
578
|
-
* // { a: 'x', b: 1, c: true } -> { a: 'x', b: 1, c: true }
|
|
579
|
-
* v.additional();
|
|
580
|
-
*
|
|
581
|
-
* // { a: 'x', b: 1, c: true } -> { a: 'x' }
|
|
582
|
-
* v.additional({ value: rule.string() });
|
|
583
|
-
*
|
|
584
|
-
* // { a: 'x', b: 1, c: true } -> { a: 'x', c: true }
|
|
585
|
-
* v.additional({ key: /^(a|c)$/ });
|
|
586
|
-
*
|
|
587
|
-
* // { a: 'x', b: 1, c: true } -> { c: true }
|
|
588
|
-
* v.additional({ key: /^(a|c)$/, value: rule.boolean() });
|
|
589
|
-
*
|
|
590
|
-
* // { a: 'x', b: 1, c: true } -> { }
|
|
591
|
-
* v.additional({ key: /^(a|c)$/, value: rule.number() });
|
|
592
|
-
* ```
|
|
593
|
-
*/
|
|
594
|
-
additional(): ObjectValidator<T extends Validator.TObject ? {
|
|
595
|
-
[K: string]: unknown;
|
|
596
|
-
} : T extends null ? null : T & {
|
|
597
|
-
[K: string]: unknown;
|
|
598
|
-
}>;
|
|
599
|
-
additional(opts: {
|
|
600
|
-
key: RegExp | RegExp[];
|
|
601
|
-
value?: undefined;
|
|
602
|
-
}): ObjectValidator<(T extends Validator.TObject ? unknown : T) & {
|
|
603
|
-
[K: string]: unknown;
|
|
604
|
-
}>;
|
|
605
|
-
additional<Additional extends Validator>(opts: {
|
|
606
|
-
key?: RegExp | RegExp[];
|
|
607
|
-
value: Additional;
|
|
608
|
-
}): ObjectValidator<T extends Validator.TObject ? {
|
|
609
|
-
[K: string]: Validator.Infer<Additional>;
|
|
610
|
-
} : T extends null ? null : T & {
|
|
611
|
-
[K: string]: Validator.Infer<Additional>;
|
|
612
|
-
}>;
|
|
613
|
-
protected isPlainObject(value: any): value is object;
|
|
614
|
-
protected validateValue(origin: Record<string, any>, key: string, label: string): Promise<ValidateResult.Any<object>>;
|
|
615
|
-
protected copy: () => ObjectValidator<T>;
|
|
616
|
-
protected copyConfig(prev: ObjectValidator): this;
|
|
617
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
declare namespace OneOfValidator {
|
|
621
|
-
interface Options<T> extends Validator.Options<T> {
|
|
622
|
-
validators: Validator[];
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
|
-
declare class OneOfValidator<T = never> extends Validator<T> {
|
|
626
|
-
protected config: OneOfValidator.Options<T>;
|
|
627
|
-
constructor(validators: [Validator, ...Validator[]]);
|
|
628
|
-
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
629
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
630
|
-
optional: () => OneOfValidator<T | Validator.TOptional>;
|
|
631
|
-
nullable: () => OneOfValidator<T | null>;
|
|
632
|
-
protected isEmpty(_: any): boolean;
|
|
633
|
-
protected validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>>;
|
|
634
|
-
protected copyConfig(prev: OneOfValidator): this;
|
|
635
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
declare namespace StreamValidator {
|
|
639
|
-
interface Options<T = stream$1.Stream> extends Validator.Options<T> {
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
declare class StreamValidator<T = stream$1.Stream> extends Validator<T> {
|
|
643
|
-
protected config: StreamValidator.Options<T>;
|
|
644
|
-
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
645
|
-
optional: () => StreamValidator<T | Validator.TOptional>;
|
|
646
|
-
nullable: () => StreamValidator<T | null>;
|
|
647
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
648
|
-
protected validateValue(value: stream$1.Stream, _key: string, label: string): ValidateResult.Any<stream$1.Stream>;
|
|
649
|
-
protected copy: () => this;
|
|
650
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
declare namespace StringValidator {
|
|
654
|
-
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
655
|
-
allowEmpty?: boolean;
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
declare class StringValidator<T = string> extends BaseStringValidator<T> {
|
|
659
|
-
protected config: StringValidator.Options<T>;
|
|
660
|
-
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
661
|
-
optional: () => StringValidator<T | Validator.TOptional>;
|
|
662
|
-
nullable: () => StringValidator<T | null>;
|
|
663
|
-
default: (string: Validator.ParameterOrFn<T>) => StringValidator<T | Validator.TDefault>;
|
|
664
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
665
|
-
match: (pattern: RegExp) => this;
|
|
666
|
-
trim: (is?: boolean) => this;
|
|
667
|
-
length: (exactOrRange: number | BaseStringValidator.LengthRange) => this;
|
|
668
|
-
/**
|
|
669
|
-
* 把空字符串`''`设置为合法的值
|
|
670
|
-
*/
|
|
671
|
-
allowEmptyString(): StringValidator<T>;
|
|
672
|
-
protected isEmpty(value: any): boolean;
|
|
673
|
-
protected validateString(value: string): ValidateResult.Any<string>;
|
|
674
|
-
protected copy: () => StringValidator<T>;
|
|
675
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
declare namespace UlidValidator {
|
|
679
|
-
interface Options<T = string> extends BaseStringValidator.Options<T> {
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
declare class UlidValidator<T = string> extends BaseStringValidator<T> {
|
|
683
|
-
protected config: UlidValidator.Options<T>;
|
|
684
|
-
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
685
|
-
optional: () => UlidValidator<T | Validator.TOptional>;
|
|
686
|
-
nullable: () => UlidValidator<T | null>;
|
|
687
|
-
default: (uuid: Validator.ParameterOrFn<T>) => UlidValidator<T | Validator.TDefault>;
|
|
688
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
689
|
-
protected validateString(ulid: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
690
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
declare namespace UrlValidator {
|
|
694
|
-
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
695
|
-
allowedScheme: string[];
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
declare class UrlValidator<T = string> extends BaseStringValidator<T> {
|
|
699
|
-
protected config: UrlValidator.Options<T>;
|
|
700
|
-
constructor();
|
|
701
|
-
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
702
|
-
optional: () => UrlValidator<T | Validator.TOptional>;
|
|
703
|
-
nullable: () => UrlValidator<T | null>;
|
|
704
|
-
default: (Url: Validator.ParameterOrFn<T>) => UrlValidator<T | Validator.TDefault>;
|
|
705
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
706
|
-
/**
|
|
707
|
-
* 允许使用的协议,不区分大小写。默认配置:`['http', 'https']`
|
|
708
|
-
*/
|
|
709
|
-
scheme(scheme: string[]): UrlValidator<T>;
|
|
710
|
-
protected validateString(url: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
711
|
-
protected copy: () => UrlValidator<T>;
|
|
712
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
declare namespace UuidValidator {
|
|
716
|
-
type Version = (typeof UuidValidator.versions)[number];
|
|
717
|
-
interface Options<T = string> extends BaseStringValidator.Options<T> {
|
|
718
|
-
uuidVersion: Version[];
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
declare class UuidValidator<T = string> extends BaseStringValidator<T> {
|
|
722
|
-
static versions: readonly ["v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"];
|
|
723
|
-
static patterns: {
|
|
724
|
-
[key in UuidValidator.Version | 'all']: RegExp;
|
|
374
|
+
fromString?: string | RegExp;
|
|
375
|
+
force?: {
|
|
376
|
+
mode: 'separator' | 'block';
|
|
377
|
+
separator?: string | RegExp;
|
|
725
378
|
};
|
|
726
|
-
protected config: UuidValidator.Options<T>;
|
|
727
|
-
constructor(versions: UuidValidator.Version[]);
|
|
728
|
-
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
729
|
-
optional: () => UuidValidator<T | Validator.TOptional>;
|
|
730
|
-
nullable: () => UuidValidator<T | null>;
|
|
731
|
-
default: (uuid: Validator.ParameterOrFn<T>) => UuidValidator<T | Validator.TDefault>;
|
|
732
|
-
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
733
|
-
match: (pattern: RegExp) => this;
|
|
734
|
-
protected validateString(uuid: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
735
|
-
protected toDocument(): OpenAPI.SchemaObject;
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
declare class Rule {
|
|
739
|
-
static register<T extends Rule, K extends keyof T>(this: new (...args: any[]) => T, name: K, SubValidator: new (...args: any[]) => Validator): void;
|
|
740
|
-
/**
|
|
741
|
-
* 规则以管道形式执行,需要匹配所有规则
|
|
742
|
-
* ```typescript
|
|
743
|
-
* rule.allOf([rule.number(), rule.string()]);
|
|
744
|
-
* ```
|
|
745
|
-
*/
|
|
746
|
-
allOf<T extends Validator[], A extends Validator, B extends Validator>(rules: [rule1: A, rule2: B, ...others: T]): AllOfValidator<Validator.Infer<A> & Validator.Infer<B> & Union2Intersection<{
|
|
747
|
-
[K in keyof T]: Validator.Infer<T[K]>;
|
|
748
|
-
}[number]>>;
|
|
749
|
-
/**
|
|
750
|
-
* 允许任何类型:
|
|
751
|
-
* - string
|
|
752
|
-
* - number
|
|
753
|
-
* - boolean
|
|
754
|
-
* - array
|
|
755
|
-
* - object
|
|
756
|
-
* - buffer
|
|
757
|
-
*/
|
|
758
|
-
any(): AnyValidator<AnyValidator.Type>;
|
|
759
|
-
/**
|
|
760
|
-
* 规则以管道形式执行,需要至少匹配一个规则
|
|
761
|
-
* ```typescript
|
|
762
|
-
* rule.anyOf([rule.number(), rule.string()]);
|
|
763
|
-
* ```
|
|
764
|
-
*/
|
|
765
|
-
anyOf<T extends Validator[], A extends Validator, B extends Validator>(rules: [rule1: A, rule2: B, ...others: T]): AnyOfValidator<Validator.Infer<A> | Validator.Infer<B> | {
|
|
766
|
-
[K in keyof T]: Validator.Infer<T[K]>;
|
|
767
|
-
}[number]>;
|
|
768
|
-
array(): ArrayValidator<unknown[]>;
|
|
769
|
-
array<T extends Validator>(item: T): ArrayValidator<Validator.Infer<T>[]>;
|
|
770
|
-
/**
|
|
771
|
-
* 对象验证规则的快捷操作,等价于 `rule.array(rule.object({}))`
|
|
772
|
-
*/
|
|
773
|
-
array<T extends {
|
|
774
|
-
[key: string]: P;
|
|
775
|
-
}, P extends Validator>(ruleObject: T): ArrayValidator<Validator.Infer<T>[]>;
|
|
776
|
-
/**
|
|
777
|
-
* 大整数类型
|
|
778
|
-
*
|
|
779
|
-
* 注意:非严格模式下,`number` 和 `string` 会尝试被转换成 `bigint` 类型
|
|
780
|
-
*/
|
|
781
|
-
bigint(): BigIntValidator<bigint>;
|
|
782
|
-
/**
|
|
783
|
-
* - 默认真值:`[1, '1', true, 'true']`
|
|
784
|
-
* - 默认假值:`[0, '0', false, 'false']`
|
|
785
|
-
*/
|
|
786
|
-
boolean(): BooleanValidator<boolean>;
|
|
787
|
-
buffer<T extends Buffer>(): BufferValidator<T>;
|
|
788
|
-
/**
|
|
789
|
-
* @deprecated 请使用 rule.date()
|
|
790
|
-
*/
|
|
791
|
-
dateTime(...formats: string[]): DateValidator<Date>;
|
|
792
|
-
/**
|
|
793
|
-
* @param formats 设置解析字符串的格式,支持同时指定不同的格式并从左到右依次尝试。如果是标准的ISO时间格式或者时间戳则无需指定。
|
|
794
|
-
*
|
|
795
|
-
* @link https://moment.github.io/luxon/#/formatting?id=table-of-tokens
|
|
796
|
-
*/
|
|
797
|
-
date(...formats: string[]): DateValidator<Date>;
|
|
798
|
-
email(): EmailValidator<string>;
|
|
799
|
-
enum<const T extends string | number | boolean>(ranges: T[]): EnumValidator<T>;
|
|
800
|
-
enum<const T extends readonly (string | number | boolean)[]>(ranges: T): EnumValidator<T[number]>;
|
|
801
|
-
hash(algorithm: HashValidator.Algorithm): HashValidator<string>;
|
|
802
|
-
/**
|
|
803
|
-
* 注意:非严格模式下,`string` 会尝试被转换成 `number` 类型
|
|
804
|
-
*/
|
|
805
|
-
int(): IntValidator<number>;
|
|
806
379
|
/**
|
|
807
|
-
*
|
|
808
|
-
*
|
|
809
|
-
* rule.ip('v6');
|
|
810
|
-
* rule.ip(['v4', 'v6']);
|
|
811
|
-
* ```
|
|
380
|
+
* - `true`:整项比较(规则见 `unique()`)
|
|
381
|
+
* - 函数:按抽取结果比较
|
|
812
382
|
*/
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
383
|
+
uniqueBy?: true | ((item: any) => unknown);
|
|
384
|
+
}
|
|
385
|
+
interface LengthRange {
|
|
386
|
+
min?: number;
|
|
387
|
+
max?: number;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
export declare class ArrayValidator<T = unknown[]> extends Validator<T> {
|
|
391
|
+
protected config: ArrayValidator.Options<T>;
|
|
392
|
+
constructor(validator?: Validator);
|
|
393
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
394
|
+
optional: () => ArrayValidator<T | Validator.TOptional>;
|
|
395
|
+
required: () => ArrayValidator<Exclude<T, Validator.TOptional>>;
|
|
396
|
+
nullable: () => ArrayValidator<T | null>;
|
|
397
|
+
default: (array: Validator.ParameterOrFn<T>) => ArrayValidator<T | Validator.TDefault>;
|
|
398
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
399
|
+
/**
|
|
400
|
+
* 强制将`非数组`的值转换成数组类型。控制台命令行和url查询字符串场景下,如果只传了一个元素,则有可能被识别为非数组,而传递多个元素时又变成了数组结构。
|
|
401
|
+
*
|
|
402
|
+
* @param mode 转换模式
|
|
403
|
+
*
|
|
404
|
+
* - `separator` 使用分割符号,把字符串拆分成数组
|
|
405
|
+
* - `block` 把输入当成一个整体,作为数组的一个元素
|
|
406
|
+
*
|
|
407
|
+
* @param separator 分割符号,默认值:`new RegExp('\s*,\s*')`
|
|
408
|
+
*/
|
|
409
|
+
forceToArray(mode: 'separator', separator?: string | RegExp): ArrayValidator<T>;
|
|
410
|
+
forceToArray(mode: 'block'): ArrayValidator<T>;
|
|
411
|
+
length(exactLength: number): this;
|
|
412
|
+
length(range: ArrayValidator.LengthRange): this;
|
|
413
|
+
/**
|
|
414
|
+
* 校验数组元素唯一(不做深比较)。
|
|
415
|
+
*
|
|
416
|
+
* 比较规则与 `Set` / `Map` 的键相同:`NaN` 与 `NaN` 视为相同,`+0` 与 `-0` 视为相同;
|
|
417
|
+
* 对象按引用判断,内容相同但不是同一引用不算重复。
|
|
418
|
+
*
|
|
419
|
+
* - 无参:整项比较,文档输出 `uniqueItems: true`
|
|
420
|
+
* - 字符串:仅允许元素类型上的固定 key(`keyof`),不支持 `'a.b'` 路径;不写入 OpenAPI
|
|
421
|
+
* - 函数:自定义抽取后再比较;不写入 OpenAPI
|
|
422
|
+
*/
|
|
423
|
+
unique(): this;
|
|
424
|
+
unique<K extends keyof ArrayValidator.ItemOf<T> & string>(key: ArrayValidator.ItemOf<T> extends object ? K : never): this;
|
|
425
|
+
unique(by: (item: ArrayValidator.ItemOf<T>) => unknown): this;
|
|
426
|
+
protected validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any[]>>;
|
|
427
|
+
protected copy: () => ArrayValidator<T>;
|
|
428
|
+
protected nested(): Validator[];
|
|
429
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
430
|
+
}
|
|
431
|
+
export declare namespace BigIntValidator {
|
|
432
|
+
interface Options<T = false> extends Validator.Options<T> {
|
|
433
|
+
min?: bigint;
|
|
434
|
+
minInclusive?: boolean;
|
|
435
|
+
max?: bigint;
|
|
436
|
+
maxInclusive?: boolean;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
export declare class BigIntValidator<T = bigint> extends Validator<T> {
|
|
440
|
+
protected config: BigIntValidator.Options<T>;
|
|
441
|
+
/**
|
|
442
|
+
* 开启严格模式后:
|
|
443
|
+
* - string或者number不再转换为大整数
|
|
444
|
+
*/
|
|
445
|
+
strict: (is?: boolean) => this;
|
|
446
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
447
|
+
optional: () => BigIntValidator<T | Validator.TOptional>;
|
|
448
|
+
required: () => BigIntValidator<Exclude<T, Validator.TOptional>>;
|
|
449
|
+
nullable: () => BigIntValidator<T | null>;
|
|
450
|
+
default: (value: bigint) => BigIntValidator<T | Validator.TDefault>;
|
|
451
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
452
|
+
min(min: bigint, inclusive?: boolean): this;
|
|
453
|
+
max(max: bigint, inclusive?: boolean): this;
|
|
454
|
+
protected validateValue(value: bigint, _key: string, label: string): ValidateResult.Any<bigint>;
|
|
455
|
+
protected copy: () => BigIntValidator<T>;
|
|
456
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
457
|
+
}
|
|
458
|
+
export declare namespace BooleanValidator {
|
|
459
|
+
interface Options<T> extends Validator.Options<T> {
|
|
460
|
+
truthyValues?: any[];
|
|
461
|
+
falsyValues?: any[];
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
export declare class BooleanValidator<T = boolean> extends Validator<T> {
|
|
465
|
+
protected config: BooleanValidator.Options<T>;
|
|
466
|
+
protected static defaultTruthyValues: any[];
|
|
467
|
+
protected static defaultFalsyValues: any[];
|
|
468
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
469
|
+
optional: () => BooleanValidator<T | Validator.TOptional>;
|
|
470
|
+
required: () => BooleanValidator<Exclude<T, Validator.TOptional>>;
|
|
471
|
+
nullable: () => BooleanValidator<T | null>;
|
|
472
|
+
default: (boolValue: boolean) => BooleanValidator<T | Validator.TDefault>;
|
|
473
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
474
|
+
/**
|
|
475
|
+
* 重新设置真值。默认值:`[1, '1', true, 'true']`
|
|
476
|
+
* ```typescript
|
|
477
|
+
* boolean.setTruthyValues([true, 'yes']);
|
|
478
|
+
* boolean.setTruthyValues(['on', 'good']);
|
|
479
|
+
* ```
|
|
480
|
+
*/
|
|
481
|
+
setTruthyValues(values: any[]): BooleanValidator<T>;
|
|
482
|
+
/**
|
|
483
|
+
* 重新设置假值。默认值:`[0, '0', false, 'false']`
|
|
484
|
+
* ```typescript
|
|
485
|
+
* boolean.setFalsyValues([false, 'no']);
|
|
486
|
+
* boolean.setFalsyValues(['off', 'bad']);
|
|
487
|
+
* ```
|
|
488
|
+
*/
|
|
489
|
+
setFalsyValues(values: any[]): BooleanValidator<T>;
|
|
490
|
+
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<boolean>;
|
|
491
|
+
protected copy: () => BooleanValidator<T>;
|
|
492
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
493
|
+
}
|
|
494
|
+
export declare namespace BufferValidator {
|
|
495
|
+
interface Options<T = Buffer> extends Validator.Options<T> {
|
|
496
|
+
fromEncodings?: Encodings[];
|
|
497
|
+
}
|
|
498
|
+
type Encodings = 'base64' | 'hex';
|
|
499
|
+
}
|
|
500
|
+
export declare class BufferValidator<T = Buffer> extends Validator<T> {
|
|
501
|
+
protected config: BufferValidator.Options<T>;
|
|
502
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
503
|
+
optional: () => BufferValidator<T | Validator.TOptional>;
|
|
504
|
+
required: () => BufferValidator<Exclude<T, Validator.TOptional>>;
|
|
505
|
+
nullable: () => BufferValidator<T | null>;
|
|
506
|
+
default: (buffer: Buffer) => BufferValidator<T | Validator.TDefault>;
|
|
507
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
508
|
+
/**
|
|
509
|
+
* 尝试从其他类型恢复为buffer类型:
|
|
510
|
+
*
|
|
511
|
+
* - `hex` 从十六进制字符串恢复
|
|
512
|
+
* - `base64` 从base64字符串恢复
|
|
513
|
+
*/
|
|
514
|
+
parseFrom(...encodings: BufferValidator.Encodings[]): this;
|
|
515
|
+
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<Buffer>;
|
|
516
|
+
protected copy: () => this;
|
|
517
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
518
|
+
}
|
|
519
|
+
export declare namespace DateValidator {
|
|
520
|
+
interface Options<T = Date> extends Validator.Options<T> {
|
|
521
|
+
min?: () => Date;
|
|
522
|
+
minInclusive?: boolean;
|
|
523
|
+
max?: () => Date;
|
|
524
|
+
maxInclusive?: boolean;
|
|
525
|
+
parseFromTimestamp?: boolean;
|
|
526
|
+
formats?: string[];
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
export declare class DateValidator<T = Date> extends Validator<T> {
|
|
530
|
+
protected config: DateValidator.Options<T>;
|
|
531
|
+
constructor(formats?: string[]);
|
|
532
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
533
|
+
optional: () => DateValidator<T | Validator.TOptional>;
|
|
534
|
+
required: () => DateValidator<Exclude<T, Validator.TOptional>>;
|
|
535
|
+
nullable: () => DateValidator<T | null>;
|
|
536
|
+
default: (date: Validator.ParameterOrFn<Date>) => DateValidator<T | Validator.TDefault>;
|
|
537
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
538
|
+
min(freshDate: () => Date, inclusive?: boolean): DateValidator<T>;
|
|
539
|
+
max(freshDate: () => Date, inclusive?: boolean): DateValidator<T>;
|
|
540
|
+
/**
|
|
541
|
+
* 尝试把时间戳数字解析成时间对象。默认已开启
|
|
542
|
+
*
|
|
543
|
+
* 支持如下格式:
|
|
544
|
+
* - 13位:1711257956199
|
|
545
|
+
* - 14位:1711257956.199
|
|
546
|
+
* - 10位:1711257956
|
|
547
|
+
*/
|
|
548
|
+
parseFromTimestamp(is?: boolean): this;
|
|
549
|
+
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<Date>;
|
|
550
|
+
protected toDate(value: any): false | Date;
|
|
551
|
+
protected compare(date: Date): boolean;
|
|
552
|
+
protected copy: () => this;
|
|
553
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
554
|
+
}
|
|
555
|
+
export declare namespace DecimalValidator {
|
|
556
|
+
interface Options<T = false> extends Validator.Options<T> {
|
|
557
|
+
min?: Decimal;
|
|
558
|
+
minInclusive?: boolean;
|
|
559
|
+
max?: Decimal;
|
|
560
|
+
maxInclusive?: boolean;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
export declare class DecimalValidator<T = Decimal> extends Validator<T> {
|
|
564
|
+
protected config: DecimalValidator.Options<T>;
|
|
565
|
+
/**
|
|
566
|
+
* 开启严格模式后:
|
|
567
|
+
* - string / number 不再转换为 Decimal
|
|
568
|
+
*/
|
|
569
|
+
strict: (is?: boolean) => this;
|
|
570
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
571
|
+
optional: () => DecimalValidator<T | Validator.TOptional>;
|
|
572
|
+
required: () => DecimalValidator<Exclude<T, Validator.TOptional>>;
|
|
573
|
+
nullable: () => DecimalValidator<T | null>;
|
|
574
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
575
|
+
default(value: string | number | Decimal): DecimalValidator<T | Validator.TDefault>;
|
|
576
|
+
min(min: string | number | Decimal, inclusive?: boolean): this;
|
|
577
|
+
max(max: string | number | Decimal, inclusive?: boolean): this;
|
|
578
|
+
protected validateValue(value: unknown, _key: string, label: string): ValidateResult.Any<Decimal>;
|
|
579
|
+
protected copy: () => DecimalValidator<T>;
|
|
580
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
581
|
+
}
|
|
582
|
+
export declare namespace EmailValidator {
|
|
583
|
+
interface Options<T> extends BaseStringValidator.Options<T> {}
|
|
584
|
+
}
|
|
585
|
+
export declare class EmailValidator<T = string> extends BaseStringValidator<T> {
|
|
586
|
+
protected config: EmailValidator.Options<T>;
|
|
587
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
588
|
+
optional: () => EmailValidator<T | Validator.TOptional>;
|
|
589
|
+
required: () => EmailValidator<Exclude<T, Validator.TOptional>>;
|
|
590
|
+
nullable: () => EmailValidator<T | null>;
|
|
591
|
+
default: (email: Validator.ParameterOrFn<T>) => EmailValidator<T | Validator.TDefault>;
|
|
592
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
593
|
+
match: (pattern: RegExp) => this;
|
|
594
|
+
protected validateString(email: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
595
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
857
596
|
}
|
|
858
|
-
|
|
859
|
-
|
|
597
|
+
/**
|
|
598
|
+
* TS enum / 枚举风格对象(值仅为 string | number)
|
|
599
|
+
* ```typescript
|
|
600
|
+
* enum Color { Red = 'red', Blue = 'blue' }
|
|
601
|
+
* const Status = { On: 1, Off: 0 } as const;
|
|
602
|
+
* ```
|
|
603
|
+
*/
|
|
604
|
+
export type EnumLike = {
|
|
605
|
+
readonly [key: string]: string | number;
|
|
606
|
+
};
|
|
607
|
+
/** 去掉 string/number 索引签名后的具名键,避免 `T[keyof T]` 被冲成 `string | number` */
|
|
608
|
+
type EnumLikeKnownKeys<T> = keyof { [K in keyof T as string extends K ? never : number extends K ? never : K]: true; };
|
|
609
|
+
/**
|
|
610
|
+
* `EnumLike` 的取值联合(OpenAPI `enum` 可选项)。
|
|
611
|
+
* 传入 TS `enum` 时通常保留枚举品牌类型(如 `Color`),而非裸字面量联合。
|
|
612
|
+
*/
|
|
613
|
+
export type EnumLikeValues<T extends EnumLike> = T[EnumLikeKnownKeys<T>];
|
|
614
|
+
/**
|
|
615
|
+
* 从 TS enum / 枚举对象抽出可选值;数字 enum 会丢掉反向映射键(`"0"` / `"1"`)
|
|
616
|
+
*/
|
|
617
|
+
export declare function enumLikeToRanges(source: EnumLike): Array<string | number>;
|
|
618
|
+
export declare namespace EnumValidator {
|
|
619
|
+
interface Options<T> extends Validator.Options<T> {
|
|
620
|
+
ranges: T[];
|
|
621
|
+
}
|
|
622
|
+
/** optional / nullable / default 等标志位,pick/omit 时需保留 */
|
|
623
|
+
type FlagsOf<T> = Extract<T, null | Validator.TOptional | Validator.TDefault>;
|
|
624
|
+
/** 真正的枚举取值(去掉标志位) */
|
|
625
|
+
type ValueOf<T> = Exclude<T, null | Validator.TOptional | Validator.TDefault>;
|
|
626
|
+
}
|
|
627
|
+
export declare class EnumValidator<const T = never> extends Validator<T> {
|
|
628
|
+
protected config: EnumValidator.Options<T>;
|
|
629
|
+
constructor(ranges?: T[]);
|
|
630
|
+
/**
|
|
631
|
+
* 开启/关闭严格模式。开启后有如下限制:
|
|
632
|
+
* - 枚举中包含数字时,如果传递的值为字符串类型,则不再转换为数字后再对比
|
|
633
|
+
* - 枚举中包含布尔值时,如果传递的值为字符串类型,则不再转换为布尔值后再对比
|
|
634
|
+
*/
|
|
635
|
+
strict: (is?: boolean) => this;
|
|
636
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
637
|
+
optional: () => EnumValidator<T | Validator.TOptional>;
|
|
638
|
+
required: () => EnumValidator<Exclude<T, Validator.TOptional>>;
|
|
639
|
+
nullable: () => EnumValidator<T | null>;
|
|
640
|
+
default: (value: Validator.ParameterOrFn<T>) => EnumValidator<T | Validator.TDefault>;
|
|
641
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
642
|
+
/**
|
|
643
|
+
* 只保留指定枚举值,得到新的枚举验证器(保留 optional / nullable / default)。
|
|
644
|
+
*
|
|
645
|
+
* ```typescript
|
|
646
|
+
* rule.enum(['a', 'b', 'c']).pick('a', 'b');
|
|
647
|
+
* rule.enum(['a', 'b', 'c']).pick(['a', 'b']);
|
|
648
|
+
* ```
|
|
649
|
+
*/
|
|
650
|
+
pick<K extends EnumValidator.ValueOf<T>>(values: readonly K[]): EnumValidator<K | EnumValidator.FlagsOf<T>>;
|
|
651
|
+
pick<K extends EnumValidator.ValueOf<T>[]>(...values: [...K]): EnumValidator<K[number] | EnumValidator.FlagsOf<T>>;
|
|
652
|
+
/**
|
|
653
|
+
* 去掉指定枚举值,得到新的枚举验证器(保留 optional / nullable / default)。
|
|
654
|
+
*
|
|
655
|
+
* ```typescript
|
|
656
|
+
* rule.enum(['a', 'b', 'c']).omit('c');
|
|
657
|
+
* rule.enum(['a', 'b', 'c']).omit(['c']);
|
|
658
|
+
* ```
|
|
659
|
+
*/
|
|
660
|
+
omit<K extends EnumValidator.ValueOf<T>>(values: readonly K[]): EnumValidator<Exclude<EnumValidator.ValueOf<T>, K> | EnumValidator.FlagsOf<T>>;
|
|
661
|
+
omit<K extends EnumValidator.ValueOf<T>[]>(...values: [...K]): EnumValidator<Exclude<EnumValidator.ValueOf<T>, K[number]> | EnumValidator.FlagsOf<T>>;
|
|
662
|
+
protected copy: () => EnumValidator<T>;
|
|
663
|
+
/** ranges 为空,或 default 不在 ranges 内时抛错 */
|
|
664
|
+
protected assertConfigHealthy(): void;
|
|
665
|
+
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<any>;
|
|
666
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
667
|
+
}
|
|
668
|
+
export declare namespace HashValidator {
|
|
669
|
+
type Algorithm = keyof (typeof HashValidator)['algorithmLength'];
|
|
670
|
+
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
671
|
+
algorithm: HashValidator.Algorithm;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
export declare class HashValidator<T = string> extends BaseStringValidator<T> {
|
|
675
|
+
/**
|
|
676
|
+
* @see https://github.com/validatorjs/validator.js
|
|
677
|
+
*/
|
|
678
|
+
protected static algorithmLength: {
|
|
679
|
+
readonly md5: 32;
|
|
680
|
+
readonly md4: 32;
|
|
681
|
+
readonly sha1: 40;
|
|
682
|
+
readonly sha256: 64;
|
|
683
|
+
readonly sha384: 96;
|
|
684
|
+
readonly sha512: 128;
|
|
685
|
+
readonly ripemd128: 32;
|
|
686
|
+
readonly ripemd160: 40;
|
|
687
|
+
readonly tiger128: 32;
|
|
688
|
+
readonly tiger160: 40;
|
|
689
|
+
readonly tiger192: 48;
|
|
690
|
+
readonly crc32: 8;
|
|
691
|
+
readonly crc32b: 8;
|
|
692
|
+
};
|
|
693
|
+
protected static algorithmPattern: Record<string, RegExp>;
|
|
694
|
+
protected config: HashValidator.Options<T>;
|
|
695
|
+
constructor(algorithm: HashValidator.Algorithm);
|
|
696
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
697
|
+
optional: () => HashValidator<T | Validator.TOptional>;
|
|
698
|
+
required: () => HashValidator<Exclude<T, Validator.TOptional>>;
|
|
699
|
+
nullable: () => HashValidator<T | null>;
|
|
700
|
+
default: (algorithm: Validator.ParameterOrFn<T>) => HashValidator<T | Validator.TDefault>;
|
|
701
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
702
|
+
protected validateString(hash: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
703
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
704
|
+
}
|
|
705
|
+
export declare class IntValidator<T = number> extends BaseNumberValidator<T> {
|
|
706
|
+
protected config: BaseNumberValidator.Options<T>;
|
|
707
|
+
optional: () => IntValidator<T | Validator.TOptional>;
|
|
708
|
+
required: () => IntValidator<Exclude<T, Validator.TOptional>>;
|
|
709
|
+
nullable: () => IntValidator<T | null>;
|
|
710
|
+
default: (integer: Validator.ParameterOrFn<T>) => IntValidator<T | Validator.TDefault>;
|
|
711
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
712
|
+
protected validateNumber(num: number, _key: string, label: string): ValidateResult.Any<number>;
|
|
713
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
714
|
+
}
|
|
715
|
+
export declare namespace IpValidator {
|
|
716
|
+
type Version = 'v4' | 'v6';
|
|
717
|
+
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
718
|
+
ipVersion: IpValidator.Version[];
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
export declare class IpValidator<T = string> extends BaseStringValidator<T> {
|
|
722
|
+
protected config: IpValidator.Options<T>;
|
|
723
|
+
constructor(version: IpValidator.Version[]);
|
|
724
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
725
|
+
optional: () => IpValidator<T | Validator.TOptional>;
|
|
726
|
+
required: () => IpValidator<Exclude<T, Validator.TOptional>>;
|
|
727
|
+
nullable: () => IpValidator<T | null>;
|
|
728
|
+
default: (ip: Validator.ParameterOrFn<T>) => IpValidator<T | Validator.TDefault>;
|
|
729
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
730
|
+
match: (pattern: RegExp) => this;
|
|
731
|
+
protected validateString(ip: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
732
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
733
|
+
}
|
|
734
|
+
/** JSON 可序列化的字面量:`string` / `number` / `boolean` / `null`(不含 `NaN` / `Infinity`) */
|
|
735
|
+
export type LiteralValue = string | number | boolean | null;
|
|
736
|
+
export declare namespace LiteralValidator {
|
|
737
|
+
interface Options<T> extends Validator.Options<T> {
|
|
738
|
+
value: T;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
export declare class LiteralValidator<const T = never> extends Validator<T> {
|
|
742
|
+
protected config: LiteralValidator.Options<T>;
|
|
743
|
+
constructor(value?: T & LiteralValue);
|
|
744
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
745
|
+
optional: () => LiteralValidator<T | Validator.TOptional>;
|
|
746
|
+
required: () => LiteralValidator<Exclude<T, Validator.TOptional>>;
|
|
747
|
+
nullable: () => LiteralValidator<T | null>;
|
|
748
|
+
default: (value: Validator.ParameterOrFn<T>) => LiteralValidator<T | Validator.TDefault>;
|
|
749
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
750
|
+
protected copy: () => LiteralValidator<T>;
|
|
751
|
+
/**
|
|
752
|
+
* 字面量为 `''` 时,空字符串是合法取值,不能再被基类当成「空值」吞掉。
|
|
753
|
+
*/
|
|
754
|
+
protected isEmpty(value: any): boolean;
|
|
755
|
+
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<T>;
|
|
756
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
757
|
+
}
|
|
758
|
+
export declare namespace NumberValidator {
|
|
759
|
+
interface Options<T = false> extends BaseNumberValidator.Options<T> {}
|
|
760
|
+
}
|
|
761
|
+
export declare class NumberValidator<T = number> extends BaseNumberValidator<T> {
|
|
762
|
+
protected config: NumberValidator.Options<T>;
|
|
763
|
+
optional: () => NumberValidator<T | Validator.TOptional>;
|
|
764
|
+
required: () => NumberValidator<Exclude<T, Validator.TOptional>>;
|
|
765
|
+
nullable: () => NumberValidator<T | null>;
|
|
766
|
+
default: (number: Validator.ParameterOrFn<T>) => NumberValidator<T | Validator.TDefault>;
|
|
767
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
768
|
+
precision: (decimals: number) => this;
|
|
769
|
+
protected validateNumber(num: number): ValidateResult.Any<number>;
|
|
770
|
+
protected copy: () => this;
|
|
771
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
772
|
+
}
|
|
773
|
+
export declare namespace ObjectValidator {
|
|
774
|
+
type Property = Record<string, Validator>;
|
|
775
|
+
interface Options<T> extends Validator.Options<T> {
|
|
776
|
+
properties?: Property;
|
|
777
|
+
stringToObject?: boolean;
|
|
778
|
+
keepAdditional?: boolean | {
|
|
779
|
+
key?: RegExp[];
|
|
780
|
+
value?: Validator;
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
export declare class ObjectValidator<T = Validator.TObject> extends Validator<T> {
|
|
785
|
+
protected config: ObjectValidator.Options<T>;
|
|
786
|
+
constructor(properties?: ObjectValidator.Property);
|
|
787
|
+
/**
|
|
788
|
+
* 开启严格模式后:
|
|
789
|
+
* - 不从字符串解析出对象,除非通过`parseFromString()`手动指定
|
|
790
|
+
*/
|
|
791
|
+
strict: (is?: boolean) => this;
|
|
792
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
793
|
+
optional: () => ObjectValidator<T | Validator.TOptional>;
|
|
794
|
+
required: () => ObjectValidator<Exclude<T, Validator.TOptional>>;
|
|
795
|
+
nullable: () => ObjectValidator<T | null>;
|
|
796
|
+
default: (object: Validator.ParameterOrFn<T>) => ObjectValidator<T | Validator.TDefault>;
|
|
797
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
798
|
+
/**
|
|
799
|
+
* 尝试使用`JSON.parse`把字符串转换为对象,在非严格模式下默认开启
|
|
800
|
+
*/
|
|
801
|
+
parseFromString(is?: boolean): ObjectValidator<T>;
|
|
802
|
+
/**
|
|
803
|
+
* 保留未被验证的键值对。对象中包含动态字段时,无法使用具体的结构体描述出对象的组成。
|
|
804
|
+
*
|
|
805
|
+
* 有以下几种保留规则:
|
|
806
|
+
* 1. 不提供参数,则保留所有的属性和值;
|
|
807
|
+
* 2. 提供`key`,则保留 键 能匹配正则表达式的键值对;
|
|
808
|
+
* 3. 提供`value`,则保留 值 能匹配验证器的键值对,同时值会根据验证器逻辑做相应的修正;
|
|
809
|
+
* 4. 提供`key`和`value`,则保留能同时满足规则2和3的键值对。
|
|
810
|
+
*
|
|
811
|
+
* ```typescript
|
|
812
|
+
* const v = rule.object();
|
|
813
|
+
*
|
|
814
|
+
* // { a: 'x', b: 1, c: true } -> { a: 'x', b: 1, c: true }
|
|
815
|
+
* v.additional();
|
|
816
|
+
*
|
|
817
|
+
* // { a: 'x', b: 1, c: true } -> { a: 'x' }
|
|
818
|
+
* v.additional({ value: rule.string() });
|
|
819
|
+
*
|
|
820
|
+
* // { a: 'x', b: 1, c: true } -> { a: 'x', c: true }
|
|
821
|
+
* v.additional({ key: /^(a|c)$/ });
|
|
822
|
+
*
|
|
823
|
+
* // { a: 'x', b: 1, c: true } -> { c: true }
|
|
824
|
+
* v.additional({ key: /^(a|c)$/, value: rule.boolean() });
|
|
825
|
+
*
|
|
826
|
+
* // { a: 'x', b: 1, c: true } -> { }
|
|
827
|
+
* v.additional({ key: /^(a|c)$/, value: rule.number() });
|
|
828
|
+
* ```
|
|
829
|
+
*/
|
|
830
|
+
additional(): ObjectValidator<T extends Validator.TObject ? {
|
|
831
|
+
[K: string]: unknown;
|
|
832
|
+
} : T extends null ? null : T & {
|
|
833
|
+
[K: string]: unknown;
|
|
834
|
+
}>;
|
|
835
|
+
additional(opts: {
|
|
836
|
+
key: RegExp | RegExp[];
|
|
837
|
+
value?: undefined;
|
|
838
|
+
}): ObjectValidator<(T extends Validator.TObject ? unknown : T) & {
|
|
839
|
+
[K: string]: unknown;
|
|
840
|
+
}>;
|
|
841
|
+
additional<Additional extends ValidatorToken>(opts: {
|
|
842
|
+
key?: RegExp | RegExp[];
|
|
843
|
+
value: Additional;
|
|
844
|
+
}): ObjectValidator<T extends Validator.TObject ? {
|
|
845
|
+
[K: string]: Validator.Infer<Additional>;
|
|
846
|
+
} : T extends null ? null : T & {
|
|
847
|
+
[K: string]: Validator.Infer<Additional>;
|
|
848
|
+
}>;
|
|
849
|
+
protected isPlainObject(value: any): value is object;
|
|
850
|
+
protected validateValue(origin: Record<string, any>, key: string, label: string): Promise<ValidateResult.Any<object>>;
|
|
851
|
+
protected copy: () => ObjectValidator<T>;
|
|
852
|
+
protected nested(): Validator[];
|
|
853
|
+
protected copyConfig(prev: ObjectValidator): this;
|
|
854
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
855
|
+
}
|
|
856
|
+
export declare namespace OneOfValidator {
|
|
857
|
+
interface Options<T> extends Validator.Options<T> {
|
|
858
|
+
validators: Validator[];
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
export declare class OneOfValidator<T = never> extends Validator<T> {
|
|
862
|
+
protected config: OneOfValidator.Options<T>;
|
|
863
|
+
constructor(validators: [Validator, ...Validator[]]);
|
|
864
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
865
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
866
|
+
optional: () => OneOfValidator<T | Validator.TOptional>;
|
|
867
|
+
required: () => OneOfValidator<Exclude<T, Validator.TOptional>>;
|
|
868
|
+
nullable: () => OneOfValidator<T | null>;
|
|
869
|
+
protected isEmpty(_: any): boolean;
|
|
870
|
+
protected validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>>;
|
|
871
|
+
protected copyConfig(prev: OneOfValidator): this;
|
|
872
|
+
protected nested(): Validator[];
|
|
873
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
874
|
+
}
|
|
875
|
+
export declare namespace StreamValidator {
|
|
876
|
+
interface Options<T = stream.Stream> extends Validator.Options<T> {}
|
|
877
|
+
}
|
|
878
|
+
export declare class StreamValidator<T = stream.Stream> extends Validator<T> {
|
|
879
|
+
protected config: StreamValidator.Options<T>;
|
|
880
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
881
|
+
optional: () => StreamValidator<T | Validator.TOptional>;
|
|
882
|
+
required: () => StreamValidator<Exclude<T, Validator.TOptional>>;
|
|
883
|
+
nullable: () => StreamValidator<T | null>;
|
|
884
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
885
|
+
protected validateValue(value: stream.Stream, _key: string, label: string): ValidateResult.Any<stream.Stream>;
|
|
886
|
+
protected copy: () => this;
|
|
887
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
888
|
+
}
|
|
889
|
+
export declare namespace StringValidator {
|
|
890
|
+
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
891
|
+
allowEmpty?: boolean;
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
export declare class StringValidator<T = string> extends BaseStringValidator<T> {
|
|
895
|
+
protected config: StringValidator.Options<T>;
|
|
896
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
897
|
+
optional: () => StringValidator<T | Validator.TOptional>;
|
|
898
|
+
required: () => StringValidator<Exclude<T, Validator.TOptional>>;
|
|
899
|
+
nullable: () => StringValidator<T | null>;
|
|
900
|
+
default: (string: Validator.ParameterOrFn<T>) => StringValidator<T | Validator.TDefault>;
|
|
901
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
902
|
+
match: (pattern: RegExp) => this;
|
|
903
|
+
trim: (is?: boolean) => this;
|
|
904
|
+
length: (exactOrRange: number | BaseStringValidator.LengthRange) => this;
|
|
905
|
+
/**
|
|
906
|
+
* 把空字符串`''`设置为合法的值
|
|
907
|
+
*/
|
|
908
|
+
allowEmptyString(): StringValidator<T>;
|
|
909
|
+
protected isEmpty(value: any): boolean;
|
|
910
|
+
protected validateString(value: string): ValidateResult.Any<string>;
|
|
911
|
+
protected copy: () => StringValidator<T>;
|
|
912
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
913
|
+
}
|
|
914
|
+
export declare namespace UlidValidator {
|
|
915
|
+
interface Options<T = string> extends BaseStringValidator.Options<T> {}
|
|
916
|
+
}
|
|
917
|
+
export declare class UlidValidator<T = string> extends BaseStringValidator<T> {
|
|
918
|
+
protected config: UlidValidator.Options<T>;
|
|
919
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
920
|
+
optional: () => UlidValidator<T | Validator.TOptional>;
|
|
921
|
+
required: () => UlidValidator<Exclude<T, Validator.TOptional>>;
|
|
922
|
+
nullable: () => UlidValidator<T | null>;
|
|
923
|
+
default: (uuid: Validator.ParameterOrFn<T>) => UlidValidator<T | Validator.TDefault>;
|
|
924
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
925
|
+
protected validateString(ulid: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
926
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
927
|
+
}
|
|
928
|
+
export declare namespace UrlValidator {
|
|
929
|
+
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
930
|
+
allowedScheme: string[];
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
export declare class UrlValidator<T = string> extends BaseStringValidator<T> {
|
|
934
|
+
protected config: UrlValidator.Options<T>;
|
|
935
|
+
constructor();
|
|
936
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
937
|
+
optional: () => UrlValidator<T | Validator.TOptional>;
|
|
938
|
+
required: () => UrlValidator<Exclude<T, Validator.TOptional>>;
|
|
939
|
+
nullable: () => UrlValidator<T | null>;
|
|
940
|
+
default: (Url: Validator.ParameterOrFn<T>) => UrlValidator<T | Validator.TDefault>;
|
|
941
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
942
|
+
/**
|
|
943
|
+
* 允许使用的协议,不区分大小写。默认配置:`['http', 'https']`
|
|
944
|
+
*/
|
|
945
|
+
scheme(scheme: string[]): UrlValidator<T>;
|
|
946
|
+
protected validateString(url: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
947
|
+
protected copy: () => UrlValidator<T>;
|
|
948
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
949
|
+
}
|
|
950
|
+
export declare namespace UuidValidator {
|
|
951
|
+
type Version = (typeof UuidValidator.versions)[number];
|
|
952
|
+
interface Options<T = string> extends BaseStringValidator.Options<T> {
|
|
953
|
+
uuidVersion: Version[];
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
export declare class UuidValidator<T = string> extends BaseStringValidator<T> {
|
|
957
|
+
static versions: readonly ['v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8'];
|
|
958
|
+
static patterns: { [key in UuidValidator.Version | 'all']: RegExp; };
|
|
959
|
+
protected config: UuidValidator.Options<T>;
|
|
960
|
+
constructor(versions: UuidValidator.Version[]);
|
|
961
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
962
|
+
optional: () => UuidValidator<T | Validator.TOptional>;
|
|
963
|
+
required: () => UuidValidator<Exclude<T, Validator.TOptional>>;
|
|
964
|
+
nullable: () => UuidValidator<T | null>;
|
|
965
|
+
default: (uuid: Validator.ParameterOrFn<T>) => UuidValidator<T | Validator.TDefault>;
|
|
966
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
967
|
+
match: (pattern: RegExp) => this;
|
|
968
|
+
protected validateString(uuid: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
969
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* 可递归嵌套的字段表;裸对象会在 `toValidator` / `schema` 入库时转为 `rule.object(...)`
|
|
973
|
+
*/
|
|
974
|
+
export type ValidatorFieldTable = {
|
|
975
|
+
[key: string]: ValidatorToken;
|
|
976
|
+
};
|
|
977
|
+
export type ValidatorToken = Validator | ValidatorSchema<any> | ValidatorFieldTable;
|
|
978
|
+
/**
|
|
979
|
+
* 将字段表中的嵌套裸对象 / schema 规范化为 Validator,供 `schema` 入库使用
|
|
980
|
+
*/
|
|
981
|
+
export type NormalizeFieldTable<T> = { [K in keyof T]: T[K] extends Validator ? T[K] : T[K] extends ValidatorSchema<any> ? ObjectValidator<Validator.Infer<T[K]>> : T[K] extends object ? ObjectValidator<Validator.Infer<T[K]>> : never; };
|
|
982
|
+
export declare function toValidator(validator: ValidatorToken): Validator;
|
|
983
|
+
export declare function toValidator(validator: undefined): undefined;
|
|
984
|
+
export declare function toValidator(validator: ValidatorToken | undefined): Validator | undefined;
|
|
985
|
+
/**
|
|
986
|
+
* 逐字段转为 Validator;嵌套裸对象 / schema 经 `toValidator` 转为 `ObjectValidator`
|
|
987
|
+
*/
|
|
988
|
+
export declare function normalizeFieldTable(fields: Record<string, ValidatorToken>): Record<string, Validator>;
|
|
989
|
+
export declare function toDocument(source: ValidatorToken): OpenAPI.ParameterBaseObject;
|
|
990
|
+
export declare class Rule {
|
|
991
|
+
static register<T extends Rule, K extends keyof T>(this: new (...args: any[]) => T, name: K, SubValidator: new (...args: any[]) => Validator): void;
|
|
992
|
+
/**
|
|
993
|
+
* 规则以管道形式执行,需要匹配所有规则
|
|
994
|
+
* ```typescript
|
|
995
|
+
* rule.allOf([rule.number(), rule.string()]);
|
|
996
|
+
* ```
|
|
997
|
+
*/
|
|
998
|
+
allOf<T extends ValidatorToken[], A extends ValidatorToken, B extends ValidatorToken>(rules: [rule1: A, rule2: B, ...others: T]): AllOfValidator<Validator.Infer<A> & Validator.Infer<B> & Union2Intersection<{ [K in keyof T]: Validator.Infer<T[K]>; }[number]>>;
|
|
999
|
+
/**
|
|
1000
|
+
* 允许任何类型:
|
|
1001
|
+
* - string
|
|
1002
|
+
* - number
|
|
1003
|
+
* - boolean
|
|
1004
|
+
* - array
|
|
1005
|
+
* - object
|
|
1006
|
+
* - buffer
|
|
1007
|
+
*/
|
|
1008
|
+
any(): AnyValidator<AnyValidator.Type>;
|
|
1009
|
+
/**
|
|
1010
|
+
* 规则以管道形式执行,需要至少匹配一个规则
|
|
1011
|
+
* ```typescript
|
|
1012
|
+
* rule.anyOf([rule.number(), rule.string()]);
|
|
1013
|
+
* ```
|
|
1014
|
+
*/
|
|
1015
|
+
anyOf<T extends ValidatorToken[], A extends ValidatorToken, B extends ValidatorToken>(rules: [rule1: A, rule2: B, ...others: T]): AnyOfValidator<Validator.Infer<A> | Validator.Infer<B> | { [K in keyof T]: Validator.Infer<T[K]>; }[number]>;
|
|
1016
|
+
array(): ArrayValidator<unknown[]>;
|
|
1017
|
+
array<T extends Validator>(item: T): ArrayValidator<Validator.Infer<T>[]>;
|
|
1018
|
+
array<T extends ValidatorSchema<any>>(item: T): ArrayValidator<Validator.Infer<T>[]>;
|
|
1019
|
+
/**
|
|
1020
|
+
* 对象验证规则的快捷操作,等价于 `rule.array(rule.object({...}))`;
|
|
1021
|
+
* 嵌套裸对象同样会转为 `rule.object`
|
|
1022
|
+
*/
|
|
1023
|
+
array<T extends {
|
|
1024
|
+
[key: string]: ValidatorToken;
|
|
1025
|
+
}>(ruleObject: T): ArrayValidator<Validator.Infer<T>[]>;
|
|
1026
|
+
/**
|
|
1027
|
+
* 大整数类型
|
|
1028
|
+
*
|
|
1029
|
+
* 注意:非严格模式下,`number` 和 `string` 会尝试被转换成 `bigint` 类型
|
|
1030
|
+
*/
|
|
1031
|
+
bigint(): BigIntValidator<bigint>;
|
|
1032
|
+
/**
|
|
1033
|
+
* - 默认真值:`[1, '1', true, 'true']`
|
|
1034
|
+
* - 默认假值:`[0, '0', false, 'false']`
|
|
1035
|
+
*/
|
|
1036
|
+
boolean(): BooleanValidator<boolean>;
|
|
1037
|
+
buffer<T extends Buffer>(): BufferValidator<T>;
|
|
1038
|
+
/**
|
|
1039
|
+
* @deprecated 请使用 rule.date()
|
|
1040
|
+
*/
|
|
1041
|
+
dateTime(...formats: string[]): DateValidator<Date>;
|
|
1042
|
+
/**
|
|
1043
|
+
* @param formats 设置解析字符串的格式,支持同时指定不同的格式并从左到右依次尝试。如果是标准的ISO时间格式或者时间戳则无需指定。
|
|
1044
|
+
*
|
|
1045
|
+
* @link https://moment.github.io/luxon/#/formatting?id=table-of-tokens
|
|
1046
|
+
*/
|
|
1047
|
+
date(...formats: string[]): DateValidator<Date>;
|
|
1048
|
+
/**
|
|
1049
|
+
* 高精度小数(decimal.js)
|
|
1050
|
+
*
|
|
1051
|
+
* 注意:非严格模式下,`number` 和 `string` 会尝试被转换成 `Decimal` 类型
|
|
1052
|
+
*/
|
|
1053
|
+
decimal(): DecimalValidator<import("decimal.js").Decimal>;
|
|
1054
|
+
email(): EmailValidator<string>;
|
|
1055
|
+
/**
|
|
1056
|
+
* ```typescript
|
|
1057
|
+
* rule.enum(['a', 'b', 1]);
|
|
1058
|
+
* rule.enum(Color); // TS enum / 枚举对象
|
|
1059
|
+
* ```
|
|
1060
|
+
*/
|
|
1061
|
+
enum<const T extends string | number | boolean>(ranges: T[]): EnumValidator<T>;
|
|
1062
|
+
enum<const T extends readonly (string | number | boolean)[]>(ranges: T): EnumValidator<T[number]>;
|
|
1063
|
+
enum<const T extends EnumLike>(enumeration: T): EnumValidator<EnumLikeValues<T>>;
|
|
1064
|
+
hash(algorithm: HashValidator.Algorithm): HashValidator<string>;
|
|
1065
|
+
/**
|
|
1066
|
+
* 注意:非严格模式下,`string` 会尝试被转换成 `number` 类型
|
|
1067
|
+
*/
|
|
1068
|
+
int(): IntValidator<number>;
|
|
1069
|
+
/**
|
|
1070
|
+
* ```typescript
|
|
1071
|
+
* rule.ip('v4');
|
|
1072
|
+
* rule.ip('v6');
|
|
1073
|
+
* rule.ip(['v4', 'v6']);
|
|
1074
|
+
* ```
|
|
1075
|
+
*/
|
|
1076
|
+
ip(version: IpValidator.Version | [IpValidator.Version, ...IpValidator.Version[]]): IpValidator<string>;
|
|
1077
|
+
/**
|
|
1078
|
+
* 精确匹配单个字面量(不做类型转换)
|
|
1079
|
+
* ```typescript
|
|
1080
|
+
* rule.literal('ok');
|
|
1081
|
+
* rule.literal(1);
|
|
1082
|
+
* rule.literal(true);
|
|
1083
|
+
* rule.literal(null);
|
|
1084
|
+
* ```
|
|
1085
|
+
*/
|
|
1086
|
+
literal<const T extends LiteralValue>(value: T): LiteralValidator<T>;
|
|
1087
|
+
/**
|
|
1088
|
+
* 注意:非严格模式下,`string` 会尝试被转换成 `number` 类型
|
|
1089
|
+
*/
|
|
1090
|
+
number(): NumberValidator<number>;
|
|
1091
|
+
/**
|
|
1092
|
+
* ```typescript
|
|
1093
|
+
* rule.object();
|
|
1094
|
+
* rule.object({
|
|
1095
|
+
* id: rule.int(),
|
|
1096
|
+
* profile: rule.object({
|
|
1097
|
+
* sex: rule.enum(['unknown', 'male', 'female']),
|
|
1098
|
+
* }),
|
|
1099
|
+
* });
|
|
1100
|
+
* rule.object(userSchema);
|
|
1101
|
+
* ```
|
|
1102
|
+
*/
|
|
1103
|
+
object(): ObjectValidator<Validator.TObject>;
|
|
1104
|
+
object<T extends ValidatorSchema<any>>(properties: T): ObjectValidator<Validator.Infer<T>>;
|
|
1105
|
+
/**
|
|
1106
|
+
* 字段表可嵌套裸对象,等价于内层再包一层 `rule.object`
|
|
1107
|
+
*/
|
|
1108
|
+
object<T extends {
|
|
1109
|
+
[key: string]: ValidatorToken;
|
|
1110
|
+
}>(properties: T): ObjectValidator<Validator.Infer<T>>;
|
|
1111
|
+
/**
|
|
1112
|
+
* 动态键对象(`Record<string, V>`),等价于 `rule.object().additional({ value })`
|
|
1113
|
+
* ```typescript
|
|
1114
|
+
* rule.record(rule.number());
|
|
1115
|
+
* rule.record({ id: rule.int() });
|
|
1116
|
+
* ```
|
|
1117
|
+
*/
|
|
1118
|
+
record<V extends ValidatorToken>(value: V): ObjectValidator<{
|
|
1119
|
+
[K: string]: Validator.Infer<V>;
|
|
1120
|
+
}>;
|
|
1121
|
+
/**
|
|
1122
|
+
* 必须且只能匹配其中一个规则,如果匹配上多个则失败
|
|
1123
|
+
* ```typescript
|
|
1124
|
+
* rule.oneOf([rule.number(), rule.string()]);
|
|
1125
|
+
* ```
|
|
1126
|
+
*/
|
|
1127
|
+
oneOf<T extends ValidatorToken[], A extends ValidatorToken, B extends ValidatorToken>(rules: [rule1: A, rule2: B, ...others: T]): OneOfValidator<Validator.Infer<A> | Validator.Infer<B> | { [K in keyof T]: Validator.Infer<T[K]>; }[number]>;
|
|
1128
|
+
/**
|
|
1129
|
+
* 数据流
|
|
1130
|
+
*/
|
|
1131
|
+
stream(): StreamValidator<import("stream")>;
|
|
1132
|
+
string(): StringValidator<string>;
|
|
1133
|
+
ulid(): UlidValidator<string>;
|
|
1134
|
+
url(): UrlValidator<string>;
|
|
1135
|
+
/**
|
|
1136
|
+
* ```typescript
|
|
1137
|
+
* rule.uuid('v4');
|
|
1138
|
+
* rule.uuid(['v4', 'v5', 'v6']);
|
|
1139
|
+
* ```
|
|
1140
|
+
*/
|
|
1141
|
+
uuid(): UuidValidator<string>;
|
|
1142
|
+
uuid(version: UuidValidator.Version): UuidValidator<string>;
|
|
1143
|
+
uuid(versions: [UuidValidator.Version, ...UuidValidator.Version[]]): UuidValidator<string>;
|
|
1144
|
+
}
|
|
1145
|
+
export declare const rule: Rule;
|
|
1146
|
+
export type ValidateOptions = {
|
|
1147
|
+
/**
|
|
1148
|
+
* - `true`(默认):失败时抛出 `ValidateDeniedError`
|
|
1149
|
+
* - `false`:失败时不抛错,返回 `{ data: null, errors }`
|
|
1150
|
+
*/
|
|
1151
|
+
throwOnError?: boolean;
|
|
1152
|
+
/**
|
|
1153
|
+
* 报错文字格式化处理(仅 `throwOnError: true` 时生效)
|
|
1154
|
+
*/
|
|
1155
|
+
errorFormatter?: (errors: ValidateResult.Denied['errors']) => string | Promise<string>;
|
|
1156
|
+
};
|
|
860
1157
|
/**
|
|
861
1158
|
* 验证传入数据,并返回可信的数据
|
|
862
1159
|
*
|
|
863
|
-
*
|
|
864
|
-
|
|
865
|
-
|
|
1160
|
+
* 默认失败抛错;传入 `{ throwOnError: false }` 时改为返回 Result。
|
|
1161
|
+
*/
|
|
1162
|
+
export declare function validate<T extends ValidatorToken>(untrusted: any, validators: T, options?: {
|
|
1163
|
+
throwOnError?: true;
|
|
1164
|
+
errorFormatter?: ValidateOptions['errorFormatter'];
|
|
1165
|
+
}): Promise<Validator.Infer<T>>;
|
|
1166
|
+
export declare function validate<T extends ValidatorToken>(untrusted: any, validators: T, options: {
|
|
1167
|
+
throwOnError: false;
|
|
1168
|
+
}): Promise<ValidateResult.Safe<Validator.Infer<T>>>;
|
|
1169
|
+
/**
|
|
1170
|
+
* 创建可派生的字段集合,可直接或经 pick/omit/extend/partial/required 后用于校验
|
|
1171
|
+
*
|
|
1172
|
+
* 嵌套裸对象会在入库前转为 `rule.object(...)`:
|
|
1173
|
+
* ```typescript
|
|
1174
|
+
* schema({
|
|
1175
|
+
* profile: { bio: rule.string() }, // 等价于 rule.object({ bio: rule.string() })
|
|
1176
|
+
* });
|
|
1177
|
+
* ```
|
|
1178
|
+
*
|
|
1179
|
+
* ```typescript
|
|
1180
|
+
* const userSchema = schema({
|
|
1181
|
+
* id: rule.int(),
|
|
1182
|
+
* name: rule.string(),
|
|
1183
|
+
* email: rule.email(),
|
|
1184
|
+
* });
|
|
1185
|
+
*
|
|
1186
|
+
* const createUserSchema = userSchema.omit('id');
|
|
1187
|
+
* const patchUserSchema = userSchema.pick('name', 'email').partial();
|
|
1188
|
+
* const requiredSchema = patchUserSchema.required();
|
|
1189
|
+
* type User = Validator.Infer<typeof userSchema>;
|
|
1190
|
+
* ```
|
|
866
1191
|
*/
|
|
867
|
-
declare const
|
|
868
|
-
|
|
869
|
-
}) => Promise<Validator.Infer<T>>;
|
|
870
|
-
|
|
871
|
-
export { AllOfValidator, AnyOfValidator, AnyValidator, ArrayValidator, BaseNumberValidator, BaseStringValidator, BigIntValidator, BooleanValidator, BufferValidator, DateValidator, EmailValidator, EnumValidator, HashValidator, IntValidator, IpValidator, NumberValidator, ObjectValidator, OneOfValidator, Rule, StreamValidator, StringValidator, type TransformedValidator, UlidValidator, UrlValidator, UuidValidator, ValidateDeniedError, ValidateResult, Validator, type ValidatorToken, rule, toValidator, validate };
|
|
1192
|
+
export declare const schema: <T extends { [K in keyof T]: ValidatorToken; }>(rules: T) => ValidatorSchema<NormalizeFieldTable<T>>;
|
|
1193
|
+
export { Decimal };
|