Skip to content

Commit 644f094

Browse files
committed
tests for paramName in BadRequestError
1 parent a83c478 commit 644f094

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

test/functional/class-validator-options.spec.ts

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import {Get} from "../../src/decorator/Get";
88
import {QueryParam} from "../../src/decorator/QueryParam";
99
import {ResponseClassTransformOptions} from "../../src/decorator/ResponseClassTransformOptions";
1010
import {RoutingControllersOptions} from "../../src/RoutingControllersOptions";
11+
import {ExpressErrorMiddlewareInterface} from "../../src/driver/express/ExpressErrorMiddlewareInterface";
12+
import {Middleware} from "../../src/decorator/Middleware";
13+
import {KoaMiddlewareInterface} from "../../src/driver/koa/KoaMiddlewareInterface";
14+
1115
const chakram = require("chakram");
1216
const expect = chakram.expect;
1317

@@ -208,4 +212,80 @@ describe("parameters auto-validation", () => {
208212
});
209213
});
210214

211-
});
215+
describe("should contain param name on validation failed", () => {
216+
217+
let requestFilter: any;
218+
beforeEach(() => {
219+
requestFilter = undefined;
220+
});
221+
222+
before(() => {
223+
getMetadataArgsStorage().reset();
224+
225+
@JsonController()
226+
class UserController {
227+
228+
@Get("/user")
229+
getUsers(@QueryParam("filter") filter: UserFilter): any {
230+
requestFilter = filter;
231+
const user = new UserModel();
232+
user.id = 1;
233+
user._firstName = "Umed";
234+
user._lastName = "Khudoiberdiev";
235+
return user;
236+
}
237+
}
238+
});
239+
240+
const options: RoutingControllersOptions = {
241+
validation: true,
242+
defaultErrorHandler: false
243+
};
244+
245+
let expressApp: any, koaApp: any;
246+
before(done => {
247+
248+
@Middleware({type: "after"})
249+
class ExpressErrorHandler implements ExpressErrorMiddlewareInterface {
250+
251+
error(error: any, request: any, response: any, next: (err?: any) => any): void {
252+
response.statusCode = 400;
253+
response.send(error.paramName);
254+
next();
255+
}
256+
}
257+
258+
expressApp = createExpressServer(options)
259+
.listen(3001, done);
260+
});
261+
after(done => expressApp.close(done));
262+
before(done => {
263+
264+
@Middleware({type: "before"})
265+
class KoaErrorHandler implements KoaMiddlewareInterface {
266+
267+
async use(context: any, next: (err?: any) => Promise<any>): Promise<any> {
268+
try {
269+
await next();
270+
} catch (e) {
271+
context.body = e.paramName;
272+
context.status = 400;
273+
}
274+
}
275+
}
276+
277+
koaApp = createKoaServer(options).listen(3002, done);
278+
});
279+
after(done => koaApp.close(done));
280+
281+
const invalidFilter = {
282+
keyword: "aa"
283+
};
284+
285+
assertRequest([3001, 3002], "get", `user?filter=${JSON.stringify(invalidFilter)}`, response => {
286+
expect(response).to.have.status(400);
287+
expect(response.body).to.equal("filter");
288+
});
289+
});
290+
291+
});

0 commit comments

Comments
 (0)