koatty_validation 1.1.0 → 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/.vscode/launch.json +81 -0
- package/CHANGELOG.md +2 -0
- package/README.md +34 -28
- package/dist/README.md +34 -28
- package/dist/index.d.ts +44 -24
- package/dist/index.js +204 -96
- package/dist/index.mjs +202 -95
- package/dist/package.json +1 -1
- package/jest-html-reporters-attach/result.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"configurations": [
|
|
3
|
+
{
|
|
4
|
+
"name": "Launch Koatty Project",
|
|
5
|
+
"type": "node",
|
|
6
|
+
"request": "launch",
|
|
7
|
+
"args": [
|
|
8
|
+
"${workspaceRoot}/src/App.ts"
|
|
9
|
+
],
|
|
10
|
+
"runtimeArgs": [
|
|
11
|
+
"--nolazy",
|
|
12
|
+
"-r",
|
|
13
|
+
"ts-node/register"
|
|
14
|
+
],
|
|
15
|
+
"envFile": "/Users/richen/Workspace/.env",
|
|
16
|
+
"sourceMaps": true,
|
|
17
|
+
"cwd": "${workspaceRoot}",
|
|
18
|
+
"protocol": "inspector",
|
|
19
|
+
"internalConsoleOptions": "neverOpen"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"name": "Launch GoPackage",
|
|
23
|
+
"type": "go",
|
|
24
|
+
"request": "launch",
|
|
25
|
+
"mode": "auto",
|
|
26
|
+
"program": "${workspaceFolder}/main.go",
|
|
27
|
+
"envFile": "/Users/richen/Workspace/.env",
|
|
28
|
+
"args": []
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"name": "Launch Java Project",
|
|
32
|
+
"type": "java",
|
|
33
|
+
"request": "launch",
|
|
34
|
+
"mainClass": "",
|
|
35
|
+
"envFile": "/Users/richen/Workspace/.env"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"name": "Launch Node.js Project",
|
|
39
|
+
"type": "node",
|
|
40
|
+
"request": "launch",
|
|
41
|
+
"mode": "auto",
|
|
42
|
+
"program": "${workspaceFolder}/app.js",
|
|
43
|
+
"envFile": "/Users/richen/Workspace/.env",
|
|
44
|
+
"args": []
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"name": "Launch Typescript Project",
|
|
48
|
+
"type": "node",
|
|
49
|
+
"request": "launch",
|
|
50
|
+
"args": [
|
|
51
|
+
"${workspaceRoot}/src/index.ts"
|
|
52
|
+
],
|
|
53
|
+
"runtimeArgs": [
|
|
54
|
+
"--nolazy",
|
|
55
|
+
"-r",
|
|
56
|
+
"ts-node/register"
|
|
57
|
+
],
|
|
58
|
+
"envFile": "/Users/richen/Workspace/.env",
|
|
59
|
+
"sourceMaps": true,
|
|
60
|
+
"cwd": "${workspaceRoot}",
|
|
61
|
+
"protocol": "inspector",
|
|
62
|
+
"internalConsoleOptions": "neverOpen"
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"type": "node",
|
|
66
|
+
"name": "vscode-jest-tests",
|
|
67
|
+
"request": "launch",
|
|
68
|
+
"console": "integratedTerminal",
|
|
69
|
+
"internalConsoleOptions": "neverOpen",
|
|
70
|
+
"disableOptimisticBPs": true,
|
|
71
|
+
"program": "${workspaceFolder}/node_modules/.bin/jest",
|
|
72
|
+
"cwd": "${workspaceFolder}",
|
|
73
|
+
"args": [
|
|
74
|
+
"--config",
|
|
75
|
+
"jest.config.js",
|
|
76
|
+
"--runInBand",
|
|
77
|
+
"--watchAll=false"
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
]
|
|
81
|
+
}
|
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
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.2.0](https://github.com/koatty/koatty_validation/compare/v1.1.0...v1.2.0) (2022-02-25)
|
|
6
|
+
|
|
5
7
|
## [1.1.0](https://github.com/koatty/koatty_validation/compare/v1.0.12...v1.1.0) (2022-02-16)
|
|
6
8
|
|
|
7
9
|
## [2.0.0](https://github.com/koatty/koatty_validation/compare/v1.0.12...v2.0.0) (2022-02-16)
|
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# koatty_validation
|
|
2
|
-
Validation Util for Koatty
|
|
2
|
+
Validation Util for Koatty. Based on class-validator, extended parameter type checking and restricted attribute functions.
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
# User Decorators
|
|
@@ -22,11 +22,11 @@ Validation Util for Koatty and ThinkORM. Based on class-validator, extended para
|
|
|
22
22
|
* @IsIn
|
|
23
23
|
* @IsNotIn
|
|
24
24
|
* @IsDate
|
|
25
|
-
* @
|
|
26
|
-
* @
|
|
27
|
-
* @
|
|
25
|
+
* @Gt
|
|
26
|
+
* @Gte
|
|
27
|
+
* @Lt
|
|
28
|
+
* @Lte
|
|
28
29
|
|
|
29
|
-
|
|
30
30
|
* @Valid
|
|
31
31
|
* @Validated
|
|
32
32
|
|
|
@@ -60,35 +60,41 @@ export class UserDTO {
|
|
|
60
60
|
|
|
61
61
|
## FunctionValidator
|
|
62
62
|
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
63
|
+
* IsCnName
|
|
64
|
+
* IsIdNumber
|
|
65
|
+
* IsZipCode
|
|
66
|
+
* IsMobile
|
|
67
|
+
* IsPlateNumber
|
|
68
|
+
* IsEmail
|
|
69
|
+
* IsIP
|
|
70
|
+
* IsPhoneNumber
|
|
71
|
+
* IsUrl
|
|
72
|
+
* IsHash
|
|
73
|
+
* IsNotEmpty
|
|
74
|
+
* Equals
|
|
75
|
+
* NotEquals
|
|
76
|
+
* Contains
|
|
77
|
+
* IsIn
|
|
78
|
+
* IsNotIn
|
|
79
|
+
* IsDate
|
|
80
|
+
* Gt
|
|
81
|
+
* Gte
|
|
82
|
+
* Lt
|
|
83
|
+
* Lte
|
|
84
|
+
|
|
85
|
+
exp:
|
|
83
86
|
|
|
84
87
|
```js
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
+
const str = "";
|
|
89
|
+
// throw Error
|
|
90
|
+
FunctionValidator.IsNotEmpty(str, "cannot be empty");
|
|
91
|
+
FunctionValidator.Contains(str, {message: "must contain s", value: "s"});
|
|
88
92
|
```
|
|
89
93
|
|
|
90
94
|
## ClassValidator
|
|
91
95
|
|
|
96
|
+
exp:
|
|
97
|
+
|
|
92
98
|
```js
|
|
93
99
|
class SchemaClass {
|
|
94
100
|
@IsDefined
|
package/dist/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# koatty_validation
|
|
2
|
-
Validation Util for Koatty
|
|
2
|
+
Validation Util for Koatty. Based on class-validator, extended parameter type checking and restricted attribute functions.
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
# User Decorators
|
|
@@ -22,11 +22,11 @@ Validation Util for Koatty and ThinkORM. Based on class-validator, extended para
|
|
|
22
22
|
* @IsIn
|
|
23
23
|
* @IsNotIn
|
|
24
24
|
* @IsDate
|
|
25
|
-
* @
|
|
26
|
-
* @
|
|
27
|
-
* @
|
|
25
|
+
* @Gt
|
|
26
|
+
* @Gte
|
|
27
|
+
* @Lt
|
|
28
|
+
* @Lte
|
|
28
29
|
|
|
29
|
-
|
|
30
30
|
* @Valid
|
|
31
31
|
* @Validated
|
|
32
32
|
|
|
@@ -60,35 +60,41 @@ export class UserDTO {
|
|
|
60
60
|
|
|
61
61
|
## FunctionValidator
|
|
62
62
|
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
63
|
+
* IsCnName
|
|
64
|
+
* IsIdNumber
|
|
65
|
+
* IsZipCode
|
|
66
|
+
* IsMobile
|
|
67
|
+
* IsPlateNumber
|
|
68
|
+
* IsEmail
|
|
69
|
+
* IsIP
|
|
70
|
+
* IsPhoneNumber
|
|
71
|
+
* IsUrl
|
|
72
|
+
* IsHash
|
|
73
|
+
* IsNotEmpty
|
|
74
|
+
* Equals
|
|
75
|
+
* NotEquals
|
|
76
|
+
* Contains
|
|
77
|
+
* IsIn
|
|
78
|
+
* IsNotIn
|
|
79
|
+
* IsDate
|
|
80
|
+
* Gt
|
|
81
|
+
* Gte
|
|
82
|
+
* Lt
|
|
83
|
+
* Lte
|
|
84
|
+
|
|
85
|
+
exp:
|
|
83
86
|
|
|
84
87
|
```js
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
+
const str = "";
|
|
89
|
+
// throw Error
|
|
90
|
+
FunctionValidator.IsNotEmpty(str, "cannot be empty");
|
|
91
|
+
FunctionValidator.Contains(str, {message: "must contain s", value: "s"});
|
|
88
92
|
```
|
|
89
93
|
|
|
90
94
|
## ClassValidator
|
|
91
95
|
|
|
96
|
+
exp:
|
|
97
|
+
|
|
92
98
|
```js
|
|
93
99
|
class SchemaClass {
|
|
94
100
|
@IsDefined
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @Author: richen
|
|
3
|
-
* @Date: 2022-02-
|
|
3
|
+
* @Date: 2022-02-25 09:58:15
|
|
4
4
|
* @License: BSD (3-Clause)
|
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
|
6
6
|
* @HomePage: https://koatty.org/
|
|
@@ -73,9 +73,37 @@ export declare function Equals(comparison: any, validationOptions?: ValidationOp
|
|
|
73
73
|
export declare function Expose(): PropertyDecorator;
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
|
-
*
|
|
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}
|
|
77
95
|
*/
|
|
78
|
-
export declare
|
|
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;
|
|
79
107
|
|
|
80
108
|
export declare type HashAlgorithm = "md4" | "md5" | "sha1" | "sha256" | "sha384" | "sha512" | "ripemd128" | "ripemd160" | "tiger128" | "tiger160" | "tiger192" | "crc32" | "crc32b";
|
|
81
109
|
|
|
@@ -270,17 +298,17 @@ export declare function Length(min: number, max?: number, validationOptions?: Va
|
|
|
270
298
|
* @param {ValidationOptions} [validationOptions]
|
|
271
299
|
* @returns {PropertyDecorator}
|
|
272
300
|
*/
|
|
273
|
-
export declare function
|
|
301
|
+
export declare function Lt(max: number, validationOptions?: ValidationOptions): PropertyDecorator;
|
|
274
302
|
|
|
275
303
|
/**
|
|
276
|
-
* Checks if the first number is
|
|
304
|
+
* Checks if the first number is less than or equal to the max value.
|
|
277
305
|
*
|
|
278
306
|
* @export
|
|
279
|
-
* @param {number}
|
|
307
|
+
* @param {number} max
|
|
280
308
|
* @param {ValidationOptions} [validationOptions]
|
|
281
309
|
* @returns {PropertyDecorator}
|
|
282
310
|
*/
|
|
283
|
-
export declare function
|
|
311
|
+
export declare function Lte(max: number, validationOptions?: ValidationOptions): PropertyDecorator;
|
|
284
312
|
|
|
285
313
|
/**
|
|
286
314
|
* Checks if value does not match ("!==") the comparison.
|
|
@@ -343,10 +371,11 @@ export declare function plainToClass(clazz: any, data: any, convert?: boolean):
|
|
|
343
371
|
*
|
|
344
372
|
* @export
|
|
345
373
|
* @param {(ValidRules | ValidRules[] | Function)} rule
|
|
346
|
-
* @param {
|
|
347
|
-
* @
|
|
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}
|
|
348
377
|
*/
|
|
349
|
-
export declare function Valid(rule: ValidRules | ValidRules[] | Function,
|
|
378
|
+
export declare function Valid(rule: ValidRules | ValidRules[] | Function, options?: string | ValidOtpions): ParameterDecorator;
|
|
350
379
|
|
|
351
380
|
declare class ValidateClass {
|
|
352
381
|
/**
|
|
@@ -377,19 +406,10 @@ declare class ValidateClass {
|
|
|
377
406
|
*/
|
|
378
407
|
export declare function Validated(): MethodDecorator;
|
|
379
408
|
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
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;
|
|
409
|
+
export declare type ValidOtpions = {
|
|
410
|
+
message: string;
|
|
411
|
+
value: any;
|
|
412
|
+
};
|
|
393
413
|
|
|
394
414
|
/**
|
|
395
415
|
* type checked rules
|
|
@@ -397,6 +417,6 @@ export declare function ValidatorFuncs(name: string, value: any, type: string, r
|
|
|
397
417
|
* @export
|
|
398
418
|
* @type {number}
|
|
399
419
|
*/
|
|
400
|
-
export declare type ValidRules = "IsNotEmpty" | "IsDate" | "IsEmail" | "IsIP" | "IsPhoneNumber" | "IsUrl" | "IsHash" | "IsCnName" | "IsIdNumber" | "IsZipCode" | "IsMobile" | "IsPlateNumber";
|
|
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";
|
|
401
421
|
|
|
402
422
|
export { }
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @Author: richen
|
|
3
|
-
* @Date: 2022-02-
|
|
3
|
+
* @Date: 2022-02-25 09:58:02
|
|
4
4
|
* @License: BSD (3-Clause)
|
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
|
6
6
|
* @HomePage: https://koatty.org/
|
|
@@ -314,7 +314,7 @@ function plateNumber(value) {
|
|
|
314
314
|
* @Usage:
|
|
315
315
|
* @Author: richen
|
|
316
316
|
* @Date: 2021-11-25 10:47:04
|
|
317
|
-
* @LastEditTime:
|
|
317
|
+
* @LastEditTime: 2022-02-25 09:51:04
|
|
318
318
|
*/
|
|
319
319
|
// constant
|
|
320
320
|
const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
|
|
@@ -403,36 +403,13 @@ const ClassValidator = ValidateClass.getInstance();
|
|
|
403
403
|
/**
|
|
404
404
|
* Validator Functions
|
|
405
405
|
*/
|
|
406
|
-
const
|
|
407
|
-
/**
|
|
408
|
-
* Checks if value matches ("===") the comparison.
|
|
409
|
-
*/
|
|
410
|
-
Equals: (value, comparison) => {
|
|
411
|
-
return classValidator.equals(value, comparison);
|
|
412
|
-
},
|
|
413
|
-
/**
|
|
414
|
-
* Checks if value does not match ("!==") the comparison.
|
|
415
|
-
*/
|
|
416
|
-
NotEquals: (value, comparison) => {
|
|
417
|
-
return classValidator.notEquals(value, comparison);
|
|
418
|
-
},
|
|
419
|
-
/**
|
|
420
|
-
* Checks if the string contains the seed. If given value is not a string, then it returns false.
|
|
421
|
-
*/
|
|
422
|
-
Contains: (value, seed) => {
|
|
423
|
-
return classValidator.contains(value, seed);
|
|
424
|
-
},
|
|
425
|
-
/**
|
|
426
|
-
* Checks if given value is in a array of allowed values.
|
|
427
|
-
*/
|
|
428
|
-
IsIn: (value, possibleValues) => {
|
|
429
|
-
return classValidator.isIn(value, possibleValues);
|
|
430
|
-
},
|
|
406
|
+
const ValidFuncs = {
|
|
431
407
|
/**
|
|
432
|
-
* Checks
|
|
408
|
+
* Checks value is not empty, undefined, null, '', NaN, [], {} and any empty string(including spaces,
|
|
409
|
+
* tabs, formfeeds, etc.), returns false
|
|
433
410
|
*/
|
|
434
|
-
|
|
435
|
-
return
|
|
411
|
+
IsNotEmpty: (value) => {
|
|
412
|
+
return !helper__namespace.isEmpty(value);
|
|
436
413
|
},
|
|
437
414
|
/**
|
|
438
415
|
* Checks if a given value is a real date.
|
|
@@ -440,25 +417,6 @@ const FunctionValidator = {
|
|
|
440
417
|
IsDate: (value) => {
|
|
441
418
|
return helper__namespace.isDate(value);
|
|
442
419
|
},
|
|
443
|
-
/**
|
|
444
|
-
* Checks if the first number is greater than or equal to the second.
|
|
445
|
-
*/
|
|
446
|
-
Min: (num, min) => {
|
|
447
|
-
return helper__namespace.toNumber(num) >= min;
|
|
448
|
-
},
|
|
449
|
-
/**
|
|
450
|
-
* Checks if the first number is less than or equal to the second.
|
|
451
|
-
*/
|
|
452
|
-
Max: (num, max) => {
|
|
453
|
-
return helper__namespace.toNumber(num) <= max;
|
|
454
|
-
},
|
|
455
|
-
/**
|
|
456
|
-
* Checks if the string's length falls in a range. Note: this function takes into account surrogate pairs.
|
|
457
|
-
* If given value is not a string, then it returns false.
|
|
458
|
-
*/
|
|
459
|
-
Length: (value, min, max) => {
|
|
460
|
-
return classValidator.length(value, min, max);
|
|
461
|
-
},
|
|
462
420
|
/**
|
|
463
421
|
* Checks if the string is an email. If given value is not a string, then it returns false.
|
|
464
422
|
*/
|
|
@@ -541,73 +499,165 @@ const FunctionValidator = {
|
|
|
541
499
|
return plateNumber(value);
|
|
542
500
|
},
|
|
543
501
|
/**
|
|
544
|
-
* Checks value
|
|
545
|
-
* tabs, formfeeds, etc.), returns false
|
|
502
|
+
* Checks if value matches ("===") the comparison.
|
|
546
503
|
*/
|
|
547
|
-
|
|
548
|
-
return
|
|
549
|
-
}
|
|
504
|
+
Equals: (value, comparison) => {
|
|
505
|
+
return classValidator.equals(value, comparison);
|
|
506
|
+
},
|
|
507
|
+
/**
|
|
508
|
+
* Checks if value does not match ("!==") the comparison.
|
|
509
|
+
*/
|
|
510
|
+
NotEquals: (value, comparison) => {
|
|
511
|
+
return classValidator.notEquals(value, comparison);
|
|
512
|
+
},
|
|
513
|
+
/**
|
|
514
|
+
* Checks if the string contains the seed. If given value is not a string, then it returns false.
|
|
515
|
+
*/
|
|
516
|
+
Contains: (value, seed) => {
|
|
517
|
+
return classValidator.contains(value, seed);
|
|
518
|
+
},
|
|
519
|
+
/**
|
|
520
|
+
* Checks if given value is in a array of allowed values.
|
|
521
|
+
*/
|
|
522
|
+
IsIn: (value, possibleValues) => {
|
|
523
|
+
return classValidator.isIn(value, possibleValues);
|
|
524
|
+
},
|
|
525
|
+
/**
|
|
526
|
+
* Checks if given value not in a array of allowed values.
|
|
527
|
+
*/
|
|
528
|
+
IsNotIn: (value, possibleValues) => {
|
|
529
|
+
return classValidator.isNotIn(value, possibleValues);
|
|
530
|
+
},
|
|
531
|
+
/**
|
|
532
|
+
* Checks if the first number is greater than or equal to the second.
|
|
533
|
+
*/
|
|
534
|
+
Gt: (num, min) => {
|
|
535
|
+
return helper__namespace.toNumber(num) > min;
|
|
536
|
+
},
|
|
537
|
+
/**
|
|
538
|
+
* Checks if the first number is less than or equal to the second.
|
|
539
|
+
*/
|
|
540
|
+
Lt: (num, max) => {
|
|
541
|
+
return helper__namespace.toNumber(num) < max;
|
|
542
|
+
},
|
|
543
|
+
/**
|
|
544
|
+
* Checks if the first number is greater than or equal to the second.
|
|
545
|
+
*/
|
|
546
|
+
Gte: (num, min) => {
|
|
547
|
+
return helper__namespace.toNumber(num) >= min;
|
|
548
|
+
},
|
|
549
|
+
/**
|
|
550
|
+
* Checks if the first number is less than or equal to the second.
|
|
551
|
+
*/
|
|
552
|
+
Lte: (num, max) => {
|
|
553
|
+
return helper__namespace.toNumber(num) <= max;
|
|
554
|
+
},
|
|
550
555
|
};
|
|
551
556
|
/**
|
|
552
557
|
* Use functions or built-in rules for validation.
|
|
553
558
|
*
|
|
554
559
|
* @export
|
|
555
|
-
* @param {
|
|
556
|
-
* @param {
|
|
557
|
-
* @param {string}
|
|
558
|
-
* @
|
|
559
|
-
* @param {string} [message]
|
|
560
|
-
* @param {boolean} [checkType=true]
|
|
561
|
-
* @returns
|
|
560
|
+
* @param {ValidRules} rule
|
|
561
|
+
* @param {unknown} value
|
|
562
|
+
* @param {(string | ValidOtpions)} [options]
|
|
563
|
+
* @returns {*}
|
|
562
564
|
*/
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
565
|
+
const FunctionValidator = {
|
|
566
|
+
IsNotEmpty: function (value, options) {
|
|
567
|
+
throw new Error("Function not implemented.");
|
|
568
|
+
},
|
|
569
|
+
IsDate: function (value, options) {
|
|
570
|
+
throw new Error("Function not implemented.");
|
|
571
|
+
},
|
|
572
|
+
IsEmail: function (value, options) {
|
|
573
|
+
throw new Error("Function not implemented.");
|
|
574
|
+
},
|
|
575
|
+
IsIP: function (value, options) {
|
|
576
|
+
throw new Error("Function not implemented.");
|
|
577
|
+
},
|
|
578
|
+
IsPhoneNumber: function (value, options) {
|
|
579
|
+
throw new Error("Function not implemented.");
|
|
580
|
+
},
|
|
581
|
+
IsUrl: function (value, options) {
|
|
582
|
+
throw new Error("Function not implemented.");
|
|
583
|
+
},
|
|
584
|
+
IsHash: function (value, options) {
|
|
585
|
+
throw new Error("Function not implemented.");
|
|
586
|
+
},
|
|
587
|
+
IsCnName: function (value, options) {
|
|
588
|
+
throw new Error("Function not implemented.");
|
|
589
|
+
},
|
|
590
|
+
IsIdNumber: function (value, options) {
|
|
591
|
+
throw new Error("Function not implemented.");
|
|
592
|
+
},
|
|
593
|
+
IsZipCode: function (value, options) {
|
|
594
|
+
throw new Error("Function not implemented.");
|
|
595
|
+
},
|
|
596
|
+
IsMobile: function (value, options) {
|
|
597
|
+
throw new Error("Function not implemented.");
|
|
598
|
+
},
|
|
599
|
+
IsPlateNumber: function (value, options) {
|
|
600
|
+
throw new Error("Function not implemented.");
|
|
601
|
+
},
|
|
602
|
+
Equals: function (value, options) {
|
|
603
|
+
throw new Error("Function not implemented.");
|
|
604
|
+
},
|
|
605
|
+
NotEquals: function (value, options) {
|
|
606
|
+
throw new Error("Function not implemented.");
|
|
607
|
+
},
|
|
608
|
+
Contains: function (value, options) {
|
|
609
|
+
throw new Error("Function not implemented.");
|
|
610
|
+
},
|
|
611
|
+
IsIn: function (value, options) {
|
|
612
|
+
throw new Error("Function not implemented.");
|
|
613
|
+
},
|
|
614
|
+
IsNotIn: function (value, options) {
|
|
615
|
+
throw new Error("Function not implemented.");
|
|
616
|
+
},
|
|
617
|
+
Gt: function (value, options) {
|
|
618
|
+
throw new Error("Function not implemented.");
|
|
619
|
+
},
|
|
620
|
+
Lt: function (value, options) {
|
|
621
|
+
throw new Error("Function not implemented.");
|
|
622
|
+
},
|
|
623
|
+
Gte: function (value, options) {
|
|
624
|
+
throw new Error("Function not implemented.");
|
|
625
|
+
},
|
|
626
|
+
Lte: function (value, options) {
|
|
627
|
+
throw new Error("Function not implemented.");
|
|
579
628
|
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
629
|
+
};
|
|
630
|
+
Object.keys(ValidFuncs).forEach((key) => {
|
|
631
|
+
FunctionValidator[key] = (value, options) => {
|
|
632
|
+
if (helper__namespace.isString(options)) {
|
|
633
|
+
options = { message: options, value: null };
|
|
584
634
|
}
|
|
585
|
-
if (
|
|
586
|
-
const err = new Error(message || `ValidatorError: invalid arguments
|
|
635
|
+
if (!ValidFuncs[key](value, options)) {
|
|
636
|
+
const err = new Error(options.message || `ValidatorError: invalid arguments.`);
|
|
587
637
|
err.code = 400;
|
|
588
638
|
err.status = 400;
|
|
589
639
|
throw err;
|
|
590
640
|
}
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
}
|
|
641
|
+
};
|
|
642
|
+
});
|
|
594
643
|
|
|
595
644
|
/*
|
|
596
645
|
* @Description:
|
|
597
646
|
* @Usage:
|
|
598
647
|
* @Author: richen
|
|
599
648
|
* @Date: 2021-11-25 10:46:57
|
|
600
|
-
* @LastEditTime: 2022-02-
|
|
649
|
+
* @LastEditTime: 2022-02-24 15:33:34
|
|
601
650
|
*/
|
|
602
651
|
/**
|
|
603
652
|
* Validation parameter's type and values.
|
|
604
653
|
*
|
|
605
654
|
* @export
|
|
606
655
|
* @param {(ValidRules | ValidRules[] | Function)} rule
|
|
607
|
-
* @param {
|
|
608
|
-
* @
|
|
656
|
+
* @param {*} [options] If the options type is a string, the value is the error message of the validation rule.
|
|
657
|
+
* Some validation rules require additional parameters, ext: @Valid("Gte", {message:"Requires value greater than or equal to 100", value: 100})
|
|
658
|
+
* @returns {*} {ParameterDecorator}
|
|
609
659
|
*/
|
|
610
|
-
function Valid(rule,
|
|
660
|
+
function Valid(rule, options) {
|
|
611
661
|
let rules = [];
|
|
612
662
|
if (helper__namespace.isString(rule)) {
|
|
613
663
|
rules = rule.split(",");
|
|
@@ -619,10 +669,13 @@ function Valid(rule, message) {
|
|
|
619
669
|
// 获取成员参数类型
|
|
620
670
|
const paramTypes = Reflect.getMetadata("design:paramtypes", target, propertyKey);
|
|
621
671
|
const type = (paramTypes[descriptor] && paramTypes[descriptor].name) ? paramTypes[descriptor].name : "object";
|
|
672
|
+
if (helper__namespace.isString(options)) {
|
|
673
|
+
options = { message: options, value: null };
|
|
674
|
+
}
|
|
622
675
|
koatty_container.IOCContainer.attachPropertyData(PARAM_RULE_KEY, {
|
|
623
676
|
name: propertyKey,
|
|
624
677
|
rule: rules,
|
|
625
|
-
|
|
678
|
+
options,
|
|
626
679
|
index: descriptor,
|
|
627
680
|
type
|
|
628
681
|
}, target, propertyKey);
|
|
@@ -1025,7 +1078,61 @@ function IsDate(validationOptions) {
|
|
|
1025
1078
|
* @param {ValidationOptions} [validationOptions]
|
|
1026
1079
|
* @returns {PropertyDecorator}
|
|
1027
1080
|
*/
|
|
1028
|
-
function
|
|
1081
|
+
function Gt(min, validationOptions) {
|
|
1082
|
+
return function (object, propertyName) {
|
|
1083
|
+
setExpose(object, propertyName);
|
|
1084
|
+
classValidator.registerDecorator({
|
|
1085
|
+
name: "vMin",
|
|
1086
|
+
target: object.constructor,
|
|
1087
|
+
propertyName,
|
|
1088
|
+
options: validationOptions,
|
|
1089
|
+
validator: {
|
|
1090
|
+
validate(value, args) {
|
|
1091
|
+
return helper__namespace.toNumber(value) > min;
|
|
1092
|
+
},
|
|
1093
|
+
defaultMessage(args) {
|
|
1094
|
+
return `invalid parameter ($property).`;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
});
|
|
1098
|
+
};
|
|
1099
|
+
}
|
|
1100
|
+
/**
|
|
1101
|
+
* Checks if the first number is less than or equal to the max value.
|
|
1102
|
+
*
|
|
1103
|
+
* @export
|
|
1104
|
+
* @param {number} max
|
|
1105
|
+
* @param {ValidationOptions} [validationOptions]
|
|
1106
|
+
* @returns {PropertyDecorator}
|
|
1107
|
+
*/
|
|
1108
|
+
function Lt(max, validationOptions) {
|
|
1109
|
+
return function (object, propertyName) {
|
|
1110
|
+
setExpose(object, propertyName);
|
|
1111
|
+
classValidator.registerDecorator({
|
|
1112
|
+
name: "vMax",
|
|
1113
|
+
target: object.constructor,
|
|
1114
|
+
propertyName,
|
|
1115
|
+
options: validationOptions,
|
|
1116
|
+
validator: {
|
|
1117
|
+
validate(value, args) {
|
|
1118
|
+
return helper__namespace.toNumber(value) < max;
|
|
1119
|
+
},
|
|
1120
|
+
defaultMessage(args) {
|
|
1121
|
+
return `invalid parameter ($property).`;
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
});
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
/**
|
|
1128
|
+
* Checks if the first number is greater than or equal to the min value.
|
|
1129
|
+
*
|
|
1130
|
+
* @export
|
|
1131
|
+
* @param {number} min
|
|
1132
|
+
* @param {ValidationOptions} [validationOptions]
|
|
1133
|
+
* @returns {PropertyDecorator}
|
|
1134
|
+
*/
|
|
1135
|
+
function Gte(min, validationOptions) {
|
|
1029
1136
|
return function (object, propertyName) {
|
|
1030
1137
|
setExpose(object, propertyName);
|
|
1031
1138
|
classValidator.registerDecorator({
|
|
@@ -1052,7 +1159,7 @@ function Min(min, validationOptions) {
|
|
|
1052
1159
|
* @param {ValidationOptions} [validationOptions]
|
|
1053
1160
|
* @returns {PropertyDecorator}
|
|
1054
1161
|
*/
|
|
1055
|
-
function
|
|
1162
|
+
function Lte(max, validationOptions) {
|
|
1056
1163
|
return function (object, propertyName) {
|
|
1057
1164
|
setExpose(object, propertyName);
|
|
1058
1165
|
classValidator.registerDecorator({
|
|
@@ -1246,6 +1353,8 @@ exports.ENABLE_VALIDATED = ENABLE_VALIDATED;
|
|
|
1246
1353
|
exports.Equals = Equals;
|
|
1247
1354
|
exports.Expose = Expose;
|
|
1248
1355
|
exports.FunctionValidator = FunctionValidator;
|
|
1356
|
+
exports.Gt = Gt;
|
|
1357
|
+
exports.Gte = Gte;
|
|
1249
1358
|
exports.IsCnName = IsCnName;
|
|
1250
1359
|
exports.IsDate = IsDate;
|
|
1251
1360
|
exports.IsDefined = IsDefined;
|
|
@@ -1262,15 +1371,14 @@ exports.IsPlateNumber = IsPlateNumber;
|
|
|
1262
1371
|
exports.IsUrl = IsUrl;
|
|
1263
1372
|
exports.IsZipCode = IsZipCode;
|
|
1264
1373
|
exports.Length = Length;
|
|
1265
|
-
exports.
|
|
1266
|
-
exports.
|
|
1374
|
+
exports.Lt = Lt;
|
|
1375
|
+
exports.Lte = Lte;
|
|
1267
1376
|
exports.NotEquals = NotEquals;
|
|
1268
1377
|
exports.PARAM_CHECK_KEY = PARAM_CHECK_KEY;
|
|
1269
1378
|
exports.PARAM_RULE_KEY = PARAM_RULE_KEY;
|
|
1270
1379
|
exports.PARAM_TYPE_KEY = PARAM_TYPE_KEY;
|
|
1271
1380
|
exports.Valid = Valid;
|
|
1272
1381
|
exports.Validated = Validated;
|
|
1273
|
-
exports.ValidatorFuncs = ValidatorFuncs;
|
|
1274
1382
|
exports.checkParamsType = checkParamsType;
|
|
1275
1383
|
exports.convertDtoParamsType = convertDtoParamsType;
|
|
1276
1384
|
exports.convertParamsType = convertParamsType;
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @Author: richen
|
|
3
|
-
* @Date: 2022-02-
|
|
3
|
+
* @Date: 2022-02-25 09:58:02
|
|
4
4
|
* @License: BSD (3-Clause)
|
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
|
6
6
|
* @HomePage: https://koatty.org/
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import * as helper from 'koatty_lib';
|
|
9
9
|
import 'reflect-metadata';
|
|
10
10
|
import { getOriginMetadata, IOCContainer } from 'koatty_container';
|
|
11
|
-
import { validate,
|
|
11
|
+
import { validate, isEmail, isIP, isPhoneNumber, isURL, isHash, equals, notEquals, contains, isIn, isNotIn, registerDecorator, isDate, length } from 'class-validator';
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* @ author: richen
|
|
@@ -290,7 +290,7 @@ function plateNumber(value) {
|
|
|
290
290
|
* @Usage:
|
|
291
291
|
* @Author: richen
|
|
292
292
|
* @Date: 2021-11-25 10:47:04
|
|
293
|
-
* @LastEditTime:
|
|
293
|
+
* @LastEditTime: 2022-02-25 09:51:04
|
|
294
294
|
*/
|
|
295
295
|
// constant
|
|
296
296
|
const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
|
|
@@ -379,36 +379,13 @@ const ClassValidator = ValidateClass.getInstance();
|
|
|
379
379
|
/**
|
|
380
380
|
* Validator Functions
|
|
381
381
|
*/
|
|
382
|
-
const
|
|
383
|
-
/**
|
|
384
|
-
* Checks if value matches ("===") the comparison.
|
|
385
|
-
*/
|
|
386
|
-
Equals: (value, comparison) => {
|
|
387
|
-
return equals(value, comparison);
|
|
388
|
-
},
|
|
389
|
-
/**
|
|
390
|
-
* Checks if value does not match ("!==") the comparison.
|
|
391
|
-
*/
|
|
392
|
-
NotEquals: (value, comparison) => {
|
|
393
|
-
return notEquals(value, comparison);
|
|
394
|
-
},
|
|
395
|
-
/**
|
|
396
|
-
* Checks if the string contains the seed. If given value is not a string, then it returns false.
|
|
397
|
-
*/
|
|
398
|
-
Contains: (value, seed) => {
|
|
399
|
-
return contains(value, seed);
|
|
400
|
-
},
|
|
401
|
-
/**
|
|
402
|
-
* Checks if given value is in a array of allowed values.
|
|
403
|
-
*/
|
|
404
|
-
IsIn: (value, possibleValues) => {
|
|
405
|
-
return isIn(value, possibleValues);
|
|
406
|
-
},
|
|
382
|
+
const ValidFuncs = {
|
|
407
383
|
/**
|
|
408
|
-
* Checks
|
|
384
|
+
* Checks value is not empty, undefined, null, '', NaN, [], {} and any empty string(including spaces,
|
|
385
|
+
* tabs, formfeeds, etc.), returns false
|
|
409
386
|
*/
|
|
410
|
-
|
|
411
|
-
return
|
|
387
|
+
IsNotEmpty: (value) => {
|
|
388
|
+
return !helper.isEmpty(value);
|
|
412
389
|
},
|
|
413
390
|
/**
|
|
414
391
|
* Checks if a given value is a real date.
|
|
@@ -416,25 +393,6 @@ const FunctionValidator = {
|
|
|
416
393
|
IsDate: (value) => {
|
|
417
394
|
return helper.isDate(value);
|
|
418
395
|
},
|
|
419
|
-
/**
|
|
420
|
-
* Checks if the first number is greater than or equal to the second.
|
|
421
|
-
*/
|
|
422
|
-
Min: (num, min) => {
|
|
423
|
-
return helper.toNumber(num) >= min;
|
|
424
|
-
},
|
|
425
|
-
/**
|
|
426
|
-
* Checks if the first number is less than or equal to the second.
|
|
427
|
-
*/
|
|
428
|
-
Max: (num, max) => {
|
|
429
|
-
return helper.toNumber(num) <= max;
|
|
430
|
-
},
|
|
431
|
-
/**
|
|
432
|
-
* Checks if the string's length falls in a range. Note: this function takes into account surrogate pairs.
|
|
433
|
-
* If given value is not a string, then it returns false.
|
|
434
|
-
*/
|
|
435
|
-
Length: (value, min, max) => {
|
|
436
|
-
return length(value, min, max);
|
|
437
|
-
},
|
|
438
396
|
/**
|
|
439
397
|
* Checks if the string is an email. If given value is not a string, then it returns false.
|
|
440
398
|
*/
|
|
@@ -517,73 +475,165 @@ const FunctionValidator = {
|
|
|
517
475
|
return plateNumber(value);
|
|
518
476
|
},
|
|
519
477
|
/**
|
|
520
|
-
* Checks value
|
|
521
|
-
* tabs, formfeeds, etc.), returns false
|
|
478
|
+
* Checks if value matches ("===") the comparison.
|
|
522
479
|
*/
|
|
523
|
-
|
|
524
|
-
return
|
|
525
|
-
}
|
|
480
|
+
Equals: (value, comparison) => {
|
|
481
|
+
return equals(value, comparison);
|
|
482
|
+
},
|
|
483
|
+
/**
|
|
484
|
+
* Checks if value does not match ("!==") the comparison.
|
|
485
|
+
*/
|
|
486
|
+
NotEquals: (value, comparison) => {
|
|
487
|
+
return notEquals(value, comparison);
|
|
488
|
+
},
|
|
489
|
+
/**
|
|
490
|
+
* Checks if the string contains the seed. If given value is not a string, then it returns false.
|
|
491
|
+
*/
|
|
492
|
+
Contains: (value, seed) => {
|
|
493
|
+
return contains(value, seed);
|
|
494
|
+
},
|
|
495
|
+
/**
|
|
496
|
+
* Checks if given value is in a array of allowed values.
|
|
497
|
+
*/
|
|
498
|
+
IsIn: (value, possibleValues) => {
|
|
499
|
+
return isIn(value, possibleValues);
|
|
500
|
+
},
|
|
501
|
+
/**
|
|
502
|
+
* Checks if given value not in a array of allowed values.
|
|
503
|
+
*/
|
|
504
|
+
IsNotIn: (value, possibleValues) => {
|
|
505
|
+
return isNotIn(value, possibleValues);
|
|
506
|
+
},
|
|
507
|
+
/**
|
|
508
|
+
* Checks if the first number is greater than or equal to the second.
|
|
509
|
+
*/
|
|
510
|
+
Gt: (num, min) => {
|
|
511
|
+
return helper.toNumber(num) > min;
|
|
512
|
+
},
|
|
513
|
+
/**
|
|
514
|
+
* Checks if the first number is less than or equal to the second.
|
|
515
|
+
*/
|
|
516
|
+
Lt: (num, max) => {
|
|
517
|
+
return helper.toNumber(num) < max;
|
|
518
|
+
},
|
|
519
|
+
/**
|
|
520
|
+
* Checks if the first number is greater than or equal to the second.
|
|
521
|
+
*/
|
|
522
|
+
Gte: (num, min) => {
|
|
523
|
+
return helper.toNumber(num) >= min;
|
|
524
|
+
},
|
|
525
|
+
/**
|
|
526
|
+
* Checks if the first number is less than or equal to the second.
|
|
527
|
+
*/
|
|
528
|
+
Lte: (num, max) => {
|
|
529
|
+
return helper.toNumber(num) <= max;
|
|
530
|
+
},
|
|
526
531
|
};
|
|
527
532
|
/**
|
|
528
533
|
* Use functions or built-in rules for validation.
|
|
529
534
|
*
|
|
530
535
|
* @export
|
|
531
|
-
* @param {
|
|
532
|
-
* @param {
|
|
533
|
-
* @param {string}
|
|
534
|
-
* @
|
|
535
|
-
* @param {string} [message]
|
|
536
|
-
* @param {boolean} [checkType=true]
|
|
537
|
-
* @returns
|
|
536
|
+
* @param {ValidRules} rule
|
|
537
|
+
* @param {unknown} value
|
|
538
|
+
* @param {(string | ValidOtpions)} [options]
|
|
539
|
+
* @returns {*}
|
|
538
540
|
*/
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
541
|
+
const FunctionValidator = {
|
|
542
|
+
IsNotEmpty: function (value, options) {
|
|
543
|
+
throw new Error("Function not implemented.");
|
|
544
|
+
},
|
|
545
|
+
IsDate: function (value, options) {
|
|
546
|
+
throw new Error("Function not implemented.");
|
|
547
|
+
},
|
|
548
|
+
IsEmail: function (value, options) {
|
|
549
|
+
throw new Error("Function not implemented.");
|
|
550
|
+
},
|
|
551
|
+
IsIP: function (value, options) {
|
|
552
|
+
throw new Error("Function not implemented.");
|
|
553
|
+
},
|
|
554
|
+
IsPhoneNumber: function (value, options) {
|
|
555
|
+
throw new Error("Function not implemented.");
|
|
556
|
+
},
|
|
557
|
+
IsUrl: function (value, options) {
|
|
558
|
+
throw new Error("Function not implemented.");
|
|
559
|
+
},
|
|
560
|
+
IsHash: function (value, options) {
|
|
561
|
+
throw new Error("Function not implemented.");
|
|
562
|
+
},
|
|
563
|
+
IsCnName: function (value, options) {
|
|
564
|
+
throw new Error("Function not implemented.");
|
|
565
|
+
},
|
|
566
|
+
IsIdNumber: function (value, options) {
|
|
567
|
+
throw new Error("Function not implemented.");
|
|
568
|
+
},
|
|
569
|
+
IsZipCode: function (value, options) {
|
|
570
|
+
throw new Error("Function not implemented.");
|
|
571
|
+
},
|
|
572
|
+
IsMobile: function (value, options) {
|
|
573
|
+
throw new Error("Function not implemented.");
|
|
574
|
+
},
|
|
575
|
+
IsPlateNumber: function (value, options) {
|
|
576
|
+
throw new Error("Function not implemented.");
|
|
577
|
+
},
|
|
578
|
+
Equals: function (value, options) {
|
|
579
|
+
throw new Error("Function not implemented.");
|
|
580
|
+
},
|
|
581
|
+
NotEquals: function (value, options) {
|
|
582
|
+
throw new Error("Function not implemented.");
|
|
583
|
+
},
|
|
584
|
+
Contains: function (value, options) {
|
|
585
|
+
throw new Error("Function not implemented.");
|
|
586
|
+
},
|
|
587
|
+
IsIn: function (value, options) {
|
|
588
|
+
throw new Error("Function not implemented.");
|
|
589
|
+
},
|
|
590
|
+
IsNotIn: function (value, options) {
|
|
591
|
+
throw new Error("Function not implemented.");
|
|
592
|
+
},
|
|
593
|
+
Gt: function (value, options) {
|
|
594
|
+
throw new Error("Function not implemented.");
|
|
595
|
+
},
|
|
596
|
+
Lt: function (value, options) {
|
|
597
|
+
throw new Error("Function not implemented.");
|
|
598
|
+
},
|
|
599
|
+
Gte: function (value, options) {
|
|
600
|
+
throw new Error("Function not implemented.");
|
|
601
|
+
},
|
|
602
|
+
Lte: function (value, options) {
|
|
603
|
+
throw new Error("Function not implemented.");
|
|
555
604
|
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
605
|
+
};
|
|
606
|
+
Object.keys(ValidFuncs).forEach((key) => {
|
|
607
|
+
FunctionValidator[key] = (value, options) => {
|
|
608
|
+
if (helper.isString(options)) {
|
|
609
|
+
options = { message: options, value: null };
|
|
560
610
|
}
|
|
561
|
-
if (
|
|
562
|
-
const err = new Error(message || `ValidatorError: invalid arguments
|
|
611
|
+
if (!ValidFuncs[key](value, options)) {
|
|
612
|
+
const err = new Error(options.message || `ValidatorError: invalid arguments.`);
|
|
563
613
|
err.code = 400;
|
|
564
614
|
err.status = 400;
|
|
565
615
|
throw err;
|
|
566
616
|
}
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
}
|
|
617
|
+
};
|
|
618
|
+
});
|
|
570
619
|
|
|
571
620
|
/*
|
|
572
621
|
* @Description:
|
|
573
622
|
* @Usage:
|
|
574
623
|
* @Author: richen
|
|
575
624
|
* @Date: 2021-11-25 10:46:57
|
|
576
|
-
* @LastEditTime: 2022-02-
|
|
625
|
+
* @LastEditTime: 2022-02-24 15:33:34
|
|
577
626
|
*/
|
|
578
627
|
/**
|
|
579
628
|
* Validation parameter's type and values.
|
|
580
629
|
*
|
|
581
630
|
* @export
|
|
582
631
|
* @param {(ValidRules | ValidRules[] | Function)} rule
|
|
583
|
-
* @param {
|
|
584
|
-
* @
|
|
632
|
+
* @param {*} [options] If the options type is a string, the value is the error message of the validation rule.
|
|
633
|
+
* Some validation rules require additional parameters, ext: @Valid("Gte", {message:"Requires value greater than or equal to 100", value: 100})
|
|
634
|
+
* @returns {*} {ParameterDecorator}
|
|
585
635
|
*/
|
|
586
|
-
function Valid(rule,
|
|
636
|
+
function Valid(rule, options) {
|
|
587
637
|
let rules = [];
|
|
588
638
|
if (helper.isString(rule)) {
|
|
589
639
|
rules = rule.split(",");
|
|
@@ -595,10 +645,13 @@ function Valid(rule, message) {
|
|
|
595
645
|
// 获取成员参数类型
|
|
596
646
|
const paramTypes = Reflect.getMetadata("design:paramtypes", target, propertyKey);
|
|
597
647
|
const type = (paramTypes[descriptor] && paramTypes[descriptor].name) ? paramTypes[descriptor].name : "object";
|
|
648
|
+
if (helper.isString(options)) {
|
|
649
|
+
options = { message: options, value: null };
|
|
650
|
+
}
|
|
598
651
|
IOCContainer.attachPropertyData(PARAM_RULE_KEY, {
|
|
599
652
|
name: propertyKey,
|
|
600
653
|
rule: rules,
|
|
601
|
-
|
|
654
|
+
options,
|
|
602
655
|
index: descriptor,
|
|
603
656
|
type
|
|
604
657
|
}, target, propertyKey);
|
|
@@ -1001,7 +1054,61 @@ function IsDate(validationOptions) {
|
|
|
1001
1054
|
* @param {ValidationOptions} [validationOptions]
|
|
1002
1055
|
* @returns {PropertyDecorator}
|
|
1003
1056
|
*/
|
|
1004
|
-
function
|
|
1057
|
+
function Gt(min, validationOptions) {
|
|
1058
|
+
return function (object, propertyName) {
|
|
1059
|
+
setExpose(object, propertyName);
|
|
1060
|
+
registerDecorator({
|
|
1061
|
+
name: "vMin",
|
|
1062
|
+
target: object.constructor,
|
|
1063
|
+
propertyName,
|
|
1064
|
+
options: validationOptions,
|
|
1065
|
+
validator: {
|
|
1066
|
+
validate(value, args) {
|
|
1067
|
+
return helper.toNumber(value) > min;
|
|
1068
|
+
},
|
|
1069
|
+
defaultMessage(args) {
|
|
1070
|
+
return `invalid parameter ($property).`;
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
});
|
|
1074
|
+
};
|
|
1075
|
+
}
|
|
1076
|
+
/**
|
|
1077
|
+
* Checks if the first number is less than or equal to the max value.
|
|
1078
|
+
*
|
|
1079
|
+
* @export
|
|
1080
|
+
* @param {number} max
|
|
1081
|
+
* @param {ValidationOptions} [validationOptions]
|
|
1082
|
+
* @returns {PropertyDecorator}
|
|
1083
|
+
*/
|
|
1084
|
+
function Lt(max, validationOptions) {
|
|
1085
|
+
return function (object, propertyName) {
|
|
1086
|
+
setExpose(object, propertyName);
|
|
1087
|
+
registerDecorator({
|
|
1088
|
+
name: "vMax",
|
|
1089
|
+
target: object.constructor,
|
|
1090
|
+
propertyName,
|
|
1091
|
+
options: validationOptions,
|
|
1092
|
+
validator: {
|
|
1093
|
+
validate(value, args) {
|
|
1094
|
+
return helper.toNumber(value) < max;
|
|
1095
|
+
},
|
|
1096
|
+
defaultMessage(args) {
|
|
1097
|
+
return `invalid parameter ($property).`;
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
});
|
|
1101
|
+
};
|
|
1102
|
+
}
|
|
1103
|
+
/**
|
|
1104
|
+
* Checks if the first number is greater than or equal to the min value.
|
|
1105
|
+
*
|
|
1106
|
+
* @export
|
|
1107
|
+
* @param {number} min
|
|
1108
|
+
* @param {ValidationOptions} [validationOptions]
|
|
1109
|
+
* @returns {PropertyDecorator}
|
|
1110
|
+
*/
|
|
1111
|
+
function Gte(min, validationOptions) {
|
|
1005
1112
|
return function (object, propertyName) {
|
|
1006
1113
|
setExpose(object, propertyName);
|
|
1007
1114
|
registerDecorator({
|
|
@@ -1028,7 +1135,7 @@ function Min(min, validationOptions) {
|
|
|
1028
1135
|
* @param {ValidationOptions} [validationOptions]
|
|
1029
1136
|
* @returns {PropertyDecorator}
|
|
1030
1137
|
*/
|
|
1031
|
-
function
|
|
1138
|
+
function Lte(max, validationOptions) {
|
|
1032
1139
|
return function (object, propertyName) {
|
|
1033
1140
|
setExpose(object, propertyName);
|
|
1034
1141
|
registerDecorator({
|
|
@@ -1216,4 +1323,4 @@ function IsHash(algorithm, validationOptions) {
|
|
|
1216
1323
|
};
|
|
1217
1324
|
}
|
|
1218
1325
|
|
|
1219
|
-
export { ClassValidator, Contains, ENABLE_VALIDATED, Equals, Expose, FunctionValidator, IsCnName, IsDate, IsDefined, IsEmail, IsHash, IsIP, IsIdNumber, IsIn, IsMobile, IsNotEmpty, IsNotIn, IsPhoneNumber, IsPlateNumber, IsUrl, IsZipCode, Length,
|
|
1326
|
+
export { ClassValidator, Contains, ENABLE_VALIDATED, Equals, Expose, FunctionValidator, Gt, Gte, IsCnName, IsDate, IsDefined, IsEmail, IsHash, IsIP, IsIdNumber, IsIn, IsMobile, IsNotEmpty, IsNotIn, IsPhoneNumber, IsPlateNumber, IsUrl, IsZipCode, Length, Lt, Lte, NotEquals, PARAM_CHECK_KEY, PARAM_RULE_KEY, PARAM_TYPE_KEY, Valid, Validated, checkParamsType, convertDtoParamsType, convertParamsType, paramterTypes, plainToClass };
|
package/dist/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
window.jest_html_reporters_callback__({"numFailedTestSuites":0,"numFailedTests":0,"numPassedTestSuites":1,"numPassedTests":
|
|
1
|
+
window.jest_html_reporters_callback__({"numFailedTestSuites":0,"numFailedTests":0,"numPassedTestSuites":1,"numPassedTests":2,"numPendingTestSuites":0,"numPendingTests":0,"numRuntimeErrorTestSuites":0,"numTodoTests":0,"numTotalTestSuites":1,"numTotalTests":2,"startTime":1645754279274,"success":false,"testResults":[{"leaks":false,"numFailingTests":0,"numPassingTests":2,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1645754280984,"runtime":1640,"slow":false,"start":1645754279344},"skipped":false,"testFilePath":"/Users/richen/Workspace/nodejs/koatty_validation/test/.test.ts","testResults":[{"ancestorTitles":["koatty_validation"],"duration":1,"failureDetails":[],"failureMessages":[],"fullName":"koatty_validation FunctionValidator","invocations":1,"location":null,"numPassingAsserts":0,"status":"passed","title":"FunctionValidator"},{"ancestorTitles":["koatty_validation"],"duration":2,"failureDetails":[],"failureMessages":[],"fullName":"koatty_validation ClassValidator","invocations":1,"location":null,"numPassingAsserts":0,"status":"passed","title":"ClassValidator"}],"failureMessage":null}],"wasInterrupted":false,"config":{"bail":0,"changedFilesWithAncestor":false,"collectCoverage":true,"collectCoverageFrom":[],"coverageDirectory":"coverage","coverageProvider":"babel","coverageReporters":["html","lcov","json","text","clover","text-summary"],"detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"expand":false,"findRelatedTests":false,"forceExit":false,"json":false,"lastCommit":false,"listTests":false,"logHeapUsage":false,"maxConcurrency":5,"maxWorkers":7,"noStackTrace":false,"nonFlagArgs":[],"notify":false,"notifyMode":"failure-change","onlyChanged":false,"onlyFailures":false,"passWithNoTests":true,"projects":[],"reporters":[["default",{}],["/Users/richen/Workspace/nodejs/koatty_validation/node_modules/jest-html-reporters/index.js",{}]],"rootDir":"/Users/richen/Workspace/nodejs/koatty_validation","runTestsByPath":false,"skipFilter":false,"testFailureExitCode":1,"testPathPattern":"","testSequencer":"/Users/richen/Workspace/nodejs/koatty_validation/node_modules/@jest/test-sequencer/build/index.js","updateSnapshot":"new","useStderr":false,"verbose":true,"watch":false,"watchAll":false,"watchman":true},"endTime":1645754281125,"_reporterOptions":{"publicPath":"/Users/richen/Workspace/nodejs/koatty_validation","filename":"jest_html_reporters.html","expand":false,"pageTitle":"","hideIcon":false,"testCommand":"npx jest","openReport":false,"failureMessageOnly":false,"enableMergeData":false,"dataMergeLevel":1},"attachInfos":{}})
|
package/package.json
CHANGED