koatty_validation 1.6.0 → 1.6.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/.turbo/turbo-build.log +120 -0
- package/.turbo/turbo-test.log +8 -0
- package/CHANGELOG.md +31 -26
- package/README.md +106 -3
- package/dist/README.md +106 -3
- package/dist/index.d.ts +94 -60
- package/dist/index.js +337 -284
- package/dist/index.mjs +338 -286
- package/dist/package.json +10 -14
- package/examples/validated-async-sync-example.ts +213 -0
- package/package.json +21 -26
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
|
|
2
|
+
> koatty_validation@1.6.3 build /Users/richen/Workspace/nodejs/koatty-monorepo/packages/koatty-validation
|
|
3
|
+
> npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
> koatty_validation@1.6.3 build:js
|
|
7
|
+
> del-cli --force dist && npx rollup --bundleConfigAsCjs -c .rollup.config.js
|
|
8
|
+
|
|
9
|
+
[36m
|
|
10
|
+
[1m./src/index.ts[22m → [1m./dist/index.js[22m...[39m
|
|
11
|
+
[1m[33m(!) Unresolved dependencies[39m[22m
|
|
12
|
+
[90mhttps://rollupjs.org/troubleshooting/#warning-treating-module-as-external-dependency[39m
|
|
13
|
+
[1mlru-cache[22m (imported by "src/performance-cache.ts")
|
|
14
|
+
[1mreflect-metadata[22m (imported by "src/util.ts")
|
|
15
|
+
[1mkoatty_lib[22m (imported by "src/util.ts", "src/rule.ts" and "src/decorators.ts")
|
|
16
|
+
[1mkoatty_container[22m (imported by "src/util.ts" and "src/decorators.ts")
|
|
17
|
+
[1mclass-validator[22m (imported by "src/rule.ts", "src/decorator-factory.ts" and "src/decorators.ts")
|
|
18
|
+
[32mcreated [1m./dist/index.js[22m in [1m2s[22m[39m
|
|
19
|
+
[36m
|
|
20
|
+
[1m./src/index.ts[22m → [1m./dist/index.mjs[22m...[39m
|
|
21
|
+
[1m[33m(!) Unresolved dependencies[39m[22m
|
|
22
|
+
[90mhttps://rollupjs.org/troubleshooting/#warning-treating-module-as-external-dependency[39m
|
|
23
|
+
[1mlru-cache[22m (imported by "src/performance-cache.ts")
|
|
24
|
+
[1mreflect-metadata[22m (imported by "src/util.ts")
|
|
25
|
+
[1mkoatty_lib[22m (imported by "src/util.ts", "src/rule.ts" and "src/decorators.ts")
|
|
26
|
+
[1mkoatty_container[22m (imported by "src/util.ts" and "src/decorators.ts")
|
|
27
|
+
[1mclass-validator[22m (imported by "src/rule.ts", "src/decorator-factory.ts" and "src/decorators.ts")
|
|
28
|
+
[32mcreated [1m./dist/index.mjs[22m in [1m1.1s[22m[39m
|
|
29
|
+
|
|
30
|
+
> koatty_validation@1.6.3 build:dts
|
|
31
|
+
> del-cli --force temp && npx tsc && npx api-extractor run --local --verbose
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
[1mapi-extractor 7.55.2 [36m - https://api-extractor.com/[39m
|
|
35
|
+
[22m
|
|
36
|
+
Using configuration from ./api-extractor.json
|
|
37
|
+
Analysis will use the bundled TypeScript version 5.8.2
|
|
38
|
+
*** The target project appears to use TypeScript 5.9.3 which is newer than the bundled compiler engine; consider upgrading API Extractor.
|
|
39
|
+
[36mWriting: /Users/richen/Workspace/nodejs/koatty-monorepo/packages/koatty-validation/temp/koatty_validation.api.json[39m
|
|
40
|
+
[36mWriting package typings: /Users/richen/Workspace/nodejs/koatty-monorepo/packages/koatty-validation/dist/index.d.ts[39m
|
|
41
|
+
[33mWarning: src/error-handler.ts:185:6 - (tsdoc-undefined-tag) The TSDoc tag "@private" is not defined in this configuration[39m
|
|
42
|
+
[33mWarning: src/performance-cache.ts:410:14 - (ae-forgotten-export) The symbol "MetadataCache" needs to be exported by the entry point index.d.ts[39m
|
|
43
|
+
[33mWarning: src/performance-cache.ts:411:14 - (ae-forgotten-export) The symbol "ValidationCache" needs to be exported by the entry point index.d.ts[39m
|
|
44
|
+
[33mWarning: src/performance-cache.ts:412:14 - (ae-forgotten-export) The symbol "RegexCache" needs to be exported by the entry point index.d.ts[39m
|
|
45
|
+
[33mWarning: src/performance-cache.ts:413:14 - (ae-forgotten-export) The symbol "PerformanceMonitor" needs to be exported by the entry point index.d.ts[39m
|
|
46
|
+
[33mWarning: src/rule.ts:28:4 - (tsdoc-undefined-tag) The TSDoc tag "@export" is not defined in this configuration[39m
|
|
47
|
+
[33mWarning: src/rule.ts:29:17 - (tsdoc-escape-right-brace) The "}" character should be escaped using a backslash to avoid confusion with a TSDoc inline tag[39m
|
|
48
|
+
[33mWarning: src/rule.ts:29:10 - (tsdoc-malformed-inline-tag) Expecting a TSDoc tag starting with "{@"[39m
|
|
49
|
+
[33mWarning: src/rule.ts:29:4 - (tsdoc-undefined-tag) The TSDoc tag "@enum" is not defined in this configuration[39m
|
|
50
|
+
[33mWarning: src/rule.ts:54:6 - (tsdoc-undefined-tag) The TSDoc tag "@static" is not defined in this configuration[39m
|
|
51
|
+
[33mWarning: src/rule.ts:56:6 - (tsdoc-undefined-tag) The TSDoc tag "@memberof" is not defined in this configuration[39m
|
|
52
|
+
[33mWarning: src/rule.ts:65:13 - (tsdoc-param-tag-with-invalid-type) The @param block should not include a JSDoc-style '{type}'[39m
|
|
53
|
+
[33mWarning: src/rule.ts:66:13 - (tsdoc-param-tag-with-invalid-type) The @param block should not include a JSDoc-style '{type}'[39m
|
|
54
|
+
[33mWarning: src/rule.ts:67:23 - (tsdoc-param-tag-with-invalid-optional-name) The @param should not include a JSDoc-style optional name; it must not be enclosed in '[ ]' brackets.[39m
|
|
55
|
+
[33mWarning: src/rule.ts:67:13 - (tsdoc-param-tag-with-invalid-type) The @param block should not include a JSDoc-style '{type}'[39m
|
|
56
|
+
[33mWarning: src/rule.ts:68:33 - (tsdoc-param-tag-with-invalid-optional-name) The @param should not include a JSDoc-style optional name; it must not be enclosed in '[ ]' brackets.[39m
|
|
57
|
+
[33mWarning: src/rule.ts:68:13 - (tsdoc-param-tag-with-invalid-type) The @param block should not include a JSDoc-style '{type}'[39m
|
|
58
|
+
[33mWarning: src/rule.ts:69:28 - (tsdoc-escape-right-brace) The "}" character should be escaped using a backslash to avoid confusion with a TSDoc inline tag[39m
|
|
59
|
+
[33mWarning: src/rule.ts:69:15 - (tsdoc-malformed-inline-tag) Expecting a TSDoc tag starting with "{@"[39m
|
|
60
|
+
[33mWarning: src/rule.ts:70:6 - (tsdoc-undefined-tag) The TSDoc tag "@memberof" is not defined in this configuration[39m
|
|
61
|
+
[33mWarning: src/rule.ts:111:14 - (ae-forgotten-export) The symbol "ValidateClass" needs to be exported by the entry point index.d.ts[39m
|
|
62
|
+
[33mWarning: src/rule.ts:116:4 - (tsdoc-undefined-tag) The TSDoc tag "@export" is not defined in this configuration[39m
|
|
63
|
+
[33mWarning: src/rule.ts:117:17 - (tsdoc-escape-right-brace) The "}" character should be escaped using a backslash to avoid confusion with a TSDoc inline tag[39m
|
|
64
|
+
[33mWarning: src/rule.ts:117:10 - (tsdoc-malformed-inline-tag) Expecting a TSDoc tag starting with "{@"[39m
|
|
65
|
+
[33mWarning: src/rule.ts:117:4 - (tsdoc-undefined-tag) The TSDoc tag "@type" is not defined in this configuration[39m
|
|
66
|
+
[33mWarning: src/rule.ts:152:64 - (tsdoc-escape-right-brace) The "}" character should be escaped using a backslash to avoid confusion with a TSDoc inline tag[39m
|
|
67
|
+
[33mWarning: src/rule.ts:152:63 - (tsdoc-malformed-inline-tag) Expecting a TSDoc tag starting with "{@"[39m
|
|
68
|
+
[33mWarning: src/rule.ts:339:4 - (tsdoc-undefined-tag) The TSDoc tag "@export" is not defined in this configuration[39m
|
|
69
|
+
[33mWarning: src/types.ts:58:6 - (tsdoc-undefined-tag) The TSDoc tag "@default" is not defined in this configuration[39m
|
|
70
|
+
[33mWarning: src/types.ts:64:6 - (tsdoc-undefined-tag) The TSDoc tag "@default" is not defined in this configuration[39m
|
|
71
|
+
[33mWarning: src/util.ts:144:4 - (tsdoc-undefined-tag) The TSDoc tag "@export" is not defined in this configuration[39m
|
|
72
|
+
[33mWarning: src/util.ts:145:11 - (tsdoc-param-tag-with-invalid-type) The @param block should not include a JSDoc-style '{type}'[39m
|
|
73
|
+
[33mWarning: src/util.ts:146:11 - (tsdoc-param-tag-with-invalid-type) The @param block should not include a JSDoc-style '{type}'[39m
|
|
74
|
+
[33mWarning: src/util.ts:147:21 - (tsdoc-param-tag-with-invalid-optional-name) The @param should not include a JSDoc-style optional name; it must not be enclosed in '[ ]' brackets.[39m
|
|
75
|
+
[33mWarning: src/util.ts:147:11 - (tsdoc-param-tag-with-invalid-type) The @param block should not include a JSDoc-style '{type}'[39m
|
|
76
|
+
[33mWarning: src/util.ts:226:11 - (tsdoc-param-tag-with-invalid-type) The @param block should not include a JSDoc-style '{type}'[39m
|
|
77
|
+
[33mWarning: src/util.ts:227:11 - (tsdoc-param-tag-with-invalid-type) The @param block should not include a JSDoc-style '{type}'[39m
|
|
78
|
+
[33mWarning: src/util.ts:228:15 - (tsdoc-escape-right-brace) The "}" character should be escaped using a backslash to avoid confusion with a TSDoc inline tag[39m
|
|
79
|
+
[33mWarning: src/util.ts:228:13 - (tsdoc-malformed-inline-tag) Expecting a TSDoc tag starting with "{@"[39m
|
|
80
|
+
[33mWarning: src/util.ts:252:11 - (tsdoc-param-tag-with-invalid-type) The @param block should not include a JSDoc-style '{type}'[39m
|
|
81
|
+
[33mWarning: src/util.ts:253:11 - (tsdoc-param-tag-with-invalid-type) The @param block should not include a JSDoc-style '{type}'[39m
|
|
82
|
+
[33mWarning: src/util.ts:254:15 - (tsdoc-escape-right-brace) The "}" character should be escaped using a backslash to avoid confusion with a TSDoc inline tag[39m
|
|
83
|
+
[33mWarning: src/util.ts:254:13 - (tsdoc-malformed-inline-tag) Expecting a TSDoc tag starting with "{@"[39m
|
|
84
|
+
[33mWarning: src/util.ts:323:11 - (tsdoc-param-tag-with-invalid-type) The @param block should not include a JSDoc-style '{type}'[39m
|
|
85
|
+
[33mWarning: src/util.ts:324:11 - (tsdoc-param-tag-with-invalid-type) The @param block should not include a JSDoc-style '{type}'[39m
|
|
86
|
+
[33mWarning: src/util.ts:325:15 - (tsdoc-escape-right-brace) The "}" character should be escaped using a backslash to avoid confusion with a TSDoc inline tag[39m
|
|
87
|
+
[33mWarning: src/util.ts:325:13 - (tsdoc-malformed-inline-tag) Expecting a TSDoc tag starting with "{@"[39m
|
|
88
|
+
|
|
89
|
+
API Extractor completed successfully
|
|
90
|
+
|
|
91
|
+
> koatty_validation@1.6.3 build:doc
|
|
92
|
+
> del-cli --force docs/api && npx api-documenter markdown --input temp --output docs/api
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
[1mapi-documenter 7.28.2 [36m - https://api-extractor.com/[39m
|
|
96
|
+
[22m
|
|
97
|
+
Reading koatty_validation.api.json
|
|
98
|
+
|
|
99
|
+
Deleting old output from docs/api
|
|
100
|
+
Writing koatty_validation package
|
|
101
|
+
Unsupported block tag: @export
|
|
102
|
+
Unsupported block tag: @export
|
|
103
|
+
Unsupported block tag: @enum
|
|
104
|
+
Unsupported block tag: @export
|
|
105
|
+
Unsupported block tag: @default
|
|
106
|
+
Unsupported block tag: @default
|
|
107
|
+
Unsupported block tag: @default
|
|
108
|
+
Unsupported block tag: @default
|
|
109
|
+
Unsupported block tag: @export
|
|
110
|
+
Unsupported block tag: @type
|
|
111
|
+
Unsupported block tag: @export
|
|
112
|
+
Unsupported block tag: @enum
|
|
113
|
+
Unsupported block tag: @export
|
|
114
|
+
Unsupported block tag: @export
|
|
115
|
+
Unsupported block tag: @export
|
|
116
|
+
Unsupported block tag: @type
|
|
117
|
+
|
|
118
|
+
> koatty_validation@1.6.3 build:cp
|
|
119
|
+
> node scripts/postBuild && copyfiles package.json LICENSE README.md dist/
|
|
120
|
+
|
package/CHANGELOG.md
CHANGED
|
@@ -1,92 +1,97 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.6.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies
|
|
8
|
+
- koatty_lib@1.4.2
|
|
9
|
+
- koatty_container@1.17.1
|
|
10
|
+
- koatty_logger@2.3.1
|
|
11
|
+
|
|
12
|
+
## 1.6.2
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- build
|
|
17
|
+
|
|
3
18
|
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
19
|
|
|
5
|
-
|
|
20
|
+
### [1.6.1](https://github.com/koatty/koatty_validation/compare/v1.6.0...v1.6.1) (2025-10-23)
|
|
6
21
|
|
|
22
|
+
## [1.6.0](https://github.com/koatty/koatty_validation/compare/v1.5.0...v1.6.0) (2025-10-22)
|
|
7
23
|
|
|
8
24
|
### Features
|
|
9
25
|
|
|
10
|
-
|
|
26
|
+
- improve validation with instance conversion and caching ([9865b93](https://github.com/koatty/koatty_validation/commit/9865b93e469a7df16fe67e0b85dab3191437b0a8))
|
|
11
27
|
|
|
12
28
|
## [1.5.0](https://github.com/koatty/koatty_validation/compare/v1.4.0...v1.5.0) (2025-10-22)
|
|
13
29
|
|
|
14
|
-
|
|
15
30
|
### Features
|
|
16
31
|
|
|
17
|
-
|
|
32
|
+
- comprehensive code optimization and enhancements ([f7ac738](https://github.com/koatty/koatty_validation/commit/f7ac7381a75a12365aee2a83530a12044c5e1d44))
|
|
18
33
|
|
|
19
34
|
## [1.4.0](https://github.com/koatty/koatty_validation/compare/v1.3.2...v1.4.0) (2025-06-10)
|
|
20
35
|
|
|
21
|
-
|
|
22
36
|
### Features
|
|
23
37
|
|
|
24
|
-
|
|
38
|
+
- add validation examples and decorator factory ([d448709](https://github.com/koatty/koatty_validation/commit/d44870987a808cd43c5a7ed5fabd5ca43162d0b6))
|
|
25
39
|
|
|
26
40
|
### [1.3.2](https://github.com/koatty/koatty_validation/compare/v1.3.1...v1.3.2) (2024-01-14)
|
|
27
41
|
|
|
28
|
-
|
|
29
42
|
### Bug Fixes
|
|
30
43
|
|
|
31
|
-
|
|
44
|
+
- deps ([6da3e43](https://github.com/koatty/koatty_validation/commit/6da3e437c946c9de2fc45f5319768fef0c4609b0))
|
|
32
45
|
|
|
33
46
|
### [1.3.1](https://github.com/koatty/koatty_validation/compare/v1.3.0...v1.3.1) (2024-01-07)
|
|
34
47
|
|
|
35
|
-
|
|
36
48
|
### Bug Fixes
|
|
37
49
|
|
|
38
|
-
|
|
39
|
-
|
|
50
|
+
- comment ([d898cf6](https://github.com/koatty/koatty_validation/commit/d898cf68bc8693f78155b7208b419dc4b11dffb4))
|
|
51
|
+
- dto 赋值类型 ([5a1d68d](https://github.com/koatty/koatty_validation/commit/5a1d68dea0b67e988427c54abae9bfe76a6eaf48))
|
|
40
52
|
|
|
41
53
|
## [1.3.0](https://github.com/koatty/koatty_validation/compare/v1.2.10...v1.3.0) (2024-01-03)
|
|
42
54
|
|
|
43
|
-
|
|
44
55
|
### Features
|
|
45
56
|
|
|
46
|
-
|
|
47
|
-
|
|
57
|
+
- 增加@CheckFunc ([d5bdc2d](https://github.com/koatty/koatty_validation/commit/d5bdc2d51cdbf6d558e376bfda51e126158d4e75))
|
|
48
58
|
|
|
49
59
|
### Bug Fixes
|
|
50
60
|
|
|
51
|
-
|
|
61
|
+
- export ValidFuncs ([7d42023](https://github.com/koatty/koatty_validation/commit/7d42023138e9577c9646423c4c38faffba46fb10))
|
|
52
62
|
|
|
53
63
|
### [1.2.10](https://github.com/koatty/koatty_validation/compare/v1.2.9...v1.2.10) (2023-12-16)
|
|
54
64
|
|
|
55
|
-
|
|
56
65
|
### Bug Fixes
|
|
57
66
|
|
|
58
|
-
|
|
67
|
+
- assign dto property ([2357642](https://github.com/koatty/koatty_validation/commit/2357642f6ac1479c839454d40ec4bd89f3fbd7b2))
|
|
59
68
|
|
|
60
69
|
### [1.2.9](https://github.com/koatty/koatty_validation/compare/v1.2.8...v1.2.9) (2023-09-01)
|
|
61
70
|
|
|
62
71
|
### [1.2.8](https://github.com/koatty/koatty_validation/compare/v1.2.7...v1.2.8) (2023-02-17)
|
|
63
72
|
|
|
64
|
-
|
|
65
73
|
### Bug Fixes
|
|
66
74
|
|
|
67
|
-
|
|
75
|
+
- convert type ([924449d](https://github.com/koatty/koatty_validation/commit/924449d9f5fd7b14cbe3c1532f96416e69cc4eed))
|
|
68
76
|
|
|
69
77
|
### [1.2.7](https://github.com/koatty/koatty_validation/compare/v1.2.6...v1.2.7) (2023-02-17)
|
|
70
78
|
|
|
71
|
-
|
|
72
79
|
### Bug Fixes
|
|
73
80
|
|
|
74
|
-
|
|
75
|
-
|
|
81
|
+
- add script ([ca82c14](https://github.com/koatty/koatty_validation/commit/ca82c14b0a2226024fb2c4476ba7353be7a2f65a))
|
|
82
|
+
- plainToClass convert type ([06ed46e](https://github.com/koatty/koatty_validation/commit/06ed46e21ce191dc1c9f5c18ee7d029e9b6d4abf))
|
|
76
83
|
|
|
77
84
|
### [1.2.6](https://github.com/koatty/koatty_validation/compare/v1.2.5...v1.2.6) (2023-01-09)
|
|
78
85
|
|
|
79
|
-
|
|
80
86
|
### Bug Fixes
|
|
81
87
|
|
|
82
|
-
|
|
88
|
+
- err msg ([18632d4](https://github.com/koatty/koatty_validation/commit/18632d4c9b05b1d117be7f8170da0ce014018ab8))
|
|
83
89
|
|
|
84
90
|
### [1.2.5](https://github.com/koatty/koatty_validation/compare/v1.2.4...v1.2.5) (2022-08-19)
|
|
85
91
|
|
|
86
|
-
|
|
87
92
|
### Bug Fixes
|
|
88
93
|
|
|
89
|
-
|
|
94
|
+
- merge ([52c65f4](https://github.com/koatty/koatty_validation/commit/52c65f41c46a02a3fc5735e6e22ee6a3ed5337f8))
|
|
90
95
|
|
|
91
96
|
### [1.2.4](https://github.com/koatty/koatty_validation/compare/v1.2.3...v1.2.4) (2022-05-27)
|
|
92
97
|
|
package/README.md
CHANGED
|
@@ -36,10 +36,16 @@ export class Controller {
|
|
|
36
36
|
// 业务逻辑
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
// DTO 验证
|
|
39
|
+
// DTO 验证 - 异步模式(默认,适用于 Koatty 框架)
|
|
40
40
|
@Validated()
|
|
41
41
|
TestDto(user: UserDTO) {
|
|
42
|
-
//
|
|
42
|
+
// 框架会在异步获取参数后自动验证 UserDTO
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// DTO 验证 - 同步模式(适用于参数已准备好的场景)
|
|
46
|
+
@Validated(false)
|
|
47
|
+
TestDtoSync(user: UserDTO) {
|
|
48
|
+
// 方法执行前立即验证 UserDTO
|
|
43
49
|
}
|
|
44
50
|
}
|
|
45
51
|
|
|
@@ -101,11 +107,107 @@ export class UserDTO {
|
|
|
101
107
|
|
|
102
108
|
```typescript
|
|
103
109
|
@Valid(rule, options) // 参数验证
|
|
104
|
-
@Validated() // DTO验证
|
|
110
|
+
@Validated() // DTO验证 (默认异步模式)
|
|
111
|
+
@Validated(true) // DTO验证 (异步模式)
|
|
112
|
+
@Validated(false) // DTO验证 (同步模式)
|
|
105
113
|
@Expose() // 暴露属性
|
|
106
114
|
@IsDefined() // 已定义(Expose别名)
|
|
107
115
|
```
|
|
108
116
|
|
|
117
|
+
## 🎭 Validated 装饰器
|
|
118
|
+
|
|
119
|
+
`@Validated` 装饰器支持同步和异步两种验证模式,以适应不同的应用场景。
|
|
120
|
+
|
|
121
|
+
### 异步模式(默认)
|
|
122
|
+
|
|
123
|
+
适用于 **Koatty 框架**中,控制器方法的参数需要异步获取的场景。
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import { Validated, checkValidated } from 'koatty_validation';
|
|
127
|
+
|
|
128
|
+
class UserController {
|
|
129
|
+
// 默认异步模式
|
|
130
|
+
@Validated()
|
|
131
|
+
async register(user: UserDTO) {
|
|
132
|
+
// 框架流程:
|
|
133
|
+
// 1. 框架接收 HTTP 请求
|
|
134
|
+
// 2. 框架异步解析请求体,构造 UserDTO 实例
|
|
135
|
+
// 3. 框架检测到 @Validated() 元数据
|
|
136
|
+
// 4. 框架调用 checkValidated() 验证参数
|
|
137
|
+
// 5. 验证通过后调用此方法
|
|
138
|
+
return { success: true };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 显式指定异步模式
|
|
142
|
+
@Validated(true)
|
|
143
|
+
async update(id: number, user: UserDTO) {
|
|
144
|
+
return { success: true };
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**异步模式特点:**
|
|
150
|
+
- ✅ 装饰器保存验证元数据到 IOC 容器
|
|
151
|
+
- ✅ 由框架在异步获取参数后执行验证
|
|
152
|
+
- ✅ 适用于参数值需要异步获取的场景
|
|
153
|
+
- ✅ 是 Koatty 框架的推荐模式
|
|
154
|
+
|
|
155
|
+
### 同步模式
|
|
156
|
+
|
|
157
|
+
适用于**单元测试**或参数值已经准备好的场景。
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
class UserService {
|
|
161
|
+
// 同步模式 - 立即验证
|
|
162
|
+
@Validated(false)
|
|
163
|
+
async createUser(user: UserDTO) {
|
|
164
|
+
// 方法执行前已经完成验证
|
|
165
|
+
return { success: true };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 适用于多个参数的场景
|
|
169
|
+
@Validated(false)
|
|
170
|
+
async updateUser(id: number, user: UserDTO) {
|
|
171
|
+
// 只验证类类型参数(UserDTO),基础类型(number)不验证
|
|
172
|
+
return { success: true };
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**同步模式特点:**
|
|
178
|
+
- ✅ 装饰器包装原方法,在调用时立即执行验证
|
|
179
|
+
- ✅ 适用于单元测试场景
|
|
180
|
+
- ✅ 适用于参数已准备好的场景
|
|
181
|
+
- ✅ 验证失败立即抛出错误
|
|
182
|
+
|
|
183
|
+
### 手动调用 checkValidated
|
|
184
|
+
|
|
185
|
+
在框架拦截器或中间件中,可以手动调用 `checkValidated` 函数:
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
import { checkValidated } from 'koatty_validation';
|
|
189
|
+
|
|
190
|
+
async function validateInMiddleware(args: any[], paramTypes: any[]) {
|
|
191
|
+
try {
|
|
192
|
+
const { validatedArgs, validationTargets } = await checkValidated(args, paramTypes);
|
|
193
|
+
console.log('验证通过');
|
|
194
|
+
return validationTargets;
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error('验证失败:', error);
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### 选择合适的模式
|
|
203
|
+
|
|
204
|
+
| 场景 | 推荐模式 | 原因 |
|
|
205
|
+
|------|---------|------|
|
|
206
|
+
| Koatty 框架控制器 | 异步 `@Validated()` | 参数需要异步获取 |
|
|
207
|
+
| 单元测试 | 同步 `@Validated(false)` | 参数已准备好,立即验证 |
|
|
208
|
+
| 独立服务/工具 | 同步 `@Validated(false)` | 不依赖框架,立即验证 |
|
|
209
|
+
| 框架拦截器 | 手动 `checkValidated()` | 完全控制验证时机 |
|
|
210
|
+
|
|
109
211
|
## 🔧 自定义装饰器
|
|
110
212
|
|
|
111
213
|
### 使用装饰器工厂创建自定义验证器
|
|
@@ -310,6 +412,7 @@ const validatedData = await ClassValidator.valid(UserSchema, rawData, true);
|
|
|
310
412
|
- [基础用法示例](./examples/basic-usage.ts)
|
|
311
413
|
- [自定义装饰器示例](./examples/custom-decorators-example.ts)
|
|
312
414
|
- [完整使用示例](./examples/usage-example.ts)
|
|
415
|
+
- [Validated 异步/同步模式示例](./examples/validated-async-sync-example.ts)
|
|
313
416
|
|
|
314
417
|
## 🔍 可用验证函数
|
|
315
418
|
|
package/dist/README.md
CHANGED
|
@@ -36,10 +36,16 @@ export class Controller {
|
|
|
36
36
|
// 业务逻辑
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
// DTO 验证
|
|
39
|
+
// DTO 验证 - 异步模式(默认,适用于 Koatty 框架)
|
|
40
40
|
@Validated()
|
|
41
41
|
TestDto(user: UserDTO) {
|
|
42
|
-
//
|
|
42
|
+
// 框架会在异步获取参数后自动验证 UserDTO
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// DTO 验证 - 同步模式(适用于参数已准备好的场景)
|
|
46
|
+
@Validated(false)
|
|
47
|
+
TestDtoSync(user: UserDTO) {
|
|
48
|
+
// 方法执行前立即验证 UserDTO
|
|
43
49
|
}
|
|
44
50
|
}
|
|
45
51
|
|
|
@@ -101,11 +107,107 @@ export class UserDTO {
|
|
|
101
107
|
|
|
102
108
|
```typescript
|
|
103
109
|
@Valid(rule, options) // 参数验证
|
|
104
|
-
@Validated() // DTO验证
|
|
110
|
+
@Validated() // DTO验证 (默认异步模式)
|
|
111
|
+
@Validated(true) // DTO验证 (异步模式)
|
|
112
|
+
@Validated(false) // DTO验证 (同步模式)
|
|
105
113
|
@Expose() // 暴露属性
|
|
106
114
|
@IsDefined() // 已定义(Expose别名)
|
|
107
115
|
```
|
|
108
116
|
|
|
117
|
+
## 🎭 Validated 装饰器
|
|
118
|
+
|
|
119
|
+
`@Validated` 装饰器支持同步和异步两种验证模式,以适应不同的应用场景。
|
|
120
|
+
|
|
121
|
+
### 异步模式(默认)
|
|
122
|
+
|
|
123
|
+
适用于 **Koatty 框架**中,控制器方法的参数需要异步获取的场景。
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import { Validated, checkValidated } from 'koatty_validation';
|
|
127
|
+
|
|
128
|
+
class UserController {
|
|
129
|
+
// 默认异步模式
|
|
130
|
+
@Validated()
|
|
131
|
+
async register(user: UserDTO) {
|
|
132
|
+
// 框架流程:
|
|
133
|
+
// 1. 框架接收 HTTP 请求
|
|
134
|
+
// 2. 框架异步解析请求体,构造 UserDTO 实例
|
|
135
|
+
// 3. 框架检测到 @Validated() 元数据
|
|
136
|
+
// 4. 框架调用 checkValidated() 验证参数
|
|
137
|
+
// 5. 验证通过后调用此方法
|
|
138
|
+
return { success: true };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 显式指定异步模式
|
|
142
|
+
@Validated(true)
|
|
143
|
+
async update(id: number, user: UserDTO) {
|
|
144
|
+
return { success: true };
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**异步模式特点:**
|
|
150
|
+
- ✅ 装饰器保存验证元数据到 IOC 容器
|
|
151
|
+
- ✅ 由框架在异步获取参数后执行验证
|
|
152
|
+
- ✅ 适用于参数值需要异步获取的场景
|
|
153
|
+
- ✅ 是 Koatty 框架的推荐模式
|
|
154
|
+
|
|
155
|
+
### 同步模式
|
|
156
|
+
|
|
157
|
+
适用于**单元测试**或参数值已经准备好的场景。
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
class UserService {
|
|
161
|
+
// 同步模式 - 立即验证
|
|
162
|
+
@Validated(false)
|
|
163
|
+
async createUser(user: UserDTO) {
|
|
164
|
+
// 方法执行前已经完成验证
|
|
165
|
+
return { success: true };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 适用于多个参数的场景
|
|
169
|
+
@Validated(false)
|
|
170
|
+
async updateUser(id: number, user: UserDTO) {
|
|
171
|
+
// 只验证类类型参数(UserDTO),基础类型(number)不验证
|
|
172
|
+
return { success: true };
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**同步模式特点:**
|
|
178
|
+
- ✅ 装饰器包装原方法,在调用时立即执行验证
|
|
179
|
+
- ✅ 适用于单元测试场景
|
|
180
|
+
- ✅ 适用于参数已准备好的场景
|
|
181
|
+
- ✅ 验证失败立即抛出错误
|
|
182
|
+
|
|
183
|
+
### 手动调用 checkValidated
|
|
184
|
+
|
|
185
|
+
在框架拦截器或中间件中,可以手动调用 `checkValidated` 函数:
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
import { checkValidated } from 'koatty_validation';
|
|
189
|
+
|
|
190
|
+
async function validateInMiddleware(args: any[], paramTypes: any[]) {
|
|
191
|
+
try {
|
|
192
|
+
const { validatedArgs, validationTargets } = await checkValidated(args, paramTypes);
|
|
193
|
+
console.log('验证通过');
|
|
194
|
+
return validationTargets;
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error('验证失败:', error);
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### 选择合适的模式
|
|
203
|
+
|
|
204
|
+
| 场景 | 推荐模式 | 原因 |
|
|
205
|
+
|------|---------|------|
|
|
206
|
+
| Koatty 框架控制器 | 异步 `@Validated()` | 参数需要异步获取 |
|
|
207
|
+
| 单元测试 | 同步 `@Validated(false)` | 参数已准备好,立即验证 |
|
|
208
|
+
| 独立服务/工具 | 同步 `@Validated(false)` | 不依赖框架,立即验证 |
|
|
209
|
+
| 框架拦截器 | 手动 `checkValidated()` | 完全控制验证时机 |
|
|
210
|
+
|
|
109
211
|
## 🔧 自定义装饰器
|
|
110
212
|
|
|
111
213
|
### 使用装饰器工厂创建自定义验证器
|
|
@@ -310,6 +412,7 @@ const validatedData = await ClassValidator.valid(UserSchema, rawData, true);
|
|
|
310
412
|
- [基础用法示例](./examples/basic-usage.ts)
|
|
311
413
|
- [自定义装饰器示例](./examples/custom-decorators-example.ts)
|
|
312
414
|
- [完整使用示例](./examples/usage-example.ts)
|
|
415
|
+
- [Validated 异步/同步模式示例](./examples/validated-async-sync-example.ts)
|
|
313
416
|
|
|
314
417
|
## 🔍 可用验证函数
|
|
315
418
|
|