koatty_validation 1.3.6 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.rollup.config.js +62 -59
- package/CHANGELOG.md +97 -75
- package/LICENSE +29 -29
- package/README.md +363 -116
- package/coverage.lcov +1607 -0
- package/dist/LICENSE +29 -29
- package/dist/README.md +363 -116
- package/dist/index.d.ts +463 -230
- package/dist/index.js +962 -2158
- package/dist/index.mjs +931 -2144
- package/dist/package.json +91 -94
- package/examples/README.md +90 -0
- package/examples/basic-usage.ts +239 -0
- package/examples/custom-decorators-example.ts +230 -0
- package/examples/usage-example.ts +284 -0
- package/package.json +91 -94
package/dist/index.d.ts
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @Author: richen
|
|
3
|
-
* @Date:
|
|
3
|
+
* @Date: 2025-10-23 01:25:11
|
|
4
4
|
* @License: BSD (3-Clause)
|
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
|
6
6
|
* @HomePage: https://koatty.org/
|
|
7
7
|
*/
|
|
8
8
|
import { CountryCode } from 'libphonenumber-js';
|
|
9
|
-
import { IsIpVersion } from 'class-validator';
|
|
10
9
|
import { ValidationOptions } from 'class-validator';
|
|
11
10
|
|
|
12
11
|
/**
|
|
13
|
-
*
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
* 缓存装饰器 - 用于缓存验证函数结果
|
|
13
|
+
*/
|
|
14
|
+
export declare function cached(validator: string, ttl?: number): (target: any, propertyName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 缓存配置选项
|
|
17
18
|
*/
|
|
18
|
-
export declare
|
|
19
|
+
export declare interface CacheOptions {
|
|
20
|
+
max?: number;
|
|
21
|
+
ttl?: number;
|
|
22
|
+
allowStale?: boolean;
|
|
23
|
+
updateAgeOnGet?: boolean;
|
|
24
|
+
}
|
|
19
25
|
|
|
20
26
|
/**
|
|
21
27
|
* Check the base types.
|
|
@@ -32,14 +38,19 @@ export declare function checkParamsType(value: any, type: string): any;
|
|
|
32
38
|
export declare const ClassValidator: ValidateClass;
|
|
33
39
|
|
|
34
40
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* @export
|
|
38
|
-
* @param {string} seed
|
|
39
|
-
* @param {ValidationOptions} [validationOptions]
|
|
40
|
-
* @returns {PropertyDecorator}
|
|
41
|
+
* 清空所有缓存
|
|
41
42
|
*/
|
|
42
|
-
export declare function
|
|
43
|
+
export declare function clearAllCaches(): void;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 配置缓存设置
|
|
47
|
+
*/
|
|
48
|
+
export declare function configureCaches(options: {
|
|
49
|
+
validation?: CacheOptions;
|
|
50
|
+
regex?: CacheOptions;
|
|
51
|
+
}): void;
|
|
52
|
+
|
|
53
|
+
export declare const Contains: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
43
54
|
|
|
44
55
|
/**
|
|
45
56
|
* convertDtoParamsType
|
|
@@ -59,208 +70,262 @@ export declare function convertDtoParamsType(clazz: any, cls: any): any;
|
|
|
59
70
|
*/
|
|
60
71
|
export declare function convertParamsType(param: any, type: string): any;
|
|
61
72
|
|
|
62
|
-
export declare const ENABLE_VALIDATED = "ENABLE_VALIDATED";
|
|
63
|
-
|
|
64
73
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
* @
|
|
68
|
-
* @param
|
|
69
|
-
* @
|
|
70
|
-
* @returns {PropertyDecorator}
|
|
74
|
+
* 创建带参数的验证装饰器
|
|
75
|
+
* @param name 装饰器名称
|
|
76
|
+
* @param validator 验证函数
|
|
77
|
+
* @param defaultMessage 默认错误信息
|
|
78
|
+
* @returns 装饰器工厂函数
|
|
71
79
|
*/
|
|
72
|
-
export declare function
|
|
80
|
+
export declare function createParameterizedDecorator(name: string, validator: ValidatorFunction, defaultMessage?: string): (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
73
81
|
|
|
74
82
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
* @
|
|
78
|
-
* @
|
|
83
|
+
* 创建简单验证装饰器(不需要额外参数)
|
|
84
|
+
* @param name 装饰器名称
|
|
85
|
+
* @param validator 验证函数
|
|
86
|
+
* @param defaultMessage 默认错误信息
|
|
87
|
+
* @returns 装饰器函数
|
|
79
88
|
*/
|
|
80
|
-
export declare function
|
|
89
|
+
export declare function createSimpleDecorator(name: string, validator: ValidatorFunction, defaultMessage?: string): (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
81
90
|
|
|
82
91
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* @
|
|
86
|
-
* @param {ValidRules} rule
|
|
87
|
-
* @param {unknown} value
|
|
88
|
-
* @param {(string | ValidOtpions)} [options]
|
|
89
|
-
* @returns {*}
|
|
92
|
+
* 创建验证装饰器的工厂函数
|
|
93
|
+
* @param options 装饰器配置选项
|
|
94
|
+
* @returns 装饰器工厂函数
|
|
90
95
|
*/
|
|
91
|
-
export declare
|
|
92
|
-
[key in ValidRules]: (value: unknown, options?: string | ValidOtpions) => void;
|
|
93
|
-
};
|
|
96
|
+
export declare function createValidationDecorator(options: DecoratorOptions): (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
94
97
|
|
|
95
98
|
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
* @export
|
|
99
|
-
* @param {number} min
|
|
100
|
-
* @param {ValidationOptions} [validationOptions]
|
|
101
|
-
* @returns {PropertyDecorator}
|
|
99
|
+
* 创建验证错误
|
|
102
100
|
*/
|
|
103
|
-
export declare function
|
|
101
|
+
export declare function createValidationError(field: string, value: any, constraint: string, customMessage?: string, context?: Record<string, any>): ValidationErrorDetail;
|
|
104
102
|
|
|
105
103
|
/**
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* @export
|
|
109
|
-
* @param {number} min
|
|
110
|
-
* @param {ValidationOptions} [validationOptions]
|
|
111
|
-
* @returns {PropertyDecorator}
|
|
104
|
+
* 批量创建验证错误
|
|
112
105
|
*/
|
|
113
|
-
export declare function
|
|
114
|
-
|
|
115
|
-
|
|
106
|
+
export declare function createValidationErrors(errors: Array<{
|
|
107
|
+
field: string;
|
|
108
|
+
value: any;
|
|
109
|
+
constraint: string;
|
|
110
|
+
message?: string;
|
|
111
|
+
context?: Record<string, any>;
|
|
112
|
+
}>): KoattyValidationError;
|
|
116
113
|
|
|
117
114
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* @export
|
|
121
|
-
* @param {string} property
|
|
122
|
-
* @param {ValidationOptions} [validationOptions]
|
|
123
|
-
* @returns {PropertyDecorator}
|
|
115
|
+
* 装饰器选项
|
|
124
116
|
*/
|
|
125
|
-
export declare
|
|
117
|
+
export declare interface DecoratorOptions {
|
|
118
|
+
name: string;
|
|
119
|
+
validator: ValidatorFunction;
|
|
120
|
+
defaultMessage?: string;
|
|
121
|
+
requiresValue?: boolean;
|
|
122
|
+
}
|
|
126
123
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
*
|
|
133
|
-
*/
|
|
134
|
-
export declare
|
|
124
|
+
export declare const ENABLE_VALIDATED = "ENABLE_VALIDATED";
|
|
125
|
+
|
|
126
|
+
export declare const Equals: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 错误信息国际化
|
|
130
|
+
*/
|
|
131
|
+
export declare const ERROR_MESSAGES: {
|
|
132
|
+
readonly zh: {
|
|
133
|
+
readonly IsCnName: "必须是有效的中文姓名";
|
|
134
|
+
readonly IsIdNumber: "必须是有效的身份证号码";
|
|
135
|
+
readonly IsZipCode: "必须是有效的邮政编码";
|
|
136
|
+
readonly IsMobile: "必须是有效的手机号码";
|
|
137
|
+
readonly IsPlateNumber: "必须是有效的车牌号码";
|
|
138
|
+
readonly IsNotEmpty: "不能为空";
|
|
139
|
+
readonly IsDate: "必须是有效的日期";
|
|
140
|
+
readonly IsEmail: "必须是有效的邮箱地址";
|
|
141
|
+
readonly IsIP: "必须是有效的IP地址";
|
|
142
|
+
readonly IsPhoneNumber: "必须是有效的电话号码";
|
|
143
|
+
readonly IsUrl: "必须是有效的URL地址";
|
|
144
|
+
readonly IsHash: "必须是有效的哈希值";
|
|
145
|
+
readonly Equals: "必须等于 {comparison}";
|
|
146
|
+
readonly NotEquals: "不能等于 {comparison}";
|
|
147
|
+
readonly Contains: "必须包含 {seed}";
|
|
148
|
+
readonly IsIn: "必须是以下值之一: {possibleValues}";
|
|
149
|
+
readonly IsNotIn: "不能是以下值之一: {possibleValues}";
|
|
150
|
+
readonly Gt: "必须大于 {min}";
|
|
151
|
+
readonly Gte: "必须大于或等于 {min}";
|
|
152
|
+
readonly Lt: "必须小于 {max}";
|
|
153
|
+
readonly Lte: "必须小于或等于 {max}";
|
|
154
|
+
readonly invalidParameter: "参数 {field} 无效";
|
|
155
|
+
readonly validationFailed: "验证失败";
|
|
156
|
+
};
|
|
157
|
+
readonly en: {
|
|
158
|
+
readonly IsCnName: "must be a valid Chinese name";
|
|
159
|
+
readonly IsIdNumber: "must be a valid ID number";
|
|
160
|
+
readonly IsZipCode: "must be a valid zip code";
|
|
161
|
+
readonly IsMobile: "must be a valid mobile number";
|
|
162
|
+
readonly IsPlateNumber: "must be a valid plate number";
|
|
163
|
+
readonly IsNotEmpty: "should not be empty";
|
|
164
|
+
readonly IsDate: "must be a valid date";
|
|
165
|
+
readonly IsEmail: "must be a valid email";
|
|
166
|
+
readonly IsIP: "must be a valid IP address";
|
|
167
|
+
readonly IsPhoneNumber: "must be a valid phone number";
|
|
168
|
+
readonly IsUrl: "must be a valid URL";
|
|
169
|
+
readonly IsHash: "must be a valid hash";
|
|
170
|
+
readonly Equals: "must equal to {comparison}";
|
|
171
|
+
readonly NotEquals: "should not equal to {comparison}";
|
|
172
|
+
readonly Contains: "must contain {seed}";
|
|
173
|
+
readonly IsIn: "must be one of the following values: {possibleValues}";
|
|
174
|
+
readonly IsNotIn: "should not be one of the following values: {possibleValues}";
|
|
175
|
+
readonly Gt: "must be greater than {min}";
|
|
176
|
+
readonly Gte: "must be greater than or equal to {min}";
|
|
177
|
+
readonly Lt: "must be less than {max}";
|
|
178
|
+
readonly Lte: "must be less than or equal to {max}";
|
|
179
|
+
readonly invalidParameter: "invalid parameter {field}";
|
|
180
|
+
readonly validationFailed: "validation failed";
|
|
181
|
+
};
|
|
182
|
+
};
|
|
135
183
|
|
|
136
184
|
/**
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* @export
|
|
140
|
-
* @returns {PropertyDecorator}
|
|
185
|
+
* 全局错误信息格式化器实例
|
|
141
186
|
*/
|
|
142
|
-
export declare
|
|
187
|
+
export declare const errorFormatter: ErrorMessageFormatter;
|
|
143
188
|
|
|
144
189
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* @export
|
|
148
|
-
* @param {IsEmailOptions} [options]
|
|
149
|
-
* @param {ValidationOptions} [validationOptions]
|
|
150
|
-
* @returns {PropertyDecorator}
|
|
190
|
+
* 错误信息格式化器
|
|
151
191
|
*/
|
|
152
|
-
export declare
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
192
|
+
export declare class ErrorMessageFormatter {
|
|
193
|
+
constructor(language?: SupportedLanguage);
|
|
194
|
+
/**
|
|
195
|
+
* 设置语言
|
|
196
|
+
*/
|
|
197
|
+
setLanguage(language: SupportedLanguage): void;
|
|
198
|
+
/**
|
|
199
|
+
* 格式化错误消息
|
|
200
|
+
*/
|
|
201
|
+
formatMessage(constraint: string, field: string, value?: any, context?: Record<string, any>): string;
|
|
202
|
+
/**
|
|
203
|
+
* 格式化值用于消息显示
|
|
204
|
+
* @private
|
|
205
|
+
*/
|
|
159
206
|
}
|
|
160
207
|
|
|
161
208
|
/**
|
|
162
|
-
*
|
|
163
|
-
* 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
|
|
164
|
-
*
|
|
165
|
-
* @export
|
|
166
|
-
* @param {HashAlgorithm} algorithm
|
|
167
|
-
* @param {ValidationOptions} [validationOptions]
|
|
168
|
-
* @returns {PropertyDecorator}
|
|
209
|
+
* 标记属性为可导出
|
|
169
210
|
*/
|
|
170
|
-
export declare function
|
|
211
|
+
export declare function Expose(): PropertyDecorator;
|
|
171
212
|
|
|
172
213
|
/**
|
|
173
|
-
*
|
|
214
|
+
* Use functions or built-in rules for validation.
|
|
215
|
+
* Throws error if validation fails.
|
|
174
216
|
*
|
|
175
217
|
* @export
|
|
176
|
-
* @param {string} property
|
|
177
|
-
* @param {ValidationOptions} [validationOptions]
|
|
178
|
-
* @returns {PropertyDecorator}
|
|
179
218
|
*/
|
|
180
|
-
export declare
|
|
219
|
+
export declare const FunctionValidator: {
|
|
220
|
+
readonly IsNotEmpty: (value: unknown, options?: string | ValidOtpions) => void;
|
|
221
|
+
readonly IsDate: (value: unknown, options?: string | ValidOtpions) => void;
|
|
222
|
+
readonly IsEmail: (value: unknown, options?: string | ValidOtpions) => void;
|
|
223
|
+
readonly IsIP: (value: unknown, options?: string | ValidOtpions) => void;
|
|
224
|
+
readonly IsPhoneNumber: (value: unknown, options?: string | ValidOtpions) => void;
|
|
225
|
+
readonly IsUrl: (value: unknown, options?: string | ValidOtpions) => void;
|
|
226
|
+
readonly IsHash: (value: unknown, options?: string | ValidOtpions) => void;
|
|
227
|
+
readonly IsCnName: (value: unknown, options?: string | ValidOtpions) => void;
|
|
228
|
+
readonly IsIdNumber: (value: unknown, options?: string | ValidOtpions) => void;
|
|
229
|
+
readonly IsZipCode: (value: unknown, options?: string | ValidOtpions) => void;
|
|
230
|
+
readonly IsMobile: (value: unknown, options?: string | ValidOtpions) => void;
|
|
231
|
+
readonly IsPlateNumber: (value: unknown, options?: string | ValidOtpions) => void;
|
|
232
|
+
readonly Equals: (value: unknown, options?: string | ValidOtpions) => void;
|
|
233
|
+
readonly NotEquals: (value: unknown, options?: string | ValidOtpions) => void;
|
|
234
|
+
readonly Contains: (value: unknown, options?: string | ValidOtpions) => void;
|
|
235
|
+
readonly IsIn: (value: unknown, options?: string | ValidOtpions) => void;
|
|
236
|
+
readonly IsNotIn: (value: unknown, options?: string | ValidOtpions) => void;
|
|
237
|
+
readonly Gt: (value: unknown, options?: string | ValidOtpions) => void;
|
|
238
|
+
readonly Lt: (value: unknown, options?: string | ValidOtpions) => void;
|
|
239
|
+
readonly Gte: (value: unknown, options?: string | ValidOtpions) => void;
|
|
240
|
+
readonly Lte: (value: unknown, options?: string | ValidOtpions) => void;
|
|
241
|
+
};
|
|
181
242
|
|
|
182
243
|
/**
|
|
183
|
-
*
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
244
|
+
* 获取所有缓存统计信息
|
|
245
|
+
*/
|
|
246
|
+
export declare function getAllCacheStats(): {
|
|
247
|
+
validation: {
|
|
248
|
+
size: number;
|
|
249
|
+
max: number;
|
|
250
|
+
calculatedSize: number;
|
|
251
|
+
keyCount: number;
|
|
252
|
+
hits: number;
|
|
253
|
+
misses: number;
|
|
254
|
+
hitRate: number;
|
|
255
|
+
totalRequests: number;
|
|
256
|
+
};
|
|
257
|
+
regex: {
|
|
258
|
+
size: number;
|
|
259
|
+
max: number;
|
|
260
|
+
calculatedSize: number;
|
|
261
|
+
};
|
|
262
|
+
performance: Record<string, any>;
|
|
263
|
+
hotspots: {
|
|
264
|
+
name: string;
|
|
265
|
+
avgTime: number;
|
|
266
|
+
count: number;
|
|
267
|
+
}[];
|
|
268
|
+
};
|
|
191
269
|
|
|
192
|
-
|
|
193
|
-
* Checks if the string is an IP (version 4 or 6). If given value is not a string, then it returns false.
|
|
194
|
-
*
|
|
195
|
-
* @export
|
|
196
|
-
* @param {number} [version]
|
|
197
|
-
* @param {ValidationOptions} [validationOptions]
|
|
198
|
-
* @returns {PropertyDecorator}
|
|
199
|
-
*/
|
|
200
|
-
export declare function IsIP(version?: IsIpVersion, validationOptions?: ValidationOptions): PropertyDecorator;
|
|
270
|
+
export declare const Gt: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
201
271
|
|
|
202
|
-
|
|
203
|
-
* Checks if value is a mobile phone number(chinese).
|
|
204
|
-
*
|
|
205
|
-
* @export
|
|
206
|
-
* @param {string} property
|
|
207
|
-
* @param {ValidationOptions} [validationOptions]
|
|
208
|
-
* @returns {PropertyDecorator}
|
|
209
|
-
*/
|
|
210
|
-
export declare function IsMobile(validationOptions?: ValidationOptions): PropertyDecorator;
|
|
272
|
+
export declare const Gte: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
211
273
|
|
|
212
274
|
/**
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
* @export
|
|
216
|
-
* @param {ValidationOptions} [validationOptions]
|
|
217
|
-
* @returns {PropertyDecorator}
|
|
275
|
+
* 哈希算法类型
|
|
218
276
|
*/
|
|
219
|
-
export declare
|
|
277
|
+
export declare type HashAlgorithm = "md4" | "md5" | "sha1" | "sha256" | "sha384" | "sha512" | "ripemd128" | "ripemd160" | "tiger128" | "tiger160" | "tiger192" | "crc32" | "crc32b";
|
|
278
|
+
|
|
279
|
+
export declare const IsCnName: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
280
|
+
|
|
281
|
+
export declare const IsDate: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
220
282
|
|
|
221
283
|
/**
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* @export
|
|
225
|
-
* @param {any[]} possibleValues
|
|
226
|
-
* @param {ValidationOptions} [validationOptions]
|
|
227
|
-
* @returns {PropertyDecorator}
|
|
284
|
+
* Expose的别名
|
|
228
285
|
*/
|
|
229
|
-
export declare function
|
|
286
|
+
export declare function IsDefined(): PropertyDecorator;
|
|
287
|
+
|
|
288
|
+
export declare function IsEmail(options?: IsEmailOptions, validationOptions?: ValidationOptions): (object: Object, propertyName: string) => void;
|
|
230
289
|
|
|
231
290
|
/**
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
* @
|
|
235
|
-
* @
|
|
236
|
-
* If users must enter the intl. prefix (e.g. +41), then you may pass "ZZ" or null as region.
|
|
237
|
-
* See [google-libphonenumber, metadata.js:countryCodeToRegionCodeMap on github]
|
|
238
|
-
* {@link https://github.com/ruimarinho/google-libphonenumber/blob/1e46138878cff479aafe2ce62175c6c49cb58720/src/metadata.js#L33}
|
|
239
|
-
* @param {ValidationOptions} [validationOptions]
|
|
240
|
-
* @returns {PropertyDecorator}
|
|
291
|
+
* koatty_validation 类型定义
|
|
292
|
+
* @author richen
|
|
293
|
+
* @copyright Copyright (c) - <richenlin(at)gmail.com>
|
|
294
|
+
* @license MIT
|
|
241
295
|
*/
|
|
242
|
-
export declare function IsPhoneNumber(region?: CountryCode, validationOptions?: ValidationOptions): PropertyDecorator;
|
|
243
|
-
|
|
244
296
|
/**
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
* @export
|
|
248
|
-
* @param {string} property
|
|
249
|
-
* @param {ValidationOptions} [validationOptions]
|
|
250
|
-
* @returns {PropertyDecorator}
|
|
297
|
+
* 邮箱验证选项
|
|
251
298
|
*/
|
|
252
|
-
export declare
|
|
299
|
+
export declare interface IsEmailOptions {
|
|
300
|
+
allow_display_name?: boolean;
|
|
301
|
+
require_display_name?: boolean;
|
|
302
|
+
allow_utf8_local_part?: boolean;
|
|
303
|
+
require_tld?: boolean;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export declare function IsHash(algorithm: HashAlgorithm, validationOptions?: ValidationOptions): (object: Object, propertyName: string) => void;
|
|
307
|
+
|
|
308
|
+
export declare const IsIdNumber: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
309
|
+
|
|
310
|
+
export declare const IsIn: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
311
|
+
|
|
312
|
+
export declare function IsIP(version?: any, validationOptions?: ValidationOptions): (object: Object, propertyName: string) => void;
|
|
313
|
+
|
|
314
|
+
export declare const IsMobile: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
315
|
+
|
|
316
|
+
export declare const IsNotEmpty: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
317
|
+
|
|
318
|
+
export declare const IsNotIn: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
319
|
+
|
|
320
|
+
export declare function IsPhoneNumber(region?: CountryCode, validationOptions?: ValidationOptions): (object: Object, propertyName: string) => void;
|
|
321
|
+
|
|
322
|
+
export declare const IsPlateNumber: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
323
|
+
|
|
324
|
+
export declare function IsUrl(options?: IsURLOptions, validationOptions?: ValidationOptions): (object: Object, propertyName: string) => void;
|
|
253
325
|
|
|
254
326
|
/**
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
* @export
|
|
258
|
-
* @param {IsURLOptions} [options]
|
|
259
|
-
* @param {ValidationOptions} [validationOptions]
|
|
260
|
-
* @returns {PropertyDecorator}
|
|
327
|
+
* URL验证选项
|
|
261
328
|
*/
|
|
262
|
-
export declare function IsUrl(options?: IsURLOptions, validationOptions?: ValidationOptions): PropertyDecorator;
|
|
263
|
-
|
|
264
329
|
export declare interface IsURLOptions {
|
|
265
330
|
protocols?: string[];
|
|
266
331
|
require_tld?: boolean;
|
|
@@ -275,62 +340,78 @@ export declare interface IsURLOptions {
|
|
|
275
340
|
disallow_auth?: boolean;
|
|
276
341
|
}
|
|
277
342
|
|
|
278
|
-
|
|
279
|
-
* Checks if value is a zipCode(chinese).
|
|
280
|
-
*
|
|
281
|
-
* @export
|
|
282
|
-
* @param {string} property
|
|
283
|
-
* @param {ValidationOptions} [validationOptions]
|
|
284
|
-
* @returns {PropertyDecorator}
|
|
285
|
-
*/
|
|
286
|
-
export declare function IsZipCode(validationOptions?: ValidationOptions): PropertyDecorator;
|
|
343
|
+
export declare const IsZipCode: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
287
344
|
|
|
288
345
|
/**
|
|
289
|
-
*
|
|
290
|
-
* If given value is not a string, then it returns false.
|
|
291
|
-
*
|
|
292
|
-
* @export
|
|
293
|
-
* @param {number} min
|
|
294
|
-
* @param {number} [max]
|
|
295
|
-
* @param {ValidationOptions} [validationOptions]
|
|
296
|
-
* @returns {PropertyDecorator}
|
|
346
|
+
* 增强的验证错误类
|
|
297
347
|
*/
|
|
298
|
-
export declare
|
|
348
|
+
export declare class KoattyValidationError extends Error {
|
|
349
|
+
readonly errors: ValidationErrorDetail[];
|
|
350
|
+
readonly statusCode: number;
|
|
351
|
+
readonly timestamp: Date;
|
|
352
|
+
constructor(errors: ValidationErrorDetail[], message?: string);
|
|
353
|
+
/**
|
|
354
|
+
* 获取第一个错误信息
|
|
355
|
+
*/
|
|
356
|
+
getFirstError(): ValidationErrorDetail | undefined;
|
|
357
|
+
/**
|
|
358
|
+
* 获取指定字段的错误
|
|
359
|
+
*/
|
|
360
|
+
getFieldErrors(field: string): ValidationErrorDetail[];
|
|
361
|
+
/**
|
|
362
|
+
* 转换为JSON格式
|
|
363
|
+
*/
|
|
364
|
+
toJSON(): {
|
|
365
|
+
name: string;
|
|
366
|
+
message: string;
|
|
367
|
+
statusCode: number;
|
|
368
|
+
timestamp: Date;
|
|
369
|
+
errors: ValidationErrorDetail[];
|
|
370
|
+
};
|
|
371
|
+
}
|
|
299
372
|
|
|
300
|
-
|
|
301
|
-
* Checks if the first number is less than or equal to the max value.
|
|
302
|
-
*
|
|
303
|
-
* @export
|
|
304
|
-
* @param {number} max
|
|
305
|
-
* @param {ValidationOptions} [validationOptions]
|
|
306
|
-
* @returns {PropertyDecorator}
|
|
307
|
-
*/
|
|
308
|
-
export declare function Lt(max: number, validationOptions?: ValidationOptions): PropertyDecorator;
|
|
373
|
+
export declare const Lt: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
309
374
|
|
|
310
|
-
|
|
311
|
-
* Checks if the first number is less than or equal to the max value.
|
|
312
|
-
*
|
|
313
|
-
* @export
|
|
314
|
-
* @param {number} max
|
|
315
|
-
* @param {ValidationOptions} [validationOptions]
|
|
316
|
-
* @returns {PropertyDecorator}
|
|
317
|
-
*/
|
|
318
|
-
export declare function Lte(max: number, validationOptions?: ValidationOptions): PropertyDecorator;
|
|
375
|
+
export declare const Lte: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
319
376
|
|
|
320
377
|
/**
|
|
321
|
-
*
|
|
322
|
-
*
|
|
323
|
-
* @export
|
|
324
|
-
* @param {*} comparison
|
|
325
|
-
* @param {ValidationOptions} [validationOptions]
|
|
326
|
-
* @returns {PropertyDecorator}
|
|
378
|
+
* 元数据缓存
|
|
327
379
|
*/
|
|
328
|
-
|
|
380
|
+
declare class MetadataCache {
|
|
381
|
+
static getInstance(): MetadataCache;
|
|
382
|
+
/**
|
|
383
|
+
* 获取类的元数据缓存
|
|
384
|
+
*/
|
|
385
|
+
getClassCache(target: Function): Map<string, any>;
|
|
386
|
+
/**
|
|
387
|
+
* 缓存元数据
|
|
388
|
+
*/
|
|
389
|
+
setMetadata(target: Function, key: string, value: any): void;
|
|
390
|
+
/**
|
|
391
|
+
* 获取缓存的元数据
|
|
392
|
+
*/
|
|
393
|
+
getMetadata(target: Function, key: string): any;
|
|
394
|
+
/**
|
|
395
|
+
* 检查是否已缓存
|
|
396
|
+
*/
|
|
397
|
+
hasMetadata(target: Function, key: string): boolean;
|
|
398
|
+
/**
|
|
399
|
+
* 清空指定类的缓存
|
|
400
|
+
*/
|
|
401
|
+
clearClassCache(target: Function): void;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export declare const metadataCache: MetadataCache;
|
|
405
|
+
|
|
406
|
+
export declare const NotEquals: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
329
407
|
|
|
330
408
|
export declare const PARAM_CHECK_KEY = "PARAM_CHECK_KEY";
|
|
331
409
|
|
|
332
410
|
export declare const PARAM_RULE_KEY = "PARAM_RULE_KEY";
|
|
333
411
|
|
|
412
|
+
/**
|
|
413
|
+
* 参数类型键常量
|
|
414
|
+
*/
|
|
334
415
|
export declare const PARAM_TYPE_KEY = "PARAM_TYPE_KEY";
|
|
335
416
|
|
|
336
417
|
/**
|
|
@@ -362,6 +443,42 @@ export declare enum paramterTypes {
|
|
|
362
443
|
"undefined" = 19
|
|
363
444
|
}
|
|
364
445
|
|
|
446
|
+
/**
|
|
447
|
+
* 性能监控
|
|
448
|
+
*/
|
|
449
|
+
declare class PerformanceMonitor {
|
|
450
|
+
static getInstance(): PerformanceMonitor;
|
|
451
|
+
/**
|
|
452
|
+
* 开始计时
|
|
453
|
+
*/
|
|
454
|
+
startTimer(name: string): () => void;
|
|
455
|
+
/**
|
|
456
|
+
* 记录性能指标
|
|
457
|
+
*/
|
|
458
|
+
/**
|
|
459
|
+
* 获取性能报告
|
|
460
|
+
*/
|
|
461
|
+
getReport(): Record<string, any>;
|
|
462
|
+
/**
|
|
463
|
+
* 获取热点分析(执行时间最长的操作)
|
|
464
|
+
*/
|
|
465
|
+
getHotspots(limit?: number): Array<{
|
|
466
|
+
name: string;
|
|
467
|
+
avgTime: number;
|
|
468
|
+
count: number;
|
|
469
|
+
}>;
|
|
470
|
+
/**
|
|
471
|
+
* 清空指标
|
|
472
|
+
*/
|
|
473
|
+
clear(): void;
|
|
474
|
+
/**
|
|
475
|
+
* 导出性能数据为CSV格式
|
|
476
|
+
*/
|
|
477
|
+
exportToCSV(): string;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
export declare const performanceMonitor: PerformanceMonitor;
|
|
481
|
+
|
|
365
482
|
/**
|
|
366
483
|
* plain object convert to class instance
|
|
367
484
|
*
|
|
@@ -374,13 +491,54 @@ export declare enum paramterTypes {
|
|
|
374
491
|
export declare function plainToClass(clazz: any, data: any, convert?: boolean): any;
|
|
375
492
|
|
|
376
493
|
/**
|
|
377
|
-
*
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
494
|
+
* 正则表达式缓存
|
|
495
|
+
*/
|
|
496
|
+
declare class RegexCache {
|
|
497
|
+
constructor(options?: CacheOptions);
|
|
498
|
+
static getInstance(options?: CacheOptions): RegexCache;
|
|
499
|
+
/**
|
|
500
|
+
* 获取缓存的正则表达式
|
|
501
|
+
*/
|
|
502
|
+
get(pattern: string, flags?: string): RegExp;
|
|
503
|
+
/**
|
|
504
|
+
* 预编译常用正则表达式
|
|
505
|
+
*/
|
|
506
|
+
precompile(patterns: Array<{
|
|
507
|
+
pattern: string;
|
|
508
|
+
flags?: string;
|
|
509
|
+
}>): void;
|
|
510
|
+
/**
|
|
511
|
+
* 获取缓存统计
|
|
512
|
+
*/
|
|
513
|
+
getStats(): {
|
|
514
|
+
size: number;
|
|
515
|
+
max: number;
|
|
516
|
+
calculatedSize: number;
|
|
517
|
+
};
|
|
518
|
+
/**
|
|
519
|
+
* 清空缓存
|
|
520
|
+
*/
|
|
521
|
+
clear(): void;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
export declare const regexCache: RegexCache;
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* 设置全局语言
|
|
528
|
+
*/
|
|
529
|
+
export declare function setValidationLanguage(language: SupportedLanguage): void;
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* 改进的错误处理机制
|
|
533
|
+
* @author richen
|
|
534
|
+
*/
|
|
535
|
+
/**
|
|
536
|
+
* 支持的语言
|
|
537
|
+
*/
|
|
538
|
+
export declare type SupportedLanguage = 'zh' | 'en';
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* 参数验证装饰器
|
|
384
542
|
*/
|
|
385
543
|
export declare function Valid(rule: ValidRules | ValidRules[] | Function, options?: string | ValidOtpions): ParameterDecorator;
|
|
386
544
|
|
|
@@ -406,13 +564,80 @@ declare class ValidateClass {
|
|
|
406
564
|
}
|
|
407
565
|
|
|
408
566
|
/**
|
|
409
|
-
*
|
|
410
|
-
*
|
|
411
|
-
* @export
|
|
412
|
-
* @returns {MethodDecorator}
|
|
567
|
+
* 方法验证装饰器
|
|
568
|
+
* 自动验证方法参数中的 DTO 对象
|
|
413
569
|
*/
|
|
414
570
|
export declare function Validated(): MethodDecorator;
|
|
415
571
|
|
|
572
|
+
/**
|
|
573
|
+
* 验证结果缓存
|
|
574
|
+
*/
|
|
575
|
+
declare class ValidationCache {
|
|
576
|
+
constructor(options?: CacheOptions);
|
|
577
|
+
static getInstance(options?: CacheOptions): ValidationCache;
|
|
578
|
+
/**
|
|
579
|
+
* 生成缓存键
|
|
580
|
+
*/
|
|
581
|
+
/**
|
|
582
|
+
* 序列化值用于缓存键
|
|
583
|
+
*/
|
|
584
|
+
/**
|
|
585
|
+
* 获取缓存的验证结果
|
|
586
|
+
*/
|
|
587
|
+
get(validator: string, value: any, ...args: any[]): boolean | undefined;
|
|
588
|
+
/**
|
|
589
|
+
* 缓存验证结果
|
|
590
|
+
*/
|
|
591
|
+
set(validator: string, value: any, result: boolean, ...args: any[]): void;
|
|
592
|
+
/**
|
|
593
|
+
* 检查是否存在缓存
|
|
594
|
+
*/
|
|
595
|
+
has(validator: string, value: any, ...args: any[]): boolean;
|
|
596
|
+
/**
|
|
597
|
+
* 删除特定缓存
|
|
598
|
+
*/
|
|
599
|
+
delete(validator: string, value: any, ...args: any[]): boolean;
|
|
600
|
+
/**
|
|
601
|
+
* 清空缓存
|
|
602
|
+
*/
|
|
603
|
+
clear(): void;
|
|
604
|
+
/**
|
|
605
|
+
* 获取缓存统计
|
|
606
|
+
*/
|
|
607
|
+
getStats(): {
|
|
608
|
+
size: number;
|
|
609
|
+
max: number;
|
|
610
|
+
calculatedSize: number;
|
|
611
|
+
keyCount: number;
|
|
612
|
+
hits: number;
|
|
613
|
+
misses: number;
|
|
614
|
+
hitRate: number;
|
|
615
|
+
totalRequests: number;
|
|
616
|
+
};
|
|
617
|
+
/**
|
|
618
|
+
* 设置缓存TTL
|
|
619
|
+
*/
|
|
620
|
+
setTTL(validator: string, value: any, ttl: number, ...args: any[]): void;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
export declare const validationCache: ValidationCache;
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* 验证错误详情
|
|
627
|
+
*/
|
|
628
|
+
export declare interface ValidationErrorDetail {
|
|
629
|
+
field: string;
|
|
630
|
+
value: any;
|
|
631
|
+
constraint: string;
|
|
632
|
+
message: string;
|
|
633
|
+
context?: Record<string, any>;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* 验证函数类型定义
|
|
638
|
+
*/
|
|
639
|
+
export declare type ValidatorFunction = (value: any, ...args: any[]) => boolean;
|
|
640
|
+
|
|
416
641
|
/**
|
|
417
642
|
* Validator Functions
|
|
418
643
|
*/
|
|
@@ -442,11 +667,11 @@ export declare const ValidFuncs: {
|
|
|
442
667
|
* See [google-libphonenumber, metadata.js:countryCodeToRegionCodeMap on github]
|
|
443
668
|
* {@link https://github.com/ruimarinho/google-libphonenumber/blob/1e46138878cff479aafe2ce62175c6c49cb58720/src/metadata.js#L33}
|
|
444
669
|
*/
|
|
445
|
-
IsPhoneNumber: (value:
|
|
670
|
+
IsPhoneNumber: (value: unknown, region?: CountryCode) => boolean;
|
|
446
671
|
/**
|
|
447
672
|
* Checks if the string is an url. If given value is not a string, then it returns false.
|
|
448
673
|
*/
|
|
449
|
-
IsUrl: (value:
|
|
674
|
+
IsUrl: (value: unknown, options?: IsURLOptions) => boolean;
|
|
450
675
|
/**
|
|
451
676
|
* check if the string is a hash of type algorithm. Algorithm is one of
|
|
452
677
|
* ['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
|
|
@@ -455,23 +680,23 @@ export declare const ValidFuncs: {
|
|
|
455
680
|
/**
|
|
456
681
|
* Checks if value is a chinese name.
|
|
457
682
|
*/
|
|
458
|
-
IsCnName: (value:
|
|
683
|
+
IsCnName: (value: unknown) => boolean;
|
|
459
684
|
/**
|
|
460
685
|
* Checks if value is a idcard number.
|
|
461
686
|
*/
|
|
462
|
-
IsIdNumber: (value:
|
|
687
|
+
IsIdNumber: (value: unknown) => boolean;
|
|
463
688
|
/**
|
|
464
689
|
* Checks if value is a zipCode.
|
|
465
690
|
*/
|
|
466
|
-
IsZipCode: (value:
|
|
691
|
+
IsZipCode: (value: unknown) => boolean;
|
|
467
692
|
/**
|
|
468
693
|
* Checks if value is a mobile phone number.
|
|
469
694
|
*/
|
|
470
|
-
IsMobile: (value:
|
|
695
|
+
IsMobile: (value: unknown) => boolean;
|
|
471
696
|
/**
|
|
472
697
|
* Checks if value is a plateNumber.
|
|
473
698
|
*/
|
|
474
|
-
IsPlateNumber: (value:
|
|
699
|
+
IsPlateNumber: (value: unknown) => boolean;
|
|
475
700
|
/**
|
|
476
701
|
* Checks if value matches ("===") the comparison.
|
|
477
702
|
*/
|
|
@@ -495,21 +720,24 @@ export declare const ValidFuncs: {
|
|
|
495
720
|
/**
|
|
496
721
|
* Checks if the first number is greater than or equal to the second.
|
|
497
722
|
*/
|
|
498
|
-
Gt: (
|
|
723
|
+
Gt: (value: unknown, min: number) => boolean;
|
|
499
724
|
/**
|
|
500
725
|
* Checks if the first number is less than or equal to the second.
|
|
501
726
|
*/
|
|
502
|
-
Lt: (
|
|
727
|
+
Lt: (value: unknown, max: number) => boolean;
|
|
503
728
|
/**
|
|
504
729
|
* Checks if the first number is greater than or equal to the second.
|
|
505
730
|
*/
|
|
506
|
-
Gte: (
|
|
731
|
+
Gte: (value: unknown, min: number) => boolean;
|
|
507
732
|
/**
|
|
508
733
|
* Checks if the first number is less than or equal to the second.
|
|
509
734
|
*/
|
|
510
|
-
Lte: (
|
|
735
|
+
Lte: (value: unknown, max: number) => boolean;
|
|
511
736
|
};
|
|
512
737
|
|
|
738
|
+
/**
|
|
739
|
+
* 验证选项
|
|
740
|
+
*/
|
|
513
741
|
export declare type ValidOtpions = {
|
|
514
742
|
message: string;
|
|
515
743
|
value: any;
|
|
@@ -523,4 +751,9 @@ export declare type ValidOtpions = {
|
|
|
523
751
|
*/
|
|
524
752
|
export declare type ValidRules = "IsNotEmpty" | "IsDate" | "IsEmail" | "IsIP" | "IsPhoneNumber" | "IsUrl" | "IsHash" | "IsCnName" | "IsIdNumber" | "IsZipCode" | "IsMobile" | "IsPlateNumber" | "Equals" | "NotEquals" | "Contains" | "IsIn" | "IsNotIn" | "Gt" | "Lt" | "Gte" | "Lte";
|
|
525
753
|
|
|
754
|
+
/**
|
|
755
|
+
* 预热缓存 - 预编译常用正则表达式
|
|
756
|
+
*/
|
|
757
|
+
export declare function warmupCaches(): void;
|
|
758
|
+
|
|
526
759
|
export { }
|