@nestia/core 1.2.0-dev.20230504 → 1.2.1-dev.20230505
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/README.md +7 -3
- package/lib/decorators/EncryptedBody.js +35 -73
- package/lib/decorators/EncryptedBody.js.map +1 -1
- package/lib/decorators/EncryptedController.d.ts +0 -5
- package/lib/decorators/EncryptedController.js +0 -5
- package/lib/decorators/EncryptedController.js.map +1 -1
- package/lib/decorators/EncryptedRoute.js +2 -9
- package/lib/decorators/EncryptedRoute.js.map +1 -1
- package/lib/decorators/PlainBody.js +14 -55
- package/lib/decorators/PlainBody.js.map +1 -1
- package/lib/decorators/TypedBody.js +20 -65
- package/lib/decorators/TypedBody.js.map +1 -1
- package/lib/decorators/TypedParam.js +6 -5
- package/lib/decorators/TypedParam.js.map +1 -1
- package/lib/decorators/TypedQuery.js +42 -59
- package/lib/decorators/TypedQuery.js.map +1 -1
- package/lib/decorators/internal/get_path_and_stringify.js +5 -3
- package/lib/decorators/internal/get_path_and_stringify.js.map +1 -1
- package/lib/decorators/internal/send_bad_request.d.ts +2 -0
- package/lib/decorators/internal/send_bad_request.js +13 -0
- package/lib/decorators/internal/send_bad_request.js.map +1 -0
- package/lib/decorators/internal/validate_request_body.d.ts +2 -1
- package/lib/decorators/internal/validate_request_body.js +15 -9
- package/lib/decorators/internal/validate_request_body.js.map +1 -1
- package/package.json +3 -3
- package/src/decorators/EncryptedBody.ts +30 -21
- package/src/decorators/EncryptedController.ts +0 -5
- package/src/decorators/EncryptedRoute.ts +2 -15
- package/src/decorators/PlainBody.ts +21 -7
- package/src/decorators/TypedBody.ts +26 -12
- package/src/decorators/TypedParam.ts +16 -8
- package/src/decorators/TypedQuery.ts +21 -15
- package/src/decorators/internal/get_path_and_stringify.ts +2 -3
- package/src/decorators/internal/send_bad_request.ts +12 -0
- package/src/decorators/internal/validate_request_body.ts +16 -13
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BadRequestException } from "@nestjs/common";
|
|
2
2
|
|
|
3
|
-
import { IValidation, TypeGuardError } from "typia";
|
|
3
|
+
import typia, { IValidation, TypeGuardError } from "typia";
|
|
4
4
|
|
|
5
5
|
import { IRequestBodyValidator } from "../../options/IRequestBodyValidator";
|
|
6
6
|
import { TransformError } from "./TransformError";
|
|
@@ -8,14 +8,15 @@ import { TransformError } from "./TransformError";
|
|
|
8
8
|
export const validate_request_body =
|
|
9
9
|
(method: string) =>
|
|
10
10
|
<T>(validator?: IRequestBodyValidator<T>) => {
|
|
11
|
-
if (!validator)
|
|
11
|
+
if (!validator) return () => TransformError(method);
|
|
12
12
|
else if (validator.type === "assert") return assert(validator.assert);
|
|
13
13
|
else if (validator.type === "is") return is(validator.is);
|
|
14
14
|
else if (validator.type === "validate")
|
|
15
15
|
return validate(validator.validate);
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
return () =>
|
|
17
|
+
new Error(
|
|
18
|
+
`Error on nestia.core.${method}(): invalid typed validator.`,
|
|
19
|
+
);
|
|
19
20
|
};
|
|
20
21
|
|
|
21
22
|
const assert =
|
|
@@ -23,9 +24,10 @@ const assert =
|
|
|
23
24
|
(data: T) => {
|
|
24
25
|
try {
|
|
25
26
|
closure(data);
|
|
27
|
+
return null;
|
|
26
28
|
} catch (exp) {
|
|
27
|
-
if (exp
|
|
28
|
-
|
|
29
|
+
if (typia.is<TypeGuardError>(exp)) {
|
|
30
|
+
return new BadRequestException({
|
|
29
31
|
path: exp.path,
|
|
30
32
|
reason: exp.message,
|
|
31
33
|
expected: exp.expected,
|
|
@@ -41,18 +43,19 @@ const is =
|
|
|
41
43
|
<T>(closure: (data: T) => boolean) =>
|
|
42
44
|
(data: T) => {
|
|
43
45
|
const success: boolean = closure(data);
|
|
44
|
-
|
|
46
|
+
return success ? null : new BadRequestException(MESSAGE);
|
|
45
47
|
};
|
|
46
48
|
|
|
47
49
|
const validate =
|
|
48
50
|
<T>(closure: (data: T) => IValidation<T>) =>
|
|
49
51
|
(data: T) => {
|
|
50
52
|
const result: IValidation<T> = closure(data);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
return result.success
|
|
54
|
+
? null
|
|
55
|
+
: new BadRequestException({
|
|
56
|
+
errors: result.errors,
|
|
57
|
+
message: MESSAGE,
|
|
58
|
+
});
|
|
56
59
|
};
|
|
57
60
|
|
|
58
61
|
const MESSAGE = "Request body data is not following the promised type.";
|