koatty_validation 1.0.10 → 1.2.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 CHANGED
@@ -1,9 +1,422 @@
1
- /**
2
- * @ author: richen
3
- * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
4
- * @ license: MIT
5
- * @ version: 2020-03-20 11:31:09
6
- */
7
- export * from "./rule";
8
- export * from "./decorator";
9
- export { checkParamsType, convertParamsType, convertDtoParamsType, plainToClass } from "./util";
1
+ /*!
2
+ * @Author: richen
3
+ * @Date: 2022-02-25 09:58:15
4
+ * @License: BSD (3-Clause)
5
+ * @Copyright (c) - <richenlin(at)gmail.com>
6
+ * @HomePage: https://koatty.org/
7
+ */
8
+ import { CountryCode } from 'libphonenumber-js';
9
+ import { IsIpVersion } from 'class-validator';
10
+ import { ValidationOptions } from 'class-validator';
11
+
12
+ /**
13
+ * Check the base types.
14
+ *
15
+ * @param {*} value
16
+ * @param {string} type
17
+ * @returns {*}
18
+ */
19
+ export declare function checkParamsType(value: any, type: string): any;
20
+
21
+ /**
22
+ * ClassValidator for manual
23
+ */
24
+ export declare const ClassValidator: ValidateClass;
25
+
26
+ /**
27
+ * Checks if the string contains the seed.
28
+ *
29
+ * @export
30
+ * @param {string} seed
31
+ * @param {ValidationOptions} [validationOptions]
32
+ * @returns {PropertyDecorator}
33
+ */
34
+ export declare function Contains(seed: string, validationOptions?: ValidationOptions): PropertyDecorator;
35
+
36
+ /**
37
+ * convertDtoParamsType
38
+ *
39
+ * @param {*} clazz
40
+ * @param {*} cls
41
+ * @param {*} data
42
+ * @returns {*}
43
+ */
44
+ export declare function convertDtoParamsType(clazz: any, cls: any, data: any): any;
45
+
46
+ /**
47
+ * 绑定参数类型转换
48
+ *
49
+ * @param {*} param
50
+ * @param {string} type
51
+ * @returns {*}
52
+ */
53
+ export declare function convertParamsType(param: any, type: string): any;
54
+
55
+ export declare const ENABLE_VALIDATED = "ENABLE_VALIDATED";
56
+
57
+ /**
58
+ * Checks if value matches ("===") the comparison.
59
+ *
60
+ * @export
61
+ * @param {*} comparison
62
+ * @param {ValidationOptions} [validationOptions]
63
+ * @returns {PropertyDecorator}
64
+ */
65
+ export declare function Equals(comparison: any, validationOptions?: ValidationOptions): PropertyDecorator;
66
+
67
+ /**
68
+ * Marks property as included in the process of transformation.
69
+ *
70
+ * @export
71
+ * @returns {PropertyDecorator}
72
+ */
73
+ export declare function Expose(): PropertyDecorator;
74
+
75
+ /**
76
+ * Use functions or built-in rules for validation.
77
+ *
78
+ * @export
79
+ * @param {ValidRules} rule
80
+ * @param {unknown} value
81
+ * @param {(string | ValidOtpions)} [options]
82
+ * @returns {*}
83
+ */
84
+ export declare const FunctionValidator: {
85
+ [key in ValidRules]: (value: unknown, options?: string | ValidOtpions) => void;
86
+ };
87
+
88
+ /**
89
+ * Checks if the first number is greater than or equal to the min value.
90
+ *
91
+ * @export
92
+ * @param {number} min
93
+ * @param {ValidationOptions} [validationOptions]
94
+ * @returns {PropertyDecorator}
95
+ */
96
+ export declare function Gt(min: number, validationOptions?: ValidationOptions): PropertyDecorator;
97
+
98
+ /**
99
+ * Checks if the first number is greater than or equal to the min value.
100
+ *
101
+ * @export
102
+ * @param {number} min
103
+ * @param {ValidationOptions} [validationOptions]
104
+ * @returns {PropertyDecorator}
105
+ */
106
+ export declare function Gte(min: number, validationOptions?: ValidationOptions): PropertyDecorator;
107
+
108
+ export declare type HashAlgorithm = "md4" | "md5" | "sha1" | "sha256" | "sha384" | "sha512" | "ripemd128" | "ripemd160" | "tiger128" | "tiger160" | "tiger192" | "crc32" | "crc32b";
109
+
110
+ /**
111
+ * Checks if value is a chinese name.
112
+ *
113
+ * @export
114
+ * @param {string} property
115
+ * @param {ValidationOptions} [validationOptions]
116
+ * @returns {PropertyDecorator}
117
+ */
118
+ export declare function IsCnName(validationOptions?: ValidationOptions): PropertyDecorator;
119
+
120
+ /**
121
+ * Checks if a given value is a real date.
122
+ *
123
+ * @export
124
+ * @param {ValidationOptions} [validationOptions]
125
+ * @returns {PropertyDecorator}
126
+ */
127
+ export declare function IsDate(validationOptions?: ValidationOptions): PropertyDecorator;
128
+
129
+ /**
130
+ * Identifies that the field needs to be defined
131
+ *
132
+ * @export
133
+ * @returns {PropertyDecorator}
134
+ */
135
+ export declare function IsDefined(): PropertyDecorator;
136
+
137
+ /**
138
+ * Checks if the string is an email. If given value is not a string, then it returns false.
139
+ *
140
+ * @export
141
+ * @param {IsEmailOptions} [options]
142
+ * @param {ValidationOptions} [validationOptions]
143
+ * @returns {PropertyDecorator}
144
+ */
145
+ export declare function IsEmail(options?: IsEmailOptions, validationOptions?: ValidationOptions): PropertyDecorator;
146
+
147
+ export declare interface IsEmailOptions {
148
+ allow_display_name?: boolean;
149
+ require_display_name?: boolean;
150
+ allow_utf8_local_part?: boolean;
151
+ require_tld?: boolean;
152
+ }
153
+
154
+ /**
155
+ * check if the string is a hash of type algorithm. Algorithm is one of ['md4', 'md5', 'sha1', 'sha256',
156
+ * 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
157
+ *
158
+ * @export
159
+ * @param {HashAlgorithm} algorithm
160
+ * @param {ValidationOptions} [validationOptions]
161
+ * @returns {PropertyDecorator}
162
+ */
163
+ export declare function IsHash(algorithm: HashAlgorithm, validationOptions?: ValidationOptions): PropertyDecorator;
164
+
165
+ /**
166
+ * Checks if value is a idCard number(chinese).
167
+ *
168
+ * @export
169
+ * @param {string} property
170
+ * @param {ValidationOptions} [validationOptions]
171
+ * @returns {PropertyDecorator}
172
+ */
173
+ export declare function IsIdNumber(validationOptions?: ValidationOptions): PropertyDecorator;
174
+
175
+ /**
176
+ * Checks if given value is in a array of allowed values.
177
+ *
178
+ * @export
179
+ * @param {any[]} possibleValues
180
+ * @param {ValidationOptions} [validationOptions]
181
+ * @returns {PropertyDecorator}
182
+ */
183
+ export declare function IsIn(possibleValues: any[], validationOptions?: ValidationOptions): PropertyDecorator;
184
+
185
+ /**
186
+ * Checks if the string is an IP (version 4 or 6). If given value is not a string, then it returns false.
187
+ *
188
+ * @export
189
+ * @param {number} [version]
190
+ * @param {ValidationOptions} [validationOptions]
191
+ * @returns {PropertyDecorator}
192
+ */
193
+ export declare function IsIP(version?: IsIpVersion, validationOptions?: ValidationOptions): PropertyDecorator;
194
+
195
+ /**
196
+ * Checks if value is a mobile phone number(chinese).
197
+ *
198
+ * @export
199
+ * @param {string} property
200
+ * @param {ValidationOptions} [validationOptions]
201
+ * @returns {PropertyDecorator}
202
+ */
203
+ export declare function IsMobile(validationOptions?: ValidationOptions): PropertyDecorator;
204
+
205
+ /**
206
+ * Checks value is not empty, undefined, null, '', NaN, [], {} and any empty string(including spaces, tabs, formfeeds, etc.), returns false.
207
+ *
208
+ * @export
209
+ * @param {ValidationOptions} [validationOptions]
210
+ * @returns {PropertyDecorator}
211
+ */
212
+ export declare function IsNotEmpty(validationOptions?: ValidationOptions): PropertyDecorator;
213
+
214
+ /**
215
+ * Checks if given value not in a array of allowed values.
216
+ *
217
+ * @export
218
+ * @param {any[]} possibleValues
219
+ * @param {ValidationOptions} [validationOptions]
220
+ * @returns {PropertyDecorator}
221
+ */
222
+ export declare function IsNotIn(possibleValues: any[], validationOptions?: ValidationOptions): PropertyDecorator;
223
+
224
+ /**
225
+ * Checks if the string is a valid phone number.
226
+ *
227
+ * @export
228
+ * @param {string} {string} region 2 characters uppercase country code (e.g. DE, US, CH).
229
+ * If users must enter the intl. prefix (e.g. +41), then you may pass "ZZ" or null as region.
230
+ * See [google-libphonenumber, metadata.js:countryCodeToRegionCodeMap on github]
231
+ * {@link https://github.com/ruimarinho/google-libphonenumber/blob/1e46138878cff479aafe2ce62175c6c49cb58720/src/metadata.js#L33}
232
+ * @param {ValidationOptions} [validationOptions]
233
+ * @returns {PropertyDecorator}
234
+ */
235
+ export declare function IsPhoneNumber(region?: CountryCode, validationOptions?: ValidationOptions): PropertyDecorator;
236
+
237
+ /**
238
+ * Checks if value is a plate number(chinese).
239
+ *
240
+ * @export
241
+ * @param {string} property
242
+ * @param {ValidationOptions} [validationOptions]
243
+ * @returns {PropertyDecorator}
244
+ */
245
+ export declare function IsPlateNumber(validationOptions?: ValidationOptions): PropertyDecorator;
246
+
247
+ /**
248
+ * Checks if the string is an url.
249
+ *
250
+ * @export
251
+ * @param {IsURLOptions} [options]
252
+ * @param {ValidationOptions} [validationOptions]
253
+ * @returns {PropertyDecorator}
254
+ */
255
+ export declare function IsUrl(options?: IsURLOptions, validationOptions?: ValidationOptions): PropertyDecorator;
256
+
257
+ export declare interface IsURLOptions {
258
+ protocols?: string[];
259
+ require_tld?: boolean;
260
+ require_protocol?: boolean;
261
+ require_host?: boolean;
262
+ require_valid_protocol?: boolean;
263
+ allow_underscores?: boolean;
264
+ host_whitelist?: (string | RegExp)[];
265
+ host_blacklist?: (string | RegExp)[];
266
+ allow_trailing_dot?: boolean;
267
+ allow_protocol_relative_urls?: boolean;
268
+ disallow_auth?: boolean;
269
+ }
270
+
271
+ /**
272
+ * Checks if value is a zipCode(chinese).
273
+ *
274
+ * @export
275
+ * @param {string} property
276
+ * @param {ValidationOptions} [validationOptions]
277
+ * @returns {PropertyDecorator}
278
+ */
279
+ export declare function IsZipCode(validationOptions?: ValidationOptions): PropertyDecorator;
280
+
281
+ /**
282
+ * Checks if the string's length falls in a range. Note: this function takes into account surrogate pairs.
283
+ * If given value is not a string, then it returns false.
284
+ *
285
+ * @export
286
+ * @param {number} min
287
+ * @param {number} [max]
288
+ * @param {ValidationOptions} [validationOptions]
289
+ * @returns {PropertyDecorator}
290
+ */
291
+ export declare function Length(min: number, max?: number, validationOptions?: ValidationOptions): PropertyDecorator;
292
+
293
+ /**
294
+ * Checks if the first number is less than or equal to the max value.
295
+ *
296
+ * @export
297
+ * @param {number} max
298
+ * @param {ValidationOptions} [validationOptions]
299
+ * @returns {PropertyDecorator}
300
+ */
301
+ export declare function Lt(max: number, validationOptions?: ValidationOptions): PropertyDecorator;
302
+
303
+ /**
304
+ * Checks if the first number is less than or equal to the max value.
305
+ *
306
+ * @export
307
+ * @param {number} max
308
+ * @param {ValidationOptions} [validationOptions]
309
+ * @returns {PropertyDecorator}
310
+ */
311
+ export declare function Lte(max: number, validationOptions?: ValidationOptions): PropertyDecorator;
312
+
313
+ /**
314
+ * Checks if value does not match ("!==") the comparison.
315
+ *
316
+ * @export
317
+ * @param {*} comparison
318
+ * @param {ValidationOptions} [validationOptions]
319
+ * @returns {PropertyDecorator}
320
+ */
321
+ export declare function NotEquals(comparison: any, validationOptions?: ValidationOptions): PropertyDecorator;
322
+
323
+ export declare const PARAM_CHECK_KEY = "PARAM_CHECK_KEY";
324
+
325
+ export declare const PARAM_RULE_KEY = "PARAM_RULE_KEY";
326
+
327
+ export declare const PARAM_TYPE_KEY = "PARAM_TYPE_KEY";
328
+
329
+ /**
330
+ * paramterTypes
331
+ *
332
+ * @export
333
+ * @enum {number}
334
+ */
335
+ export declare enum paramterTypes {
336
+ "Number" = 0,
337
+ "number" = 1,
338
+ "String" = 2,
339
+ "string" = 3,
340
+ "Boolean" = 4,
341
+ "boolean" = 5,
342
+ "Array" = 6,
343
+ "array" = 7,
344
+ "Tuple" = 8,
345
+ "tuple" = 9,
346
+ "Object" = 10,
347
+ "object" = 11,
348
+ "Enum" = 12,
349
+ "enum" = 13,
350
+ "Bigint" = 14,
351
+ "bigint" = 15,
352
+ "Null" = 16,
353
+ "null" = 17,
354
+ "Undefined" = 18,
355
+ "undefined" = 19
356
+ }
357
+
358
+ /**
359
+ *
360
+ *
361
+ * @export
362
+ * @param {*} clazz
363
+ * @param {*} data
364
+ * @param {boolean} [convert=false]
365
+ * @returns
366
+ */
367
+ export declare function plainToClass(clazz: any, data: any, convert?: boolean): any;
368
+
369
+ /**
370
+ * Validation parameter's type and values.
371
+ *
372
+ * @export
373
+ * @param {(ValidRules | ValidRules[] | Function)} rule
374
+ * @param {*} [options] If the options type is a string, the value is the error message of the validation rule.
375
+ * Some validation rules require additional parameters, ext: @Valid("Gte", {message:"Requires value greater than or equal to 100", value: 100})
376
+ * @returns {*} {ParameterDecorator}
377
+ */
378
+ export declare function Valid(rule: ValidRules | ValidRules[] | Function, options?: string | ValidOtpions): ParameterDecorator;
379
+
380
+ declare class ValidateClass {
381
+ /**
382
+ *
383
+ *
384
+ * @static
385
+ * @returns
386
+ * @memberof ValidateUtil
387
+ */
388
+ static getInstance(): ValidateClass;
389
+ /**
390
+ * validated data vs dto class
391
+ *
392
+ * @param {*} Clazz
393
+ * @param {*} data
394
+ * @param {boolean} [convert=false] auto convert parameters type
395
+ * @returns {Promise<any>}
396
+ * @memberof ValidateClass
397
+ */
398
+ valid(Clazz: any, data: any, convert?: boolean): Promise<any>;
399
+ }
400
+
401
+ /**
402
+ * Validation parameter's type and values from DTO class.
403
+ *
404
+ * @export
405
+ * @returns {MethodDecorator}
406
+ */
407
+ export declare function Validated(): MethodDecorator;
408
+
409
+ export declare type ValidOtpions = {
410
+ message: string;
411
+ value: any;
412
+ };
413
+
414
+ /**
415
+ * type checked rules
416
+ *
417
+ * @export
418
+ * @type {number}
419
+ */
420
+ 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";
421
+
422
+ export { }