koatty_validation 1.0.12 → 1.1.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 CHANGED
@@ -2,10 +2,6 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
- ### [1.0.12](https://github.com/koatty/koatty_validation/compare/v1.0.10...v1.0.12) (2021-12-21)
5
+ ## [1.1.0](https://github.com/koatty/koatty_validation/compare/v1.0.12...v1.1.0) (2022-02-16)
6
6
 
7
- ### [1.0.10](https://github.com/koatty/koatty_validation/compare/v1.0.8...v1.0.10) (2021-11-25)
8
-
9
- ### [1.0.8](https://github.com/koatty/koatty_validation/compare/v1.0.6...v1.0.8) (2021-11-23)
10
-
11
- ### [1.0.6](https://github.com/koatty/koatty_validation/compare/v1.0.4...v1.0.6) (2021-11-20)
7
+ ## [2.0.0](https://github.com/koatty/koatty_validation/compare/v1.0.12...v2.0.0) (2022-02-16)
package/dist/LICENSE ADDED
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2020, Koatty
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/dist/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # koatty_validation
2
+ Validation Util for Koatty and ThinkORM. Based on class-validator, extended parameter type checking and restricted attribute functions.
3
+
4
+
5
+ # User Decorators
6
+
7
+ * @IsDefined
8
+ * @IsCnName
9
+ * @IsIdNumber
10
+ * @IsZipCode
11
+ * @IsMobile
12
+ * @IsPlateNumber
13
+ * @IsEmail
14
+ * @IsIP
15
+ * @IsPhoneNumber
16
+ * @IsUrl
17
+ * @IsHash
18
+ * @IsNotEmpty
19
+ * @Equals
20
+ * @NotEquals
21
+ * @Contains
22
+ * @IsIn
23
+ * @IsNotIn
24
+ * @IsDate
25
+ * @Min
26
+ * @Max
27
+ * @Length
28
+
29
+
30
+ * @Valid
31
+ * @Validated
32
+
33
+
34
+
35
+ ```js
36
+
37
+ export class Controller {
38
+
39
+ Test(@Valid("IsNotEmpty", "can not be empty!!") id: number){
40
+ //todo
41
+ }
42
+
43
+ @Validated() // use dto validation
44
+ TestDto(user: UserDTO) {
45
+
46
+ }
47
+ }
48
+
49
+ export class UserDTO {
50
+ @IsNotEmpty({ message: "can not be empty!!" })
51
+ phoneNum: string;
52
+
53
+ @IsCnName({ message: "must be cn name"})
54
+ userName: string;
55
+ }
56
+
57
+ ```
58
+
59
+ # Validator for manual
60
+
61
+ ## FunctionValidator
62
+
63
+ * FunctionValidator.IsCnName
64
+ * FunctionValidator.IsIdNumber
65
+ * FunctionValidator.IsZipCode
66
+ * FunctionValidator.IsMobile
67
+ * FunctionValidator.IsPlateNumber
68
+ * FunctionValidator.IsEmail
69
+ * FunctionValidator.IsIP
70
+ * FunctionValidator.IsPhoneNumber
71
+ * FunctionValidator.IsUrl
72
+ * FunctionValidator.IsHash
73
+ * FunctionValidator.IsNotEmpty
74
+ * FunctionValidator.Equals
75
+ * FunctionValidator.NotEquals
76
+ * FunctionValidator.Contains
77
+ * FunctionValidator.IsIn
78
+ * FunctionValidator.IsNotIn
79
+ * FunctionValidator.IsDate
80
+ * FunctionValidator.Min
81
+ * FunctionValidator.Max
82
+ * FunctionValidator.Length
83
+
84
+ ```js
85
+ if (!FunctionValidator.IsNotEmpty(data)) {
86
+ console.log('error');
87
+ }
88
+ ```
89
+
90
+ ## ClassValidator
91
+
92
+ ```js
93
+ class SchemaClass {
94
+ @IsDefined
95
+ id: number;
96
+
97
+ @IsNotEmpty
98
+ name: string;
99
+ }
100
+
101
+
102
+ ClassValidator.valid(SchemaClass, {name: ''}).catch(err => {
103
+ console.log(err);
104
+ })
105
+ ```
106
+
package/dist/index.d.ts CHANGED
@@ -1,9 +1,402 @@
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, getOriginMetadata } from "./util";
1
+ /*!
2
+ * @Author: richen
3
+ * @Date: 2022-02-16 18:32:01
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
+ * Validator Functions
77
+ */
78
+ export declare const FunctionValidator: any;
79
+
80
+ export declare type HashAlgorithm = "md4" | "md5" | "sha1" | "sha256" | "sha384" | "sha512" | "ripemd128" | "ripemd160" | "tiger128" | "tiger160" | "tiger192" | "crc32" | "crc32b";
81
+
82
+ /**
83
+ * Checks if value is a chinese name.
84
+ *
85
+ * @export
86
+ * @param {string} property
87
+ * @param {ValidationOptions} [validationOptions]
88
+ * @returns {PropertyDecorator}
89
+ */
90
+ export declare function IsCnName(validationOptions?: ValidationOptions): PropertyDecorator;
91
+
92
+ /**
93
+ * Checks if a given value is a real date.
94
+ *
95
+ * @export
96
+ * @param {ValidationOptions} [validationOptions]
97
+ * @returns {PropertyDecorator}
98
+ */
99
+ export declare function IsDate(validationOptions?: ValidationOptions): PropertyDecorator;
100
+
101
+ /**
102
+ * Identifies that the field needs to be defined
103
+ *
104
+ * @export
105
+ * @returns {PropertyDecorator}
106
+ */
107
+ export declare function IsDefined(): PropertyDecorator;
108
+
109
+ /**
110
+ * Checks if the string is an email. If given value is not a string, then it returns false.
111
+ *
112
+ * @export
113
+ * @param {IsEmailOptions} [options]
114
+ * @param {ValidationOptions} [validationOptions]
115
+ * @returns {PropertyDecorator}
116
+ */
117
+ export declare function IsEmail(options?: IsEmailOptions, validationOptions?: ValidationOptions): PropertyDecorator;
118
+
119
+ export declare interface IsEmailOptions {
120
+ allow_display_name?: boolean;
121
+ require_display_name?: boolean;
122
+ allow_utf8_local_part?: boolean;
123
+ require_tld?: boolean;
124
+ }
125
+
126
+ /**
127
+ * check if the string is a hash of type algorithm. Algorithm is one of ['md4', 'md5', 'sha1', 'sha256',
128
+ * 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
129
+ *
130
+ * @export
131
+ * @param {HashAlgorithm} algorithm
132
+ * @param {ValidationOptions} [validationOptions]
133
+ * @returns {PropertyDecorator}
134
+ */
135
+ export declare function IsHash(algorithm: HashAlgorithm, validationOptions?: ValidationOptions): PropertyDecorator;
136
+
137
+ /**
138
+ * Checks if value is a idCard number(chinese).
139
+ *
140
+ * @export
141
+ * @param {string} property
142
+ * @param {ValidationOptions} [validationOptions]
143
+ * @returns {PropertyDecorator}
144
+ */
145
+ export declare function IsIdNumber(validationOptions?: ValidationOptions): PropertyDecorator;
146
+
147
+ /**
148
+ * Checks if given value is in a array of allowed values.
149
+ *
150
+ * @export
151
+ * @param {any[]} possibleValues
152
+ * @param {ValidationOptions} [validationOptions]
153
+ * @returns {PropertyDecorator}
154
+ */
155
+ export declare function IsIn(possibleValues: any[], validationOptions?: ValidationOptions): PropertyDecorator;
156
+
157
+ /**
158
+ * Checks if the string is an IP (version 4 or 6). If given value is not a string, then it returns false.
159
+ *
160
+ * @export
161
+ * @param {number} [version]
162
+ * @param {ValidationOptions} [validationOptions]
163
+ * @returns {PropertyDecorator}
164
+ */
165
+ export declare function IsIP(version?: IsIpVersion, validationOptions?: ValidationOptions): PropertyDecorator;
166
+
167
+ /**
168
+ * Checks if value is a mobile phone number(chinese).
169
+ *
170
+ * @export
171
+ * @param {string} property
172
+ * @param {ValidationOptions} [validationOptions]
173
+ * @returns {PropertyDecorator}
174
+ */
175
+ export declare function IsMobile(validationOptions?: ValidationOptions): PropertyDecorator;
176
+
177
+ /**
178
+ * Checks value is not empty, undefined, null, '', NaN, [], {} and any empty string(including spaces, tabs, formfeeds, etc.), returns false.
179
+ *
180
+ * @export
181
+ * @param {ValidationOptions} [validationOptions]
182
+ * @returns {PropertyDecorator}
183
+ */
184
+ export declare function IsNotEmpty(validationOptions?: ValidationOptions): PropertyDecorator;
185
+
186
+ /**
187
+ * Checks if given value not in a array of allowed values.
188
+ *
189
+ * @export
190
+ * @param {any[]} possibleValues
191
+ * @param {ValidationOptions} [validationOptions]
192
+ * @returns {PropertyDecorator}
193
+ */
194
+ export declare function IsNotIn(possibleValues: any[], validationOptions?: ValidationOptions): PropertyDecorator;
195
+
196
+ /**
197
+ * Checks if the string is a valid phone number.
198
+ *
199
+ * @export
200
+ * @param {string} {string} region 2 characters uppercase country code (e.g. DE, US, CH).
201
+ * If users must enter the intl. prefix (e.g. +41), then you may pass "ZZ" or null as region.
202
+ * See [google-libphonenumber, metadata.js:countryCodeToRegionCodeMap on github]
203
+ * {@link https://github.com/ruimarinho/google-libphonenumber/blob/1e46138878cff479aafe2ce62175c6c49cb58720/src/metadata.js#L33}
204
+ * @param {ValidationOptions} [validationOptions]
205
+ * @returns {PropertyDecorator}
206
+ */
207
+ export declare function IsPhoneNumber(region?: CountryCode, validationOptions?: ValidationOptions): PropertyDecorator;
208
+
209
+ /**
210
+ * Checks if value is a plate number(chinese).
211
+ *
212
+ * @export
213
+ * @param {string} property
214
+ * @param {ValidationOptions} [validationOptions]
215
+ * @returns {PropertyDecorator}
216
+ */
217
+ export declare function IsPlateNumber(validationOptions?: ValidationOptions): PropertyDecorator;
218
+
219
+ /**
220
+ * Checks if the string is an url.
221
+ *
222
+ * @export
223
+ * @param {IsURLOptions} [options]
224
+ * @param {ValidationOptions} [validationOptions]
225
+ * @returns {PropertyDecorator}
226
+ */
227
+ export declare function IsUrl(options?: IsURLOptions, validationOptions?: ValidationOptions): PropertyDecorator;
228
+
229
+ export declare interface IsURLOptions {
230
+ protocols?: string[];
231
+ require_tld?: boolean;
232
+ require_protocol?: boolean;
233
+ require_host?: boolean;
234
+ require_valid_protocol?: boolean;
235
+ allow_underscores?: boolean;
236
+ host_whitelist?: (string | RegExp)[];
237
+ host_blacklist?: (string | RegExp)[];
238
+ allow_trailing_dot?: boolean;
239
+ allow_protocol_relative_urls?: boolean;
240
+ disallow_auth?: boolean;
241
+ }
242
+
243
+ /**
244
+ * Checks if value is a zipCode(chinese).
245
+ *
246
+ * @export
247
+ * @param {string} property
248
+ * @param {ValidationOptions} [validationOptions]
249
+ * @returns {PropertyDecorator}
250
+ */
251
+ export declare function IsZipCode(validationOptions?: ValidationOptions): PropertyDecorator;
252
+
253
+ /**
254
+ * Checks if the string's length falls in a range. Note: this function takes into account surrogate pairs.
255
+ * If given value is not a string, then it returns false.
256
+ *
257
+ * @export
258
+ * @param {number} min
259
+ * @param {number} [max]
260
+ * @param {ValidationOptions} [validationOptions]
261
+ * @returns {PropertyDecorator}
262
+ */
263
+ export declare function Length(min: number, max?: number, validationOptions?: ValidationOptions): PropertyDecorator;
264
+
265
+ /**
266
+ * Checks if the first number is less than or equal to the max value.
267
+ *
268
+ * @export
269
+ * @param {number} max
270
+ * @param {ValidationOptions} [validationOptions]
271
+ * @returns {PropertyDecorator}
272
+ */
273
+ export declare function Max(max: number, validationOptions?: ValidationOptions): PropertyDecorator;
274
+
275
+ /**
276
+ * Checks if the first number is greater than or equal to the min value.
277
+ *
278
+ * @export
279
+ * @param {number} min
280
+ * @param {ValidationOptions} [validationOptions]
281
+ * @returns {PropertyDecorator}
282
+ */
283
+ export declare function Min(min: number, validationOptions?: ValidationOptions): PropertyDecorator;
284
+
285
+ /**
286
+ * Checks if value does not match ("!==") the comparison.
287
+ *
288
+ * @export
289
+ * @param {*} comparison
290
+ * @param {ValidationOptions} [validationOptions]
291
+ * @returns {PropertyDecorator}
292
+ */
293
+ export declare function NotEquals(comparison: any, validationOptions?: ValidationOptions): PropertyDecorator;
294
+
295
+ export declare const PARAM_CHECK_KEY = "PARAM_CHECK_KEY";
296
+
297
+ export declare const PARAM_RULE_KEY = "PARAM_RULE_KEY";
298
+
299
+ export declare const PARAM_TYPE_KEY = "PARAM_TYPE_KEY";
300
+
301
+ /**
302
+ * paramterTypes
303
+ *
304
+ * @export
305
+ * @enum {number}
306
+ */
307
+ export declare enum paramterTypes {
308
+ "Number" = 0,
309
+ "number" = 1,
310
+ "String" = 2,
311
+ "string" = 3,
312
+ "Boolean" = 4,
313
+ "boolean" = 5,
314
+ "Array" = 6,
315
+ "array" = 7,
316
+ "Tuple" = 8,
317
+ "tuple" = 9,
318
+ "Object" = 10,
319
+ "object" = 11,
320
+ "Enum" = 12,
321
+ "enum" = 13,
322
+ "Bigint" = 14,
323
+ "bigint" = 15,
324
+ "Null" = 16,
325
+ "null" = 17,
326
+ "Undefined" = 18,
327
+ "undefined" = 19
328
+ }
329
+
330
+ /**
331
+ *
332
+ *
333
+ * @export
334
+ * @param {*} clazz
335
+ * @param {*} data
336
+ * @param {boolean} [convert=false]
337
+ * @returns
338
+ */
339
+ export declare function plainToClass(clazz: any, data: any, convert?: boolean): any;
340
+
341
+ /**
342
+ * Validation parameter's type and values.
343
+ *
344
+ * @export
345
+ * @param {(ValidRules | ValidRules[] | Function)} rule
346
+ * @param {string} [message]
347
+ * @returns {ParameterDecorator}
348
+ */
349
+ export declare function Valid(rule: ValidRules | ValidRules[] | Function, message?: string): ParameterDecorator;
350
+
351
+ declare class ValidateClass {
352
+ /**
353
+ *
354
+ *
355
+ * @static
356
+ * @returns
357
+ * @memberof ValidateUtil
358
+ */
359
+ static getInstance(): ValidateClass;
360
+ /**
361
+ * validated data vs dto class
362
+ *
363
+ * @param {*} Clazz
364
+ * @param {*} data
365
+ * @param {boolean} [convert=false] auto convert parameters type
366
+ * @returns {Promise<any>}
367
+ * @memberof ValidateClass
368
+ */
369
+ valid(Clazz: any, data: any, convert?: boolean): Promise<any>;
370
+ }
371
+
372
+ /**
373
+ * Validation parameter's type and values from DTO class.
374
+ *
375
+ * @export
376
+ * @returns {MethodDecorator}
377
+ */
378
+ export declare function Validated(): MethodDecorator;
379
+
380
+ /**
381
+ * Use functions or built-in rules for validation.
382
+ *
383
+ * @export
384
+ * @param {string} name
385
+ * @param {*} value
386
+ * @param {string} type
387
+ * @param {(ValidRules | ValidRules[] | Function)} rule
388
+ * @param {string} [message]
389
+ * @param {boolean} [checkType=true]
390
+ * @returns
391
+ */
392
+ export declare function ValidatorFuncs(name: string, value: any, type: string, rule: ValidRules | ValidRules[] | Function, message?: string, checkType?: boolean): any;
393
+
394
+ /**
395
+ * type checked rules
396
+ *
397
+ * @export
398
+ * @type {number}
399
+ */
400
+ export declare type ValidRules = "IsNotEmpty" | "IsDate" | "IsEmail" | "IsIP" | "IsPhoneNumber" | "IsUrl" | "IsHash" | "IsCnName" | "IsIdNumber" | "IsZipCode" | "IsMobile" | "IsPlateNumber";
401
+
402
+ export { }