Skip to content

Commit c9ca9c8

Browse files
committed
Validation pipe example
1 parent cd62156 commit c9ca9c8

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
"@types/jest": "^25.1.4",
3131
"@types/node": "^13.9.0",
3232
"@types/supertest": "^2.0.7",
33+
"class-transformer": "^0.3.2",
34+
"class-validator": "^0.13.1",
3335
"jest": "^25.1.0",
3436
"prettier": "^1.15.3",
3537
"rxjs": "^6.0.0",

src/index.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ describe("json-rpc-e2e", () => {
4545
expect(res).toStrictEqual({ test: "hi" });
4646
});
4747

48+
it(`should throw if sends an invalid DTO`, async () => {
49+
let res = service.invokeClientService({ test: "" });
50+
await expect(res).rejects.toThrow("hi");
51+
});
4852
it(`should fail to make a request with an unauthorized JsonRpcClient`, async () => {
4953
let result = unauthorizedService.invokeClientService({ test: "hi" });
5054
await expect(result).rejects.toThrowError("Forbidden resource");

src/test-handler.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import {
1010
UseInterceptors,
1111
UseGuards,
1212
Scope,
13-
Inject
13+
ValidationPipe
1414
} from "@nestjs/common";
1515
import { Ctx } from "@nestjs/microservices";
1616

17+
import { IsNotEmpty } from "class-validator";
18+
1719
import { CodedRpcException, JsonRpcContext, RpcController, RpcMethod, RpcService } from ".";
1820

1921
const initialModuleState = {
@@ -83,6 +85,11 @@ class TestGuard implements CanActivate {
8385

8486
type IRpcTestService = RpcController<ITestClientService>;
8587

88+
export class TestDto {
89+
@IsNotEmpty()
90+
test!: string;
91+
}
92+
8693
@RpcService({
8794
namespace: "test"
8895
})
@@ -92,14 +99,6 @@ export class TestService implements IRpcTestService {
9299
DecorationsState.serviceConstructorCount = DecorationsState.serviceConstructorCount + 1;
93100
}
94101

95-
@UsePipes(TestPipe)
96-
@UseInterceptors(TestInterceptor)
97-
@UseGuards(TestGuard)
98-
@RpcMethod()
99-
public async invoke(params: { test: string }) {
100-
return Promise.resolve(params);
101-
}
102-
103102
@UsePipes(TestPipe)
104103
@UseInterceptors(TestInterceptor)
105104
@UseGuards(TestGuard)
@@ -110,10 +109,11 @@ export class TestService implements IRpcTestService {
110109
}
111110

112111
@UsePipes(TestPipe)
112+
@UsePipes(ValidationPipe)
113113
@UseInterceptors(TestInterceptor)
114114
@UseGuards(TestGuard)
115115
@RpcMethod()
116-
public async invokeClientService(params: { test: string }) {
116+
public async invokeClientService(params: TestDto) {
117117
return Promise.resolve(params);
118118
}
119119

@@ -131,7 +131,6 @@ export class TestService implements IRpcTestService {
131131
}
132132

133133
export interface ITestClientService {
134-
invoke(params: { test: string }): Promise<{ test: string }>;
135134
invokeClientService(params: { test: string }): Promise<{ test: string }>;
136135
testError(params: { errorTest: string }): Promise<void>;
137136
injectContext(params: {}): Promise<{ key: string | undefined }>;

0 commit comments

Comments
 (0)