koatty_validation 1.1.0 → 1.2.3
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 +6 -0
- package/README.md +34 -28
- package/dist/README.md +34 -28
- package/dist/index.d.ts +44 -24
- package/dist/index.js +207 -104
- package/dist/index.mjs +205 -103
- 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,12 @@
|
|
|
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.3](https://github.com/koatty/koatty_validation/compare/v1.2.2...v1.2.3) (2022-03-09)
|
|
6
|
+
|
|
7
|
+
### [1.2.2](https://github.com/koatty/koatty_validation/compare/v1.2.0...v1.2.2) (2022-02-25)
|
|
8
|
+
|
|
9
|
+
## [1.2.0](https://github.com/koatty/koatty_validation/compare/v1.1.0...v1.2.0) (2022-02-25)
|
|
10
|
+
|
|
5
11
|
## [1.1.0](https://github.com/koatty/koatty_validation/compare/v1.0.12...v1.1.0) (2022-02-16)
|
|
6
12
|
|
|
7
13
|
## [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-
|
|
3
|
+
* @Date: 2022-03-09 18:25:00
|
|
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-
|
|
3
|
+
* @Date: 2022-03-09 18:24:48
|
|
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-03-09 18:23:18
|
|
318
318
|
*/
|
|
319
319
|
// constant
|
|
320
320
|
const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
|
|
@@ -388,10 +388,7 @@ class ValidateClass {
|
|
|
388
388
|
errors = await classValidator.validate(obj, { skipMissingProperties: true });
|
|
389
389
|
}
|
|
390
390
|
if (errors.length > 0) {
|
|
391
|
-
|
|
392
|
-
err.code = 400;
|
|
393
|
-
err.status = 400;
|
|
394
|
-
throw err;
|
|
391
|
+
throw new Error(Object.values(errors[0].constraints)[0]);
|
|
395
392
|
}
|
|
396
393
|
return obj;
|
|
397
394
|
}
|
|
@@ -403,36 +400,13 @@ const ClassValidator = ValidateClass.getInstance();
|
|
|
403
400
|
/**
|
|
404
401
|
* Validator Functions
|
|
405
402
|
*/
|
|
406
|
-
const
|
|
407
|
-
/**
|
|
408
|
-
* Checks if value matches ("===") the comparison.
|
|
409
|
-
*/
|
|
410
|
-
Equals: (value, comparison) => {
|
|
411
|
-
return classValidator.equals(value, comparison);
|
|
412
|
-
},
|
|
403
|
+
const ValidFuncs = {
|
|
413
404
|
/**
|
|
414
|
-
* Checks
|
|
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
|
-
},
|
|
431
|
-
/**
|
|
432
|
-
* Checks if given value not in a array of allowed values.
|
|
405
|
+
* Checks value is not empty, undefined, null, '', NaN, [], {} and any empty string(including spaces,
|
|
406
|
+
* tabs, formfeeds, etc.), returns false
|
|
433
407
|
*/
|
|
434
|
-
|
|
435
|
-
return
|
|
408
|
+
IsNotEmpty: (value) => {
|
|
409
|
+
return !helper__namespace.isEmpty(value);
|
|
436
410
|
},
|
|
437
411
|
/**
|
|
438
412
|
* Checks if a given value is a real date.
|
|
@@ -440,25 +414,6 @@ const FunctionValidator = {
|
|
|
440
414
|
IsDate: (value) => {
|
|
441
415
|
return helper__namespace.isDate(value);
|
|
442
416
|
},
|
|
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
417
|
/**
|
|
463
418
|
* Checks if the string is an email. If given value is not a string, then it returns false.
|
|
464
419
|
*/
|
|
@@ -541,73 +496,162 @@ const FunctionValidator = {
|
|
|
541
496
|
return plateNumber(value);
|
|
542
497
|
},
|
|
543
498
|
/**
|
|
544
|
-
* Checks value
|
|
545
|
-
* tabs, formfeeds, etc.), returns false
|
|
499
|
+
* Checks if value matches ("===") the comparison.
|
|
546
500
|
*/
|
|
547
|
-
|
|
548
|
-
return
|
|
549
|
-
}
|
|
501
|
+
Equals: (value, comparison) => {
|
|
502
|
+
return classValidator.equals(value, comparison);
|
|
503
|
+
},
|
|
504
|
+
/**
|
|
505
|
+
* Checks if value does not match ("!==") the comparison.
|
|
506
|
+
*/
|
|
507
|
+
NotEquals: (value, comparison) => {
|
|
508
|
+
return classValidator.notEquals(value, comparison);
|
|
509
|
+
},
|
|
510
|
+
/**
|
|
511
|
+
* Checks if the string contains the seed. If given value is not a string, then it returns false.
|
|
512
|
+
*/
|
|
513
|
+
Contains: (value, seed) => {
|
|
514
|
+
return classValidator.contains(value, seed);
|
|
515
|
+
},
|
|
516
|
+
/**
|
|
517
|
+
* Checks if given value is in a array of allowed values.
|
|
518
|
+
*/
|
|
519
|
+
IsIn: (value, possibleValues) => {
|
|
520
|
+
return classValidator.isIn(value, possibleValues);
|
|
521
|
+
},
|
|
522
|
+
/**
|
|
523
|
+
* Checks if given value not in a array of allowed values.
|
|
524
|
+
*/
|
|
525
|
+
IsNotIn: (value, possibleValues) => {
|
|
526
|
+
return classValidator.isNotIn(value, possibleValues);
|
|
527
|
+
},
|
|
528
|
+
/**
|
|
529
|
+
* Checks if the first number is greater than or equal to the second.
|
|
530
|
+
*/
|
|
531
|
+
Gt: (num, min) => {
|
|
532
|
+
return helper__namespace.toNumber(num) > min;
|
|
533
|
+
},
|
|
534
|
+
/**
|
|
535
|
+
* Checks if the first number is less than or equal to the second.
|
|
536
|
+
*/
|
|
537
|
+
Lt: (num, max) => {
|
|
538
|
+
return helper__namespace.toNumber(num) < max;
|
|
539
|
+
},
|
|
540
|
+
/**
|
|
541
|
+
* Checks if the first number is greater than or equal to the second.
|
|
542
|
+
*/
|
|
543
|
+
Gte: (num, min) => {
|
|
544
|
+
return helper__namespace.toNumber(num) >= min;
|
|
545
|
+
},
|
|
546
|
+
/**
|
|
547
|
+
* Checks if the first number is less than or equal to the second.
|
|
548
|
+
*/
|
|
549
|
+
Lte: (num, max) => {
|
|
550
|
+
return helper__namespace.toNumber(num) <= max;
|
|
551
|
+
},
|
|
550
552
|
};
|
|
551
553
|
/**
|
|
552
554
|
* Use functions or built-in rules for validation.
|
|
553
555
|
*
|
|
554
556
|
* @export
|
|
555
|
-
* @param {
|
|
556
|
-
* @param {
|
|
557
|
-
* @param {string}
|
|
558
|
-
* @
|
|
559
|
-
* @param {string} [message]
|
|
560
|
-
* @param {boolean} [checkType=true]
|
|
561
|
-
* @returns
|
|
557
|
+
* @param {ValidRules} rule
|
|
558
|
+
* @param {unknown} value
|
|
559
|
+
* @param {(string | ValidOtpions)} [options]
|
|
560
|
+
* @returns {*}
|
|
562
561
|
*/
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
562
|
+
const FunctionValidator = {
|
|
563
|
+
IsNotEmpty: function (value, options) {
|
|
564
|
+
throw new Error("Function not implemented.");
|
|
565
|
+
},
|
|
566
|
+
IsDate: function (value, options) {
|
|
567
|
+
throw new Error("Function not implemented.");
|
|
568
|
+
},
|
|
569
|
+
IsEmail: function (value, options) {
|
|
570
|
+
throw new Error("Function not implemented.");
|
|
571
|
+
},
|
|
572
|
+
IsIP: function (value, options) {
|
|
573
|
+
throw new Error("Function not implemented.");
|
|
574
|
+
},
|
|
575
|
+
IsPhoneNumber: function (value, options) {
|
|
576
|
+
throw new Error("Function not implemented.");
|
|
577
|
+
},
|
|
578
|
+
IsUrl: function (value, options) {
|
|
579
|
+
throw new Error("Function not implemented.");
|
|
580
|
+
},
|
|
581
|
+
IsHash: function (value, options) {
|
|
582
|
+
throw new Error("Function not implemented.");
|
|
583
|
+
},
|
|
584
|
+
IsCnName: function (value, options) {
|
|
585
|
+
throw new Error("Function not implemented.");
|
|
586
|
+
},
|
|
587
|
+
IsIdNumber: function (value, options) {
|
|
588
|
+
throw new Error("Function not implemented.");
|
|
589
|
+
},
|
|
590
|
+
IsZipCode: function (value, options) {
|
|
591
|
+
throw new Error("Function not implemented.");
|
|
592
|
+
},
|
|
593
|
+
IsMobile: function (value, options) {
|
|
594
|
+
throw new Error("Function not implemented.");
|
|
595
|
+
},
|
|
596
|
+
IsPlateNumber: function (value, options) {
|
|
597
|
+
throw new Error("Function not implemented.");
|
|
598
|
+
},
|
|
599
|
+
Equals: function (value, options) {
|
|
600
|
+
throw new Error("Function not implemented.");
|
|
601
|
+
},
|
|
602
|
+
NotEquals: function (value, options) {
|
|
603
|
+
throw new Error("Function not implemented.");
|
|
604
|
+
},
|
|
605
|
+
Contains: function (value, options) {
|
|
606
|
+
throw new Error("Function not implemented.");
|
|
607
|
+
},
|
|
608
|
+
IsIn: function (value, options) {
|
|
609
|
+
throw new Error("Function not implemented.");
|
|
610
|
+
},
|
|
611
|
+
IsNotIn: function (value, options) {
|
|
612
|
+
throw new Error("Function not implemented.");
|
|
613
|
+
},
|
|
614
|
+
Gt: function (value, options) {
|
|
615
|
+
throw new Error("Function not implemented.");
|
|
616
|
+
},
|
|
617
|
+
Lt: function (value, options) {
|
|
618
|
+
throw new Error("Function not implemented.");
|
|
619
|
+
},
|
|
620
|
+
Gte: function (value, options) {
|
|
621
|
+
throw new Error("Function not implemented.");
|
|
622
|
+
},
|
|
623
|
+
Lte: function (value, options) {
|
|
624
|
+
throw new Error("Function not implemented.");
|
|
579
625
|
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
626
|
+
};
|
|
627
|
+
Object.keys(ValidFuncs).forEach((key) => {
|
|
628
|
+
FunctionValidator[key] = (value, options) => {
|
|
629
|
+
if (helper__namespace.isString(options)) {
|
|
630
|
+
options = { message: options, value: null };
|
|
584
631
|
}
|
|
585
|
-
if (
|
|
586
|
-
|
|
587
|
-
err.code = 400;
|
|
588
|
-
err.status = 400;
|
|
589
|
-
throw err;
|
|
632
|
+
if (!ValidFuncs[key](value, options.value)) {
|
|
633
|
+
throw new Error(options.message || `ValidatorError: invalid arguments.`);
|
|
590
634
|
}
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
}
|
|
635
|
+
};
|
|
636
|
+
});
|
|
594
637
|
|
|
595
638
|
/*
|
|
596
639
|
* @Description:
|
|
597
640
|
* @Usage:
|
|
598
641
|
* @Author: richen
|
|
599
642
|
* @Date: 2021-11-25 10:46:57
|
|
600
|
-
* @LastEditTime: 2022-02
|
|
643
|
+
* @LastEditTime: 2022-03-02 11:23:27
|
|
601
644
|
*/
|
|
602
645
|
/**
|
|
603
646
|
* Validation parameter's type and values.
|
|
604
647
|
*
|
|
605
648
|
* @export
|
|
606
649
|
* @param {(ValidRules | ValidRules[] | Function)} rule
|
|
607
|
-
* @param {
|
|
608
|
-
* @
|
|
650
|
+
* @param {*} [options] If the options type is a string, the value is the error message of the validation rule.
|
|
651
|
+
* Some validation rules require additional parameters, ext: @Valid("Gte", {message:"Requires value greater than or equal to 100", value: 100})
|
|
652
|
+
* @returns {*} {ParameterDecorator}
|
|
609
653
|
*/
|
|
610
|
-
function Valid(rule,
|
|
654
|
+
function Valid(rule, options) {
|
|
611
655
|
let rules = [];
|
|
612
656
|
if (helper__namespace.isString(rule)) {
|
|
613
657
|
rules = rule.split(",");
|
|
@@ -616,13 +660,17 @@ function Valid(rule, message) {
|
|
|
616
660
|
rules = rule;
|
|
617
661
|
}
|
|
618
662
|
return (target, propertyKey, descriptor) => {
|
|
663
|
+
var _a;
|
|
619
664
|
// 获取成员参数类型
|
|
620
665
|
const paramTypes = Reflect.getMetadata("design:paramtypes", target, propertyKey);
|
|
621
|
-
const type = (paramTypes[descriptor]
|
|
666
|
+
const type = ((_a = paramTypes[descriptor]) === null || _a === void 0 ? void 0 : _a.name) ? paramTypes[descriptor].name : 'object';
|
|
667
|
+
if (helper__namespace.isString(options)) {
|
|
668
|
+
options = { message: options, value: null };
|
|
669
|
+
}
|
|
622
670
|
koatty_container.IOCContainer.attachPropertyData(PARAM_RULE_KEY, {
|
|
623
671
|
name: propertyKey,
|
|
624
672
|
rule: rules,
|
|
625
|
-
|
|
673
|
+
options,
|
|
626
674
|
index: descriptor,
|
|
627
675
|
type
|
|
628
676
|
}, target, propertyKey);
|
|
@@ -1025,7 +1073,61 @@ function IsDate(validationOptions) {
|
|
|
1025
1073
|
* @param {ValidationOptions} [validationOptions]
|
|
1026
1074
|
* @returns {PropertyDecorator}
|
|
1027
1075
|
*/
|
|
1028
|
-
function
|
|
1076
|
+
function Gt(min, validationOptions) {
|
|
1077
|
+
return function (object, propertyName) {
|
|
1078
|
+
setExpose(object, propertyName);
|
|
1079
|
+
classValidator.registerDecorator({
|
|
1080
|
+
name: "vMin",
|
|
1081
|
+
target: object.constructor,
|
|
1082
|
+
propertyName,
|
|
1083
|
+
options: validationOptions,
|
|
1084
|
+
validator: {
|
|
1085
|
+
validate(value, args) {
|
|
1086
|
+
return helper__namespace.toNumber(value) > min;
|
|
1087
|
+
},
|
|
1088
|
+
defaultMessage(args) {
|
|
1089
|
+
return `invalid parameter ($property).`;
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
});
|
|
1093
|
+
};
|
|
1094
|
+
}
|
|
1095
|
+
/**
|
|
1096
|
+
* Checks if the first number is less than or equal to the max value.
|
|
1097
|
+
*
|
|
1098
|
+
* @export
|
|
1099
|
+
* @param {number} max
|
|
1100
|
+
* @param {ValidationOptions} [validationOptions]
|
|
1101
|
+
* @returns {PropertyDecorator}
|
|
1102
|
+
*/
|
|
1103
|
+
function Lt(max, validationOptions) {
|
|
1104
|
+
return function (object, propertyName) {
|
|
1105
|
+
setExpose(object, propertyName);
|
|
1106
|
+
classValidator.registerDecorator({
|
|
1107
|
+
name: "vMax",
|
|
1108
|
+
target: object.constructor,
|
|
1109
|
+
propertyName,
|
|
1110
|
+
options: validationOptions,
|
|
1111
|
+
validator: {
|
|
1112
|
+
validate(value, args) {
|
|
1113
|
+
return helper__namespace.toNumber(value) < max;
|
|
1114
|
+
},
|
|
1115
|
+
defaultMessage(args) {
|
|
1116
|
+
return `invalid parameter ($property).`;
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
});
|
|
1120
|
+
};
|
|
1121
|
+
}
|
|
1122
|
+
/**
|
|
1123
|
+
* Checks if the first number is greater than or equal to the min value.
|
|
1124
|
+
*
|
|
1125
|
+
* @export
|
|
1126
|
+
* @param {number} min
|
|
1127
|
+
* @param {ValidationOptions} [validationOptions]
|
|
1128
|
+
* @returns {PropertyDecorator}
|
|
1129
|
+
*/
|
|
1130
|
+
function Gte(min, validationOptions) {
|
|
1029
1131
|
return function (object, propertyName) {
|
|
1030
1132
|
setExpose(object, propertyName);
|
|
1031
1133
|
classValidator.registerDecorator({
|
|
@@ -1052,7 +1154,7 @@ function Min(min, validationOptions) {
|
|
|
1052
1154
|
* @param {ValidationOptions} [validationOptions]
|
|
1053
1155
|
* @returns {PropertyDecorator}
|
|
1054
1156
|
*/
|
|
1055
|
-
function
|
|
1157
|
+
function Lte(max, validationOptions) {
|
|
1056
1158
|
return function (object, propertyName) {
|
|
1057
1159
|
setExpose(object, propertyName);
|
|
1058
1160
|
classValidator.registerDecorator({
|
|
@@ -1246,6 +1348,8 @@ exports.ENABLE_VALIDATED = ENABLE_VALIDATED;
|
|
|
1246
1348
|
exports.Equals = Equals;
|
|
1247
1349
|
exports.Expose = Expose;
|
|
1248
1350
|
exports.FunctionValidator = FunctionValidator;
|
|
1351
|
+
exports.Gt = Gt;
|
|
1352
|
+
exports.Gte = Gte;
|
|
1249
1353
|
exports.IsCnName = IsCnName;
|
|
1250
1354
|
exports.IsDate = IsDate;
|
|
1251
1355
|
exports.IsDefined = IsDefined;
|
|
@@ -1262,15 +1366,14 @@ exports.IsPlateNumber = IsPlateNumber;
|
|
|
1262
1366
|
exports.IsUrl = IsUrl;
|
|
1263
1367
|
exports.IsZipCode = IsZipCode;
|
|
1264
1368
|
exports.Length = Length;
|
|
1265
|
-
exports.
|
|
1266
|
-
exports.
|
|
1369
|
+
exports.Lt = Lt;
|
|
1370
|
+
exports.Lte = Lte;
|
|
1267
1371
|
exports.NotEquals = NotEquals;
|
|
1268
1372
|
exports.PARAM_CHECK_KEY = PARAM_CHECK_KEY;
|
|
1269
1373
|
exports.PARAM_RULE_KEY = PARAM_RULE_KEY;
|
|
1270
1374
|
exports.PARAM_TYPE_KEY = PARAM_TYPE_KEY;
|
|
1271
1375
|
exports.Valid = Valid;
|
|
1272
1376
|
exports.Validated = Validated;
|
|
1273
|
-
exports.ValidatorFuncs = ValidatorFuncs;
|
|
1274
1377
|
exports.checkParamsType = checkParamsType;
|
|
1275
1378
|
exports.convertDtoParamsType = convertDtoParamsType;
|
|
1276
1379
|
exports.convertParamsType = convertParamsType;
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @Author: richen
|
|
3
|
-
* @Date: 2022-
|
|
3
|
+
* @Date: 2022-03-09 18:24:48
|
|
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-03-09 18:23:18
|
|
294
294
|
*/
|
|
295
295
|
// constant
|
|
296
296
|
const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
|
|
@@ -364,10 +364,7 @@ class ValidateClass {
|
|
|
364
364
|
errors = await validate(obj, { skipMissingProperties: true });
|
|
365
365
|
}
|
|
366
366
|
if (errors.length > 0) {
|
|
367
|
-
|
|
368
|
-
err.code = 400;
|
|
369
|
-
err.status = 400;
|
|
370
|
-
throw err;
|
|
367
|
+
throw new Error(Object.values(errors[0].constraints)[0]);
|
|
371
368
|
}
|
|
372
369
|
return obj;
|
|
373
370
|
}
|
|
@@ -379,36 +376,13 @@ const ClassValidator = ValidateClass.getInstance();
|
|
|
379
376
|
/**
|
|
380
377
|
* Validator Functions
|
|
381
378
|
*/
|
|
382
|
-
const
|
|
383
|
-
/**
|
|
384
|
-
* Checks if value matches ("===") the comparison.
|
|
385
|
-
*/
|
|
386
|
-
Equals: (value, comparison) => {
|
|
387
|
-
return equals(value, comparison);
|
|
388
|
-
},
|
|
379
|
+
const ValidFuncs = {
|
|
389
380
|
/**
|
|
390
|
-
* Checks
|
|
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
|
-
},
|
|
407
|
-
/**
|
|
408
|
-
* Checks if given value not in a array of allowed values.
|
|
381
|
+
* Checks value is not empty, undefined, null, '', NaN, [], {} and any empty string(including spaces,
|
|
382
|
+
* tabs, formfeeds, etc.), returns false
|
|
409
383
|
*/
|
|
410
|
-
|
|
411
|
-
return
|
|
384
|
+
IsNotEmpty: (value) => {
|
|
385
|
+
return !helper.isEmpty(value);
|
|
412
386
|
},
|
|
413
387
|
/**
|
|
414
388
|
* Checks if a given value is a real date.
|
|
@@ -416,25 +390,6 @@ const FunctionValidator = {
|
|
|
416
390
|
IsDate: (value) => {
|
|
417
391
|
return helper.isDate(value);
|
|
418
392
|
},
|
|
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
393
|
/**
|
|
439
394
|
* Checks if the string is an email. If given value is not a string, then it returns false.
|
|
440
395
|
*/
|
|
@@ -517,73 +472,162 @@ const FunctionValidator = {
|
|
|
517
472
|
return plateNumber(value);
|
|
518
473
|
},
|
|
519
474
|
/**
|
|
520
|
-
* Checks value
|
|
521
|
-
* tabs, formfeeds, etc.), returns false
|
|
475
|
+
* Checks if value matches ("===") the comparison.
|
|
522
476
|
*/
|
|
523
|
-
|
|
524
|
-
return
|
|
525
|
-
}
|
|
477
|
+
Equals: (value, comparison) => {
|
|
478
|
+
return equals(value, comparison);
|
|
479
|
+
},
|
|
480
|
+
/**
|
|
481
|
+
* Checks if value does not match ("!==") the comparison.
|
|
482
|
+
*/
|
|
483
|
+
NotEquals: (value, comparison) => {
|
|
484
|
+
return notEquals(value, comparison);
|
|
485
|
+
},
|
|
486
|
+
/**
|
|
487
|
+
* Checks if the string contains the seed. If given value is not a string, then it returns false.
|
|
488
|
+
*/
|
|
489
|
+
Contains: (value, seed) => {
|
|
490
|
+
return contains(value, seed);
|
|
491
|
+
},
|
|
492
|
+
/**
|
|
493
|
+
* Checks if given value is in a array of allowed values.
|
|
494
|
+
*/
|
|
495
|
+
IsIn: (value, possibleValues) => {
|
|
496
|
+
return isIn(value, possibleValues);
|
|
497
|
+
},
|
|
498
|
+
/**
|
|
499
|
+
* Checks if given value not in a array of allowed values.
|
|
500
|
+
*/
|
|
501
|
+
IsNotIn: (value, possibleValues) => {
|
|
502
|
+
return isNotIn(value, possibleValues);
|
|
503
|
+
},
|
|
504
|
+
/**
|
|
505
|
+
* Checks if the first number is greater than or equal to the second.
|
|
506
|
+
*/
|
|
507
|
+
Gt: (num, min) => {
|
|
508
|
+
return helper.toNumber(num) > min;
|
|
509
|
+
},
|
|
510
|
+
/**
|
|
511
|
+
* Checks if the first number is less than or equal to the second.
|
|
512
|
+
*/
|
|
513
|
+
Lt: (num, max) => {
|
|
514
|
+
return helper.toNumber(num) < max;
|
|
515
|
+
},
|
|
516
|
+
/**
|
|
517
|
+
* Checks if the first number is greater than or equal to the second.
|
|
518
|
+
*/
|
|
519
|
+
Gte: (num, min) => {
|
|
520
|
+
return helper.toNumber(num) >= min;
|
|
521
|
+
},
|
|
522
|
+
/**
|
|
523
|
+
* Checks if the first number is less than or equal to the second.
|
|
524
|
+
*/
|
|
525
|
+
Lte: (num, max) => {
|
|
526
|
+
return helper.toNumber(num) <= max;
|
|
527
|
+
},
|
|
526
528
|
};
|
|
527
529
|
/**
|
|
528
530
|
* Use functions or built-in rules for validation.
|
|
529
531
|
*
|
|
530
532
|
* @export
|
|
531
|
-
* @param {
|
|
532
|
-
* @param {
|
|
533
|
-
* @param {string}
|
|
534
|
-
* @
|
|
535
|
-
* @param {string} [message]
|
|
536
|
-
* @param {boolean} [checkType=true]
|
|
537
|
-
* @returns
|
|
533
|
+
* @param {ValidRules} rule
|
|
534
|
+
* @param {unknown} value
|
|
535
|
+
* @param {(string | ValidOtpions)} [options]
|
|
536
|
+
* @returns {*}
|
|
538
537
|
*/
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
538
|
+
const FunctionValidator = {
|
|
539
|
+
IsNotEmpty: function (value, options) {
|
|
540
|
+
throw new Error("Function not implemented.");
|
|
541
|
+
},
|
|
542
|
+
IsDate: function (value, options) {
|
|
543
|
+
throw new Error("Function not implemented.");
|
|
544
|
+
},
|
|
545
|
+
IsEmail: function (value, options) {
|
|
546
|
+
throw new Error("Function not implemented.");
|
|
547
|
+
},
|
|
548
|
+
IsIP: function (value, options) {
|
|
549
|
+
throw new Error("Function not implemented.");
|
|
550
|
+
},
|
|
551
|
+
IsPhoneNumber: function (value, options) {
|
|
552
|
+
throw new Error("Function not implemented.");
|
|
553
|
+
},
|
|
554
|
+
IsUrl: function (value, options) {
|
|
555
|
+
throw new Error("Function not implemented.");
|
|
556
|
+
},
|
|
557
|
+
IsHash: function (value, options) {
|
|
558
|
+
throw new Error("Function not implemented.");
|
|
559
|
+
},
|
|
560
|
+
IsCnName: function (value, options) {
|
|
561
|
+
throw new Error("Function not implemented.");
|
|
562
|
+
},
|
|
563
|
+
IsIdNumber: function (value, options) {
|
|
564
|
+
throw new Error("Function not implemented.");
|
|
565
|
+
},
|
|
566
|
+
IsZipCode: function (value, options) {
|
|
567
|
+
throw new Error("Function not implemented.");
|
|
568
|
+
},
|
|
569
|
+
IsMobile: function (value, options) {
|
|
570
|
+
throw new Error("Function not implemented.");
|
|
571
|
+
},
|
|
572
|
+
IsPlateNumber: function (value, options) {
|
|
573
|
+
throw new Error("Function not implemented.");
|
|
574
|
+
},
|
|
575
|
+
Equals: function (value, options) {
|
|
576
|
+
throw new Error("Function not implemented.");
|
|
577
|
+
},
|
|
578
|
+
NotEquals: function (value, options) {
|
|
579
|
+
throw new Error("Function not implemented.");
|
|
580
|
+
},
|
|
581
|
+
Contains: function (value, options) {
|
|
582
|
+
throw new Error("Function not implemented.");
|
|
583
|
+
},
|
|
584
|
+
IsIn: function (value, options) {
|
|
585
|
+
throw new Error("Function not implemented.");
|
|
586
|
+
},
|
|
587
|
+
IsNotIn: function (value, options) {
|
|
588
|
+
throw new Error("Function not implemented.");
|
|
589
|
+
},
|
|
590
|
+
Gt: function (value, options) {
|
|
591
|
+
throw new Error("Function not implemented.");
|
|
592
|
+
},
|
|
593
|
+
Lt: function (value, options) {
|
|
594
|
+
throw new Error("Function not implemented.");
|
|
595
|
+
},
|
|
596
|
+
Gte: function (value, options) {
|
|
597
|
+
throw new Error("Function not implemented.");
|
|
598
|
+
},
|
|
599
|
+
Lte: function (value, options) {
|
|
600
|
+
throw new Error("Function not implemented.");
|
|
555
601
|
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
602
|
+
};
|
|
603
|
+
Object.keys(ValidFuncs).forEach((key) => {
|
|
604
|
+
FunctionValidator[key] = (value, options) => {
|
|
605
|
+
if (helper.isString(options)) {
|
|
606
|
+
options = { message: options, value: null };
|
|
560
607
|
}
|
|
561
|
-
if (
|
|
562
|
-
|
|
563
|
-
err.code = 400;
|
|
564
|
-
err.status = 400;
|
|
565
|
-
throw err;
|
|
608
|
+
if (!ValidFuncs[key](value, options.value)) {
|
|
609
|
+
throw new Error(options.message || `ValidatorError: invalid arguments.`);
|
|
566
610
|
}
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
}
|
|
611
|
+
};
|
|
612
|
+
});
|
|
570
613
|
|
|
571
614
|
/*
|
|
572
615
|
* @Description:
|
|
573
616
|
* @Usage:
|
|
574
617
|
* @Author: richen
|
|
575
618
|
* @Date: 2021-11-25 10:46:57
|
|
576
|
-
* @LastEditTime: 2022-02
|
|
619
|
+
* @LastEditTime: 2022-03-02 11:23:27
|
|
577
620
|
*/
|
|
578
621
|
/**
|
|
579
622
|
* Validation parameter's type and values.
|
|
580
623
|
*
|
|
581
624
|
* @export
|
|
582
625
|
* @param {(ValidRules | ValidRules[] | Function)} rule
|
|
583
|
-
* @param {
|
|
584
|
-
* @
|
|
626
|
+
* @param {*} [options] If the options type is a string, the value is the error message of the validation rule.
|
|
627
|
+
* Some validation rules require additional parameters, ext: @Valid("Gte", {message:"Requires value greater than or equal to 100", value: 100})
|
|
628
|
+
* @returns {*} {ParameterDecorator}
|
|
585
629
|
*/
|
|
586
|
-
function Valid(rule,
|
|
630
|
+
function Valid(rule, options) {
|
|
587
631
|
let rules = [];
|
|
588
632
|
if (helper.isString(rule)) {
|
|
589
633
|
rules = rule.split(",");
|
|
@@ -592,13 +636,17 @@ function Valid(rule, message) {
|
|
|
592
636
|
rules = rule;
|
|
593
637
|
}
|
|
594
638
|
return (target, propertyKey, descriptor) => {
|
|
639
|
+
var _a;
|
|
595
640
|
// 获取成员参数类型
|
|
596
641
|
const paramTypes = Reflect.getMetadata("design:paramtypes", target, propertyKey);
|
|
597
|
-
const type = (paramTypes[descriptor]
|
|
642
|
+
const type = ((_a = paramTypes[descriptor]) === null || _a === void 0 ? void 0 : _a.name) ? paramTypes[descriptor].name : 'object';
|
|
643
|
+
if (helper.isString(options)) {
|
|
644
|
+
options = { message: options, value: null };
|
|
645
|
+
}
|
|
598
646
|
IOCContainer.attachPropertyData(PARAM_RULE_KEY, {
|
|
599
647
|
name: propertyKey,
|
|
600
648
|
rule: rules,
|
|
601
|
-
|
|
649
|
+
options,
|
|
602
650
|
index: descriptor,
|
|
603
651
|
type
|
|
604
652
|
}, target, propertyKey);
|
|
@@ -1001,7 +1049,61 @@ function IsDate(validationOptions) {
|
|
|
1001
1049
|
* @param {ValidationOptions} [validationOptions]
|
|
1002
1050
|
* @returns {PropertyDecorator}
|
|
1003
1051
|
*/
|
|
1004
|
-
function
|
|
1052
|
+
function Gt(min, validationOptions) {
|
|
1053
|
+
return function (object, propertyName) {
|
|
1054
|
+
setExpose(object, propertyName);
|
|
1055
|
+
registerDecorator({
|
|
1056
|
+
name: "vMin",
|
|
1057
|
+
target: object.constructor,
|
|
1058
|
+
propertyName,
|
|
1059
|
+
options: validationOptions,
|
|
1060
|
+
validator: {
|
|
1061
|
+
validate(value, args) {
|
|
1062
|
+
return helper.toNumber(value) > min;
|
|
1063
|
+
},
|
|
1064
|
+
defaultMessage(args) {
|
|
1065
|
+
return `invalid parameter ($property).`;
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
});
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
/**
|
|
1072
|
+
* Checks if the first number is less than or equal to the max value.
|
|
1073
|
+
*
|
|
1074
|
+
* @export
|
|
1075
|
+
* @param {number} max
|
|
1076
|
+
* @param {ValidationOptions} [validationOptions]
|
|
1077
|
+
* @returns {PropertyDecorator}
|
|
1078
|
+
*/
|
|
1079
|
+
function Lt(max, validationOptions) {
|
|
1080
|
+
return function (object, propertyName) {
|
|
1081
|
+
setExpose(object, propertyName);
|
|
1082
|
+
registerDecorator({
|
|
1083
|
+
name: "vMax",
|
|
1084
|
+
target: object.constructor,
|
|
1085
|
+
propertyName,
|
|
1086
|
+
options: validationOptions,
|
|
1087
|
+
validator: {
|
|
1088
|
+
validate(value, args) {
|
|
1089
|
+
return helper.toNumber(value) < max;
|
|
1090
|
+
},
|
|
1091
|
+
defaultMessage(args) {
|
|
1092
|
+
return `invalid parameter ($property).`;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
});
|
|
1096
|
+
};
|
|
1097
|
+
}
|
|
1098
|
+
/**
|
|
1099
|
+
* Checks if the first number is greater than or equal to the min value.
|
|
1100
|
+
*
|
|
1101
|
+
* @export
|
|
1102
|
+
* @param {number} min
|
|
1103
|
+
* @param {ValidationOptions} [validationOptions]
|
|
1104
|
+
* @returns {PropertyDecorator}
|
|
1105
|
+
*/
|
|
1106
|
+
function Gte(min, validationOptions) {
|
|
1005
1107
|
return function (object, propertyName) {
|
|
1006
1108
|
setExpose(object, propertyName);
|
|
1007
1109
|
registerDecorator({
|
|
@@ -1028,7 +1130,7 @@ function Min(min, validationOptions) {
|
|
|
1028
1130
|
* @param {ValidationOptions} [validationOptions]
|
|
1029
1131
|
* @returns {PropertyDecorator}
|
|
1030
1132
|
*/
|
|
1031
|
-
function
|
|
1133
|
+
function Lte(max, validationOptions) {
|
|
1032
1134
|
return function (object, propertyName) {
|
|
1033
1135
|
setExpose(object, propertyName);
|
|
1034
1136
|
registerDecorator({
|
|
@@ -1216,4 +1318,4 @@ function IsHash(algorithm, validationOptions) {
|
|
|
1216
1318
|
};
|
|
1217
1319
|
}
|
|
1218
1320
|
|
|
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,
|
|
1321
|
+
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":1646821485222,"success":false,"testResults":[{"leaks":false,"numFailingTests":0,"numPassingTests":2,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1646821486984,"runtime":1684,"slow":false,"start":1646821485300},"skipped":false,"testFilePath":"/Users/richen/Workspace/nodejs/koatty_validation/test/.test.ts","testResults":[{"ancestorTitles":["koatty_validation"],"duration":20,"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":1646821487107,"_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