@aomex/common-validator 3.9.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/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/dist/index.d.ts +858 -0
- package/dist/index.js +1561 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,858 @@
|
|
|
1
|
+
import { OpenAPI, Union2Intersection } from '@aomex/internal-tools';
|
|
2
|
+
import * as stream from 'stream';
|
|
3
|
+
import stream$1 from 'node:stream';
|
|
4
|
+
|
|
5
|
+
declare namespace ValidateResult {
|
|
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>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
interface TransformedValidator<T> extends Validator<T> {
|
|
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 PartialOpenAPISchema = Pick<OpenAPI.SchemaObject, 'title' | 'description' | 'deprecated' | 'example' | 'externalDocs'>;
|
|
50
|
+
type DocumentMergeMode = 'merge' | 'replace';
|
|
51
|
+
interface Options<Type> {
|
|
52
|
+
defaultValue?: Type | (() => Type);
|
|
53
|
+
required: boolean;
|
|
54
|
+
nullable: boolean;
|
|
55
|
+
transform?: (value: any) => Promise<any> | any;
|
|
56
|
+
docs?: PartialOpenAPISchema;
|
|
57
|
+
strict: boolean;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
declare abstract class Validator<T = unknown> {
|
|
61
|
+
protected readonly SubClass: new (...args: any[]) => Validator;
|
|
62
|
+
constructor();
|
|
63
|
+
protected readonly config: Validator.Options<T>;
|
|
64
|
+
static toDocument(validator: Validator): OpenAPI.ParameterBaseObject;
|
|
65
|
+
/**
|
|
66
|
+
* @param is 是否开启,默认值:`true`
|
|
67
|
+
*/
|
|
68
|
+
protected strict(is?: boolean): this;
|
|
69
|
+
/**
|
|
70
|
+
* 扩展openapi的配置
|
|
71
|
+
* - **replace:** 本次设置直接覆盖原来的扩展配置
|
|
72
|
+
* - **merge:** 本次设置通过`Object.assign`的形式合并到原来的扩展配置
|
|
73
|
+
* @param mode 默认值:`merge`
|
|
74
|
+
*/
|
|
75
|
+
protected docs(docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode): this;
|
|
76
|
+
/**
|
|
77
|
+
* 值如果是 `undefined, null, ''`或者没传入,以上情况会直接转换成`undefined`
|
|
78
|
+
*
|
|
79
|
+
* 注意:如果执行了`default(...)`,则无需再执行`optional()`
|
|
80
|
+
*
|
|
81
|
+
* 注意:如果设置了`nullable()`,则`null`不做处理
|
|
82
|
+
*/
|
|
83
|
+
protected optional(): Validator;
|
|
84
|
+
/**
|
|
85
|
+
* 把`null`识别成合法的值
|
|
86
|
+
* @see optional
|
|
87
|
+
*/
|
|
88
|
+
protected nullable(): Validator;
|
|
89
|
+
/**
|
|
90
|
+
* 数据验证成功最后会触发该方法,你可以把数据转换成任何格式
|
|
91
|
+
* ```typescript
|
|
92
|
+
* // 最终返回类型:string
|
|
93
|
+
* rule.string()
|
|
94
|
+
* // 最终返回类型:Array<string>
|
|
95
|
+
* rule.string().transform((value) => [value])
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
protected transform<T1>(fn: (value: any) => Promise<T1> | T1): Validator;
|
|
99
|
+
/**
|
|
100
|
+
* 如果值为空或者没传入,则使用该默认值,而且无需验证
|
|
101
|
+
*
|
|
102
|
+
* 注意:如果执行了`default(...)`,则无需再执行`optional()`
|
|
103
|
+
*/
|
|
104
|
+
protected default(value: any): Validator;
|
|
105
|
+
protected validate(value: any, key?: string, label?: string): Promise<ValidateResult.Any<any>>;
|
|
106
|
+
protected isEmpty(value: any): boolean;
|
|
107
|
+
protected isValidNull(value: any): boolean;
|
|
108
|
+
protected getDefaultValue<T = any>(value: any): T | undefined;
|
|
109
|
+
protected abstract validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>> | ValidateResult.Any<any>;
|
|
110
|
+
protected copy(): Validator;
|
|
111
|
+
protected copyConfig(prev: Validator): this;
|
|
112
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
declare namespace BaseStringValidator {
|
|
116
|
+
interface Options<T> extends Validator.Options<T> {
|
|
117
|
+
trim?: boolean;
|
|
118
|
+
pattern?: RegExp;
|
|
119
|
+
lengthRange?: LengthRange;
|
|
120
|
+
}
|
|
121
|
+
interface LengthRange {
|
|
122
|
+
min?: number;
|
|
123
|
+
max?: number;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
declare abstract class BaseStringValidator<T = string> extends Validator<T> {
|
|
127
|
+
protected config: BaseStringValidator.Options<T>;
|
|
128
|
+
/**
|
|
129
|
+
* 删除两边空格后再进行验证
|
|
130
|
+
*/
|
|
131
|
+
protected trim(is?: boolean): this;
|
|
132
|
+
protected length(exactLength: number): this;
|
|
133
|
+
protected length(range: BaseStringValidator.LengthRange): this;
|
|
134
|
+
protected match(pattern: RegExp): this;
|
|
135
|
+
protected isEmpty(value: any): boolean;
|
|
136
|
+
protected getTrimValue(value: string): string;
|
|
137
|
+
protected validateValue(value: any, key: string, label: string): ValidateResult.Any<any> | Promise<ValidateResult.Any<any>>;
|
|
138
|
+
protected abstract validateString(value: string, key: string, label: string): ValidateResult.Any<any> | Promise<ValidateResult.Any<any>>;
|
|
139
|
+
protected copy: () => BaseStringValidator<T>;
|
|
140
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
declare namespace BaseNumberValidator {
|
|
144
|
+
interface Options<T = false> extends Validator.Options<T> {
|
|
145
|
+
min?: number;
|
|
146
|
+
minInclusive?: boolean;
|
|
147
|
+
max?: number;
|
|
148
|
+
maxInclusive?: boolean;
|
|
149
|
+
precision?: number;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
declare abstract class BaseNumberValidator<T = number> extends Validator<T> {
|
|
153
|
+
protected config: BaseNumberValidator.Options<T>;
|
|
154
|
+
/**
|
|
155
|
+
* 开启/关闭严格模式,开启后有以下限制:
|
|
156
|
+
* - 字符串不再尝试转换为数字
|
|
157
|
+
*/
|
|
158
|
+
strict: (is?: boolean) => this;
|
|
159
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
160
|
+
/**
|
|
161
|
+
* 最小范围限制
|
|
162
|
+
* @param min 最小值
|
|
163
|
+
* @param inclusive 是否包含最小值,默认值:`true`
|
|
164
|
+
*/
|
|
165
|
+
min(min: number, inclusive?: boolean): this;
|
|
166
|
+
/**
|
|
167
|
+
* 最大范围限制
|
|
168
|
+
* @param min 最大值
|
|
169
|
+
* @param inclusive 是否包含最大值,默认值:`true`
|
|
170
|
+
*/
|
|
171
|
+
max(max: number, inclusive?: boolean): this;
|
|
172
|
+
/**
|
|
173
|
+
* 小数点保留位数,使用`.toFixed()`剔除多余的小数
|
|
174
|
+
*/
|
|
175
|
+
protected precision(decimals: number): this;
|
|
176
|
+
protected validateValue(num: number, key: string, label: string): ValidateResult.Any<number>;
|
|
177
|
+
protected abstract validateNumber(num: number, key: string, label: string): ValidateResult.Any<number>;
|
|
178
|
+
protected copy: () => BaseNumberValidator<T>;
|
|
179
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
declare class ValidateDeniedError extends Error {
|
|
183
|
+
protected readonly errors: ValidateResult.Denied['errors'];
|
|
184
|
+
constructor(message: string | undefined, errors?: ValidateResult.Denied['errors']);
|
|
185
|
+
getValidateErrors(): string[];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
type ValidatorToken = Validator | {
|
|
189
|
+
[key: string]: Validator;
|
|
190
|
+
};
|
|
191
|
+
declare function toValidator(validator: ValidatorToken): Validator;
|
|
192
|
+
declare function toValidator(validator: undefined): undefined;
|
|
193
|
+
declare function toValidator(validator: ValidatorToken | undefined): Validator | undefined;
|
|
194
|
+
|
|
195
|
+
declare namespace AllOfValidator {
|
|
196
|
+
interface Options<T> extends Validator.Options<T> {
|
|
197
|
+
validators: Validator[];
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
declare class AllOfValidator<T = never> extends Validator<T> {
|
|
201
|
+
protected config: AllOfValidator.Options<T>;
|
|
202
|
+
constructor(validators: [Validator, ...Validator[]]);
|
|
203
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
204
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
205
|
+
protected isEmpty(_: any): boolean;
|
|
206
|
+
protected validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>>;
|
|
207
|
+
protected copyConfig(prev: AllOfValidator): this;
|
|
208
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
declare namespace AnyValidator {
|
|
212
|
+
type Type = number | string | boolean | any[] | {
|
|
213
|
+
[K: string]: unknown;
|
|
214
|
+
} | bigint | Buffer;
|
|
215
|
+
}
|
|
216
|
+
declare class AnyValidator<T = AnyValidator.Type> extends Validator<T> {
|
|
217
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
218
|
+
optional: () => AnyValidator<T | Validator.TOptional>;
|
|
219
|
+
nullable: () => AnyValidator<T | null>;
|
|
220
|
+
default: (anyValue: Validator.ParameterOrFn<T>) => AnyValidator<T | Validator.TDefault>;
|
|
221
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
222
|
+
protected validateValue(value: any): ValidateResult.Any<any>;
|
|
223
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
declare namespace AnyOfValidator {
|
|
227
|
+
interface Options<T> extends Validator.Options<T> {
|
|
228
|
+
validators: Validator[];
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
declare class AnyOfValidator<T = never> extends Validator<T> {
|
|
232
|
+
protected config: AnyOfValidator.Options<T>;
|
|
233
|
+
constructor(validators: [Validator, ...Validator[]]);
|
|
234
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
235
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
236
|
+
protected isEmpty(_: any): boolean;
|
|
237
|
+
protected validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>>;
|
|
238
|
+
protected copyConfig(prev: AnyOfValidator): this;
|
|
239
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
declare namespace ArrayValidator {
|
|
243
|
+
interface Options<T> extends Validator.Options<T> {
|
|
244
|
+
itemValidator?: Validator;
|
|
245
|
+
lengthRange: {
|
|
246
|
+
min?: number;
|
|
247
|
+
max?: number;
|
|
248
|
+
};
|
|
249
|
+
fromString?: string | RegExp;
|
|
250
|
+
force?: {
|
|
251
|
+
mode: 'separator' | 'block';
|
|
252
|
+
separator?: string | RegExp;
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
interface LengthRange {
|
|
256
|
+
min?: number;
|
|
257
|
+
max?: number;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
declare class ArrayValidator<T = unknown[]> extends Validator<T> {
|
|
261
|
+
protected config: ArrayValidator.Options<T>;
|
|
262
|
+
constructor(validator?: Validator);
|
|
263
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
264
|
+
optional: () => ArrayValidator<T | Validator.TOptional>;
|
|
265
|
+
nullable: () => ArrayValidator<T | null>;
|
|
266
|
+
default: (array: Validator.ParameterOrFn<T>) => ArrayValidator<T | Validator.TDefault>;
|
|
267
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
268
|
+
/**
|
|
269
|
+
* 强制将`非数组`的值转换成数组类型。控制台命令行和url查询字符串场景下,如果只传了一个元素,则有可能被识别为非数组,而传递多个元素时又变成了数组结构。
|
|
270
|
+
*
|
|
271
|
+
* @param mode 转换模式
|
|
272
|
+
*
|
|
273
|
+
* - `separator` 使用分割符号,把字符串拆分成数组
|
|
274
|
+
* - `block` 把输入当成一个整体,作为数组的一个元素
|
|
275
|
+
*
|
|
276
|
+
* @param separator 分割符号,默认值:`new RegExp('\s*,\s*')`
|
|
277
|
+
*/
|
|
278
|
+
forceToArray(mode: 'separator', separator?: string | RegExp): ArrayValidator<T>;
|
|
279
|
+
forceToArray(mode: 'block'): ArrayValidator<T>;
|
|
280
|
+
length(exactLength: number): this;
|
|
281
|
+
length(range: ArrayValidator.LengthRange): this;
|
|
282
|
+
protected validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any[]>>;
|
|
283
|
+
protected copy: () => ArrayValidator<T>;
|
|
284
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
declare namespace BigIntValidator {
|
|
288
|
+
interface Options<T = false> extends Validator.Options<T> {
|
|
289
|
+
min?: bigint;
|
|
290
|
+
minInclusive?: boolean;
|
|
291
|
+
max?: bigint;
|
|
292
|
+
maxInclusive?: boolean;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
declare class BigIntValidator<T = bigint> extends Validator<T> {
|
|
296
|
+
protected config: BigIntValidator.Options<T>;
|
|
297
|
+
/**
|
|
298
|
+
* 开启严格模式后:
|
|
299
|
+
* - string或者number不再转换为大整数
|
|
300
|
+
*/
|
|
301
|
+
strict: (is?: boolean) => this;
|
|
302
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
303
|
+
optional: () => BigIntValidator<T | Validator.TOptional>;
|
|
304
|
+
nullable: () => BigIntValidator<T | null>;
|
|
305
|
+
default: (value: bigint) => BigIntValidator<T | Validator.TDefault>;
|
|
306
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
307
|
+
min(min: bigint, inclusive?: boolean): this;
|
|
308
|
+
max(max: bigint, inclusive?: boolean): this;
|
|
309
|
+
protected validateValue(value: bigint, _key: string, label: string): ValidateResult.Any<bigint>;
|
|
310
|
+
protected copy: () => BigIntValidator<T>;
|
|
311
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
declare namespace BooleanValidator {
|
|
315
|
+
interface Options<T> extends Validator.Options<T> {
|
|
316
|
+
truthyValues?: any[];
|
|
317
|
+
falsyValues?: any[];
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
declare class BooleanValidator<T = boolean> extends Validator<T> {
|
|
321
|
+
protected config: BooleanValidator.Options<T>;
|
|
322
|
+
protected static defaultTruthyValues: any[];
|
|
323
|
+
protected static defaultFalsyValues: any[];
|
|
324
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
325
|
+
optional: () => BooleanValidator<T | Validator.TOptional>;
|
|
326
|
+
nullable: () => BooleanValidator<T | null>;
|
|
327
|
+
default: (boolValue: boolean) => BooleanValidator<T | Validator.TDefault>;
|
|
328
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
329
|
+
/**
|
|
330
|
+
* 重新设置真值。默认值:`[1, '1', true, 'true']`
|
|
331
|
+
* ```typescript
|
|
332
|
+
* boolean.setTruthyValues([true, 'yes']);
|
|
333
|
+
* boolean.setTruthyValues(['on', 'good']);
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
setTruthyValues(values: any[]): BooleanValidator<T>;
|
|
337
|
+
/**
|
|
338
|
+
* 重新设置假值。默认值:`[0, '0', false, 'false']`
|
|
339
|
+
* ```typescript
|
|
340
|
+
* boolean.setFalsyValues([false, 'no']);
|
|
341
|
+
* boolean.setFalsyValues(['off', 'bad']);
|
|
342
|
+
* ```
|
|
343
|
+
*/
|
|
344
|
+
setFalsyValues(values: any[]): BooleanValidator<T>;
|
|
345
|
+
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<boolean>;
|
|
346
|
+
protected copy: () => BooleanValidator<T>;
|
|
347
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
declare namespace BufferValidator {
|
|
351
|
+
interface Options<T = Buffer> extends Validator.Options<T> {
|
|
352
|
+
fromEncodings?: Encodings[];
|
|
353
|
+
}
|
|
354
|
+
type Encodings = 'base64' | 'hex';
|
|
355
|
+
}
|
|
356
|
+
declare class BufferValidator<T = Buffer> extends Validator<T> {
|
|
357
|
+
protected config: BufferValidator.Options<T>;
|
|
358
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
359
|
+
optional: () => BufferValidator<T | Validator.TOptional>;
|
|
360
|
+
nullable: () => BufferValidator<T | null>;
|
|
361
|
+
default: (buffer: Buffer) => BufferValidator<T | Validator.TDefault>;
|
|
362
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
363
|
+
/**
|
|
364
|
+
* 尝试从其他类型恢复为buffer类型:
|
|
365
|
+
*
|
|
366
|
+
* - `hex` 从十六进制字符串恢复
|
|
367
|
+
* - `base64` 从base64字符串恢复
|
|
368
|
+
*/
|
|
369
|
+
parseFrom(...encodings: BufferValidator.Encodings[]): this;
|
|
370
|
+
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<Buffer>;
|
|
371
|
+
protected copy: () => this;
|
|
372
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
declare namespace DateTimeValidator {
|
|
376
|
+
interface Options<T = Date> extends Validator.Options<T> {
|
|
377
|
+
min?: () => Date;
|
|
378
|
+
minInclusive?: boolean;
|
|
379
|
+
max?: () => Date;
|
|
380
|
+
maxInclusive?: boolean;
|
|
381
|
+
parseFromTimestamp?: boolean;
|
|
382
|
+
formats?: string[];
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
declare class DateTimeValidator<T = Date> extends Validator<T> {
|
|
386
|
+
protected config: DateTimeValidator.Options<T>;
|
|
387
|
+
constructor(formats?: string[]);
|
|
388
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
389
|
+
optional: () => DateTimeValidator<T | Validator.TOptional>;
|
|
390
|
+
nullable: () => DateTimeValidator<T | null>;
|
|
391
|
+
default: (date: Validator.ParameterOrFn<Date>) => DateTimeValidator<T | Validator.TDefault>;
|
|
392
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
393
|
+
min(freshDate: () => Date, inclusive?: boolean): DateTimeValidator<T>;
|
|
394
|
+
max(freshDate: () => Date, inclusive?: boolean): DateTimeValidator<T>;
|
|
395
|
+
/**
|
|
396
|
+
* 尝试把时间戳数字解析成时间对象。默认已开启
|
|
397
|
+
*
|
|
398
|
+
* 支持如下格式:
|
|
399
|
+
* - 13位:1711257956199
|
|
400
|
+
* - 14位:1711257956.199
|
|
401
|
+
* - 10位:1711257956
|
|
402
|
+
*/
|
|
403
|
+
parseFromTimestamp(is?: boolean): this;
|
|
404
|
+
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<Date>;
|
|
405
|
+
protected toDate(value: any): false | Date;
|
|
406
|
+
protected compare(date: Date): boolean;
|
|
407
|
+
protected copy: () => this;
|
|
408
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
declare namespace EmailValidator {
|
|
412
|
+
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
declare class EmailValidator<T = string> extends BaseStringValidator<T> {
|
|
416
|
+
protected config: EmailValidator.Options<T>;
|
|
417
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
418
|
+
optional: () => EmailValidator<T | Validator.TOptional>;
|
|
419
|
+
nullable: () => EmailValidator<T | null>;
|
|
420
|
+
default: (email: Validator.ParameterOrFn<T>) => EmailValidator<T | Validator.TDefault>;
|
|
421
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
422
|
+
match: (pattern: RegExp) => this;
|
|
423
|
+
protected validateString(email: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
424
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
declare namespace EnumValidator {
|
|
428
|
+
interface Options<T> extends Validator.Options<T> {
|
|
429
|
+
ranges: T[];
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
declare class EnumValidator<const T = never> extends Validator<T> {
|
|
433
|
+
protected config: EnumValidator.Options<T>;
|
|
434
|
+
constructor(ranges?: T[]);
|
|
435
|
+
/**
|
|
436
|
+
* 开启/关闭严格模式。开启后有如下限制:
|
|
437
|
+
* - 枚举中包含数字时,如果传递的值为字符串类型,则不再转换为数字后再对比
|
|
438
|
+
*/
|
|
439
|
+
strict: (is?: boolean) => this;
|
|
440
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
441
|
+
optional: () => EnumValidator<T | Validator.TOptional>;
|
|
442
|
+
nullable: () => EnumValidator<T | null>;
|
|
443
|
+
default: (value: Validator.ParameterOrFn<T>) => EnumValidator<T | Validator.TDefault>;
|
|
444
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
445
|
+
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<any>;
|
|
446
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
declare namespace HashValidator {
|
|
450
|
+
type Algorithm = keyof (typeof HashValidator)['algorithmLength'];
|
|
451
|
+
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
452
|
+
algorithm: HashValidator.Algorithm;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
declare class HashValidator<T = string> extends BaseStringValidator<T> {
|
|
456
|
+
/**
|
|
457
|
+
* @see https://github.com/validatorjs/validator.js
|
|
458
|
+
*/
|
|
459
|
+
protected static algorithmLength: {
|
|
460
|
+
readonly md5: 32;
|
|
461
|
+
readonly md4: 32;
|
|
462
|
+
readonly sha1: 40;
|
|
463
|
+
readonly sha256: 64;
|
|
464
|
+
readonly sha384: 96;
|
|
465
|
+
readonly sha512: 128;
|
|
466
|
+
readonly ripemd128: 32;
|
|
467
|
+
readonly ripemd160: 40;
|
|
468
|
+
readonly tiger128: 32;
|
|
469
|
+
readonly tiger160: 40;
|
|
470
|
+
readonly tiger192: 48;
|
|
471
|
+
readonly crc32: 8;
|
|
472
|
+
readonly crc32b: 8;
|
|
473
|
+
};
|
|
474
|
+
protected static algorithmPattern: Record<string, RegExp>;
|
|
475
|
+
protected config: HashValidator.Options<T>;
|
|
476
|
+
constructor(algorithm: HashValidator.Algorithm);
|
|
477
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
478
|
+
optional: () => HashValidator<T | Validator.TOptional>;
|
|
479
|
+
nullable: () => HashValidator<T | null>;
|
|
480
|
+
default: (algorithm: Validator.ParameterOrFn<T>) => HashValidator<T | Validator.TDefault>;
|
|
481
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
482
|
+
protected validateString(hash: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
483
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
declare class IntValidator<T = number> extends BaseNumberValidator<T> {
|
|
487
|
+
protected config: BaseNumberValidator.Options<T>;
|
|
488
|
+
optional: () => IntValidator<T | Validator.TOptional>;
|
|
489
|
+
nullable: () => IntValidator<T | null>;
|
|
490
|
+
default: (integer: Validator.ParameterOrFn<T>) => IntValidator<T | Validator.TDefault>;
|
|
491
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
492
|
+
protected validateNumber(num: number, _key: string, label: string): ValidateResult.Any<number>;
|
|
493
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
declare namespace IpValidator {
|
|
497
|
+
type Version = 'v4' | 'v6';
|
|
498
|
+
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
499
|
+
ipVersion: IpValidator.Version[];
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
declare class IpValidator<T = string> extends BaseStringValidator<T> {
|
|
503
|
+
protected config: IpValidator.Options<T>;
|
|
504
|
+
constructor(version: IpValidator.Version[]);
|
|
505
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
506
|
+
optional: () => IpValidator<T | Validator.TOptional>;
|
|
507
|
+
nullable: () => IpValidator<T | null>;
|
|
508
|
+
default: (ip: Validator.ParameterOrFn<T>) => IpValidator<T | Validator.TDefault>;
|
|
509
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
510
|
+
match: (pattern: RegExp) => this;
|
|
511
|
+
protected validateString(ip: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
512
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
declare namespace NumberValidator {
|
|
516
|
+
interface Options<T = false> extends BaseNumberValidator.Options<T> {
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
declare class NumberValidator<T = number> extends BaseNumberValidator<T> {
|
|
520
|
+
protected config: NumberValidator.Options<T>;
|
|
521
|
+
optional: () => NumberValidator<T | Validator.TOptional>;
|
|
522
|
+
nullable: () => NumberValidator<T | null>;
|
|
523
|
+
default: (number: Validator.ParameterOrFn<T>) => NumberValidator<T | Validator.TDefault>;
|
|
524
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
525
|
+
precision: (decimals: number) => this;
|
|
526
|
+
protected validateNumber(num: number): ValidateResult.Any<number>;
|
|
527
|
+
protected copy: () => this;
|
|
528
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
declare namespace ObjectValidator {
|
|
532
|
+
type Property = Record<string, Validator>;
|
|
533
|
+
interface Options<T> extends Validator.Options<T> {
|
|
534
|
+
properties?: Property;
|
|
535
|
+
stringToObject?: boolean;
|
|
536
|
+
keepAdditional?: boolean | {
|
|
537
|
+
key?: RegExp[];
|
|
538
|
+
value?: Validator;
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
declare class ObjectValidator<T = Validator.TObject> extends Validator<T> {
|
|
543
|
+
protected config: ObjectValidator.Options<T>;
|
|
544
|
+
constructor(properties?: ObjectValidator.Property);
|
|
545
|
+
/**
|
|
546
|
+
* 开启严格模式后:
|
|
547
|
+
* - 不从字符串解析出对象,除非通过`parseFromString()`手动指定
|
|
548
|
+
*/
|
|
549
|
+
strict: (is?: boolean) => this;
|
|
550
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
551
|
+
optional: () => ObjectValidator<T | Validator.TOptional>;
|
|
552
|
+
nullable: () => ObjectValidator<T | null>;
|
|
553
|
+
default: (object: Validator.ParameterOrFn<T>) => ObjectValidator<T | Validator.TDefault>;
|
|
554
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
555
|
+
/**
|
|
556
|
+
* 尝试使用`JSON.parse`把字符串转换为对象,在非严格模式下默认开启
|
|
557
|
+
*/
|
|
558
|
+
parseFromString(is?: boolean): ObjectValidator<T>;
|
|
559
|
+
/**
|
|
560
|
+
* 保留未被验证的键值对。对象中包含动态字段时,无法使用具体的结构体描述出对象的组成。
|
|
561
|
+
*
|
|
562
|
+
* 有以下几种保留规则:
|
|
563
|
+
* 1. 不提供参数,则保留所有的属性和值;
|
|
564
|
+
* 2. 提供`key`,则保留 键 能匹配正则表达式的键值对;
|
|
565
|
+
* 3. 提供`value`,则保留 值 能匹配验证器的键值对,同时值会根据验证器逻辑做相应的修正;
|
|
566
|
+
* 4. 提供`key`和`value`,则保留能同时满足规则2和3的键值对。
|
|
567
|
+
*
|
|
568
|
+
* ```typescript
|
|
569
|
+
* const v = rule.object();
|
|
570
|
+
*
|
|
571
|
+
* // { a: 'x', b: 1, c: true } -> { a: 'x', b: 1, c: true }
|
|
572
|
+
* v.additional();
|
|
573
|
+
*
|
|
574
|
+
* // { a: 'x', b: 1, c: true } -> { a: 'x' }
|
|
575
|
+
* v.additional({ value: rule.string() });
|
|
576
|
+
*
|
|
577
|
+
* // { a: 'x', b: 1, c: true } -> { a: 'x', c: true }
|
|
578
|
+
* v.additional({ key: /^(a|c)$/ });
|
|
579
|
+
*
|
|
580
|
+
* // { a: 'x', b: 1, c: true } -> { c: true }
|
|
581
|
+
* v.additional({ key: /^(a|c)$/, value: rule.boolean() });
|
|
582
|
+
*
|
|
583
|
+
* // { a: 'x', b: 1, c: true } -> { }
|
|
584
|
+
* v.additional({ key: /^(a|c)$/, value: rule.number() });
|
|
585
|
+
* ```
|
|
586
|
+
*/
|
|
587
|
+
additional(): ObjectValidator<T extends Validator.TObject ? {
|
|
588
|
+
[K: string]: unknown;
|
|
589
|
+
} : T extends null ? null : T & {
|
|
590
|
+
[K: string]: unknown;
|
|
591
|
+
}>;
|
|
592
|
+
additional(opts: {
|
|
593
|
+
key: RegExp | RegExp[];
|
|
594
|
+
value?: undefined;
|
|
595
|
+
}): ObjectValidator<(T extends Validator.TObject ? unknown : T) & {
|
|
596
|
+
[K: string]: unknown;
|
|
597
|
+
}>;
|
|
598
|
+
additional<Additional extends Validator>(opts: {
|
|
599
|
+
key?: RegExp | RegExp[];
|
|
600
|
+
value: Additional;
|
|
601
|
+
}): ObjectValidator<T extends Validator.TObject ? {
|
|
602
|
+
[K: string]: Validator.Infer<Additional>;
|
|
603
|
+
} : T extends null ? null : T & {
|
|
604
|
+
[K: string]: Validator.Infer<Additional>;
|
|
605
|
+
}>;
|
|
606
|
+
protected isPlainObject(value: any): value is object;
|
|
607
|
+
protected validateValue(origin: Record<string, any>, key: string, label: string): Promise<ValidateResult.Any<object>>;
|
|
608
|
+
protected copy: () => ObjectValidator<T>;
|
|
609
|
+
protected copyConfig(prev: ObjectValidator): this;
|
|
610
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
declare namespace OneOfValidator {
|
|
614
|
+
interface Options<T> extends Validator.Options<T> {
|
|
615
|
+
validators: Validator[];
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
declare class OneOfValidator<T = never> extends Validator<T> {
|
|
619
|
+
protected config: OneOfValidator.Options<T>;
|
|
620
|
+
constructor(validators: [Validator, ...Validator[]]);
|
|
621
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
622
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
623
|
+
protected isEmpty(_: any): boolean;
|
|
624
|
+
protected validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>>;
|
|
625
|
+
protected copyConfig(prev: OneOfValidator): this;
|
|
626
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
declare namespace StreamValidator {
|
|
630
|
+
interface Options<T = stream$1.Stream> extends Validator.Options<T> {
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
declare class StreamValidator<T = stream$1.Stream> extends Validator<T> {
|
|
634
|
+
protected config: StreamValidator.Options<T>;
|
|
635
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
636
|
+
optional: () => StreamValidator<T | Validator.TOptional>;
|
|
637
|
+
nullable: () => StreamValidator<T | null>;
|
|
638
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
639
|
+
protected validateValue(value: stream$1.Stream, _key: string, label: string): ValidateResult.Any<stream$1.Stream>;
|
|
640
|
+
protected copy: () => this;
|
|
641
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
declare namespace StringValidator {
|
|
645
|
+
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
646
|
+
allowEmpty?: boolean;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
declare class StringValidator<T = string> extends BaseStringValidator<T> {
|
|
650
|
+
protected config: StringValidator.Options<T>;
|
|
651
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
652
|
+
optional: () => StringValidator<T | Validator.TOptional>;
|
|
653
|
+
nullable: () => StringValidator<T | null>;
|
|
654
|
+
default: (string: Validator.ParameterOrFn<T>) => StringValidator<T | Validator.TDefault>;
|
|
655
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
656
|
+
match: (pattern: RegExp) => this;
|
|
657
|
+
trim: (is?: boolean) => this;
|
|
658
|
+
length: (exactOrRange: number | BaseStringValidator.LengthRange) => this;
|
|
659
|
+
/**
|
|
660
|
+
* 把空字符串`''`设置为合法的值
|
|
661
|
+
*/
|
|
662
|
+
allowEmptyString(): StringValidator<T>;
|
|
663
|
+
protected isEmpty(value: any): boolean;
|
|
664
|
+
protected validateString(value: string): ValidateResult.Any<string>;
|
|
665
|
+
protected copy: () => StringValidator<T>;
|
|
666
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
declare namespace UlidValidator {
|
|
670
|
+
interface Options<T = string> extends BaseStringValidator.Options<T> {
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
declare class UlidValidator<T = string> extends BaseStringValidator<T> {
|
|
674
|
+
protected config: UlidValidator.Options<T>;
|
|
675
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
676
|
+
optional: () => UlidValidator<T | Validator.TOptional>;
|
|
677
|
+
nullable: () => UlidValidator<T | null>;
|
|
678
|
+
default: (uuid: Validator.ParameterOrFn<T>) => UlidValidator<T | Validator.TDefault>;
|
|
679
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
680
|
+
protected validateString(ulid: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
681
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
declare namespace UrlValidator {
|
|
685
|
+
interface Options<T> extends BaseStringValidator.Options<T> {
|
|
686
|
+
allowedScheme: string[];
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
declare class UrlValidator<T = string> extends BaseStringValidator<T> {
|
|
690
|
+
protected config: UrlValidator.Options<T>;
|
|
691
|
+
constructor();
|
|
692
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
693
|
+
optional: () => UrlValidator<T | Validator.TOptional>;
|
|
694
|
+
nullable: () => UrlValidator<T | null>;
|
|
695
|
+
default: (Url: Validator.ParameterOrFn<T>) => UrlValidator<T | Validator.TDefault>;
|
|
696
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
697
|
+
/**
|
|
698
|
+
* 允许使用的协议,不区分大小写。默认配置:`['http', 'https']`
|
|
699
|
+
*/
|
|
700
|
+
scheme(scheme: string[]): UrlValidator<T>;
|
|
701
|
+
protected validateString(url: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
702
|
+
protected copy: () => UrlValidator<T>;
|
|
703
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
declare namespace UuidValidator {
|
|
707
|
+
type Version = (typeof UuidValidator.versions)[number];
|
|
708
|
+
interface Options<T = string> extends BaseStringValidator.Options<T> {
|
|
709
|
+
uuidVersion: Version[];
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
declare class UuidValidator<T = string> extends BaseStringValidator<T> {
|
|
713
|
+
static versions: readonly ["v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"];
|
|
714
|
+
static patterns: {
|
|
715
|
+
[key in UuidValidator.Version | 'all']: RegExp;
|
|
716
|
+
};
|
|
717
|
+
protected config: UuidValidator.Options<T>;
|
|
718
|
+
constructor(versions: UuidValidator.Version[]);
|
|
719
|
+
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this;
|
|
720
|
+
optional: () => UuidValidator<T | Validator.TOptional>;
|
|
721
|
+
nullable: () => UuidValidator<T | null>;
|
|
722
|
+
default: (uuid: Validator.ParameterOrFn<T>) => UuidValidator<T | Validator.TDefault>;
|
|
723
|
+
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>;
|
|
724
|
+
match: (pattern: RegExp) => this;
|
|
725
|
+
protected validateString(uuid: string, _key: string, label: string): ValidateResult.Any<string>;
|
|
726
|
+
protected toDocument(): OpenAPI.SchemaObject;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
declare class Rule {
|
|
730
|
+
static register<T extends Rule, K extends keyof T>(this: new (...args: any[]) => T, name: K, SubValidator: new (...args: any[]) => Validator): void;
|
|
731
|
+
/**
|
|
732
|
+
* 规则以管道形式执行,需要匹配所有规则
|
|
733
|
+
* ```typescript
|
|
734
|
+
* rule.allOf([rule.number(), rule.string()]);
|
|
735
|
+
* ```
|
|
736
|
+
*/
|
|
737
|
+
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<{
|
|
738
|
+
[K in keyof T]: Validator.Infer<T[K]>;
|
|
739
|
+
}[number]>>;
|
|
740
|
+
/**
|
|
741
|
+
* 允许任何类型:
|
|
742
|
+
* - string
|
|
743
|
+
* - number
|
|
744
|
+
* - boolean
|
|
745
|
+
* - array
|
|
746
|
+
* - object
|
|
747
|
+
* - buffer
|
|
748
|
+
*/
|
|
749
|
+
any(): AnyValidator<AnyValidator.Type>;
|
|
750
|
+
/**
|
|
751
|
+
* 规则以管道形式执行,需要至少匹配一个规则
|
|
752
|
+
* ```typescript
|
|
753
|
+
* rule.anyOf([rule.number(), rule.string()]);
|
|
754
|
+
* ```
|
|
755
|
+
*/
|
|
756
|
+
anyOf<T extends Validator[], A extends Validator, B extends Validator>(rules: [rule1: A, rule2: B, ...others: T]): AnyOfValidator<Validator.Infer<A> | Validator.Infer<B> | {
|
|
757
|
+
[K in keyof T]: Validator.Infer<T[K]>;
|
|
758
|
+
}[number]>;
|
|
759
|
+
array(): ArrayValidator<unknown[]>;
|
|
760
|
+
array<T extends Validator>(item: T): ArrayValidator<Validator.Infer<T>[]>;
|
|
761
|
+
/**
|
|
762
|
+
* 对象验证规则的快捷操作,等价于 `rule.array(rule.object({}))`
|
|
763
|
+
*/
|
|
764
|
+
array<T extends {
|
|
765
|
+
[key: string]: P;
|
|
766
|
+
}, P extends Validator>(ruleObject: T): ArrayValidator<Validator.Infer<T>[]>;
|
|
767
|
+
/**
|
|
768
|
+
* 大整数类型
|
|
769
|
+
*
|
|
770
|
+
* 注意:非严格模式下,`number` 和 `string` 会尝试被转换成 `bigint` 类型
|
|
771
|
+
*/
|
|
772
|
+
bigint(): BigIntValidator<bigint>;
|
|
773
|
+
/**
|
|
774
|
+
* - 默认真值:`[1, '1', true, 'true']`
|
|
775
|
+
* - 默认假值:`[0, '0', false, 'false']`
|
|
776
|
+
*/
|
|
777
|
+
boolean(): BooleanValidator<boolean>;
|
|
778
|
+
buffer<T extends Buffer>(): BufferValidator<T>;
|
|
779
|
+
/**
|
|
780
|
+
* @param formats 设置解析字符串的格式,支持同时指定不同的格式并从左到右依次尝试。如果是标准的ISO时间格式或者时间戳则无需指定。
|
|
781
|
+
*
|
|
782
|
+
* @link https://moment.github.io/luxon/#/formatting?id=table-of-tokens
|
|
783
|
+
*/
|
|
784
|
+
dateTime(...formats: string[]): DateTimeValidator<Date>;
|
|
785
|
+
email(): EmailValidator<string>;
|
|
786
|
+
enum<const T extends string | number | boolean>(ranges: T[]): EnumValidator<T>;
|
|
787
|
+
enum<const T extends readonly (string | number | boolean)[]>(ranges: T): EnumValidator<T[number]>;
|
|
788
|
+
hash(algorithm: HashValidator.Algorithm): HashValidator<string>;
|
|
789
|
+
/**
|
|
790
|
+
* 注意:非严格模式下,`string` 会尝试被转换成 `number` 类型
|
|
791
|
+
*/
|
|
792
|
+
int(): IntValidator<number>;
|
|
793
|
+
/**
|
|
794
|
+
* ```typescript
|
|
795
|
+
* rule.ip('v4');
|
|
796
|
+
* rule.ip('v6');
|
|
797
|
+
* rule.ip(['v4', 'v6']);
|
|
798
|
+
* ```
|
|
799
|
+
*/
|
|
800
|
+
ip(version: IpValidator.Version | [IpValidator.Version, ...IpValidator.Version[]]): IpValidator<string>;
|
|
801
|
+
/**
|
|
802
|
+
* 注意:非严格模式下,`string` 会尝试被转换成 `number` 类型
|
|
803
|
+
*/
|
|
804
|
+
number(): NumberValidator<number>;
|
|
805
|
+
/**
|
|
806
|
+
* ```typescript
|
|
807
|
+
* rule.object();
|
|
808
|
+
* rule.object({
|
|
809
|
+
* id: rule.int(),
|
|
810
|
+
* profile: rule.object({
|
|
811
|
+
* sex: rule.enum(['unknown', 'male', 'female']),
|
|
812
|
+
* }),
|
|
813
|
+
* });
|
|
814
|
+
* ```
|
|
815
|
+
*/
|
|
816
|
+
object<T extends {
|
|
817
|
+
[key: string]: Validator;
|
|
818
|
+
} | undefined>(properties?: T): ObjectValidator<keyof T extends undefined ? Validator.TObject : Validator.Infer<T>>;
|
|
819
|
+
/**
|
|
820
|
+
* 必须且只能匹配其中一个规则,如果匹配上多个则失败
|
|
821
|
+
* ```typescript
|
|
822
|
+
* rule.oneOf([rule.number(), rule.string()]);
|
|
823
|
+
* ```
|
|
824
|
+
*/
|
|
825
|
+
oneOf<T extends Validator[], A extends Validator, B extends Validator>(rules: [rule1: A, rule2: B, ...others: T]): OneOfValidator<Validator.Infer<A> | Validator.Infer<B> | {
|
|
826
|
+
[K in keyof T]: Validator.Infer<T[K]>;
|
|
827
|
+
}[number]>;
|
|
828
|
+
/**
|
|
829
|
+
* 数据流
|
|
830
|
+
*/
|
|
831
|
+
stream(): StreamValidator<stream.Stream>;
|
|
832
|
+
string(): StringValidator<string>;
|
|
833
|
+
ulid(): UlidValidator<string>;
|
|
834
|
+
url(): UrlValidator<string>;
|
|
835
|
+
/**
|
|
836
|
+
* ```typescript
|
|
837
|
+
* rule.uuid('v4');
|
|
838
|
+
* rule.uuid(['v4', 'v5', 'v6']);
|
|
839
|
+
* ```
|
|
840
|
+
*/
|
|
841
|
+
uuid(): UuidValidator<string>;
|
|
842
|
+
uuid(version: UuidValidator.Version): UuidValidator<string>;
|
|
843
|
+
uuid(versions: [UuidValidator.Version, ...UuidValidator.Version[]]): UuidValidator<string>;
|
|
844
|
+
}
|
|
845
|
+
declare const rule: Rule;
|
|
846
|
+
|
|
847
|
+
/**
|
|
848
|
+
* 验证传入数据,并返回可信的数据
|
|
849
|
+
*
|
|
850
|
+
* @param untrusted 数据源
|
|
851
|
+
* @param schema 验证器
|
|
852
|
+
* @param options.errorFormatter 报错文字格式化处理
|
|
853
|
+
*/
|
|
854
|
+
declare const validate: <T extends ValidatorToken>(untrusted: any, validators: T, options?: {
|
|
855
|
+
errorFormatter?: (errors: ValidateResult.Denied["errors"]) => string | Promise<string>;
|
|
856
|
+
}) => Promise<Validator.Infer<T>>;
|
|
857
|
+
|
|
858
|
+
export { AllOfValidator, AnyOfValidator, AnyValidator, ArrayValidator, BaseNumberValidator, BaseStringValidator, BigIntValidator, BooleanValidator, BufferValidator, DateTimeValidator, EmailValidator, EnumValidator, HashValidator, IntValidator, IpValidator, NumberValidator, ObjectValidator, OneOfValidator, Rule, StreamValidator, StringValidator, type TransformedValidator, UlidValidator, UrlValidator, UuidValidator, ValidateDeniedError, ValidateResult, Validator, type ValidatorToken, rule, toValidator, validate };
|