fix: QueryParams validation not working with koa
Seems like validation of query params not working with koa. Here is a reproducing code:
import "reflect-metadata"
import { Post, Body, Get, JsonController, QueryParams, createKoaServer, createExpressServer } from "routing-controllers";
import { MinLength } from "class-validator"
class UserQuery {
@MinLength(5)
author: string
@MinLength(5)
contributor: string
hello() {
return "Hello world!"
}
}
@JsonController()
class GithubController {
@Get("/github")
helloFromGithub(@QueryParams() query: UserQuery) {
return query.hello()
}
}
createExpressServer({
validation: true,
}).listen(3001, () => {
console.log("express on")
})
createKoaServer({
validation: true,
}).listen(3002, () => {
console.log("koa on")
})
Response from express (http://localhost:3001/github?author=Umed&contributor=19majkel94&somethingPrivate=blablabla) is ok - for valid params i get correct response "Hello world!"
{
"name": "BadRequestError",
"message": "Invalid queries, check 'errors' property for more info.",
"stack": "Error\n at BadRequestError.HttpError [as constructor] (E:\\#Programowanie\\#GitHub\\routing-controllers-test\\src\\http-error\\HttpError.ts:19:22)\n at new BadRequestError (E:\\#Programowanie\\#GitHub\\routing-controllers-test\\src\\http-error\\BadRequestError.ts:10:9)\n at E:\\#Programowanie\\#GitHub\\routing-controllers-test\\src\\ActionParameterHandler.ts:185:40\n at <anonymous>\n at process._tickDomainCallback (internal/process/next_tick.js:208:7)",
"errors": [
{
"target": {
"author": "Umed",
"contributor": "19majkel94",
"somethingPrivate": "blablabla"
},
"value": "Umed",
"property": "author",
"children": [],
"constraints": {
"minLength": "author must be longer than or equal to 5 characters"
}
}
]
}
Sample response from koa even with correct parameteres (http://localhost:3002/github?author=pleerock&contributor=19majkel94&somethingPrivate=blablabla):
{
"name": "BadRequestError",
"message": "Invalid queries, check 'errors' property for more info.",
"stack": "Error\n at BadRequestError.HttpError [as constructor] (E:\\#Programowanie\\#GitHub\\routing-controllers-test\\src\\http-error\\HttpError.ts:19:22)\n at new BadRequestError (E:\\#Programowanie\\#GitHub\\routing-controllers-test\\src\\http-error\\BadRequestError.ts:10:9)\n at E:\\#Programowanie\\#GitHub\\routing-controllers-test\\src\\ActionParameterHandler.ts:185:40\n at <anonymous>\n at process._tickDomainCallback (internal/process/next_tick.js:208:7)",
"errors": {}
}
With @Body or @QueryParam validation works ok. Only with QueryParams with koa the weird class-validator error appears on the console:
ERROR: { Error
at BadRequestError.HttpError [as constructor] (E:\#Programowanie\#GitHub\routing-controllers-test\src\http-error\HttpError.ts:19:22)
at new BadRequestError (E:\#Programowanie\#GitHub\routing-controllers-test\src\http-error\BadRequestError.ts:10:9)
at E:\#Programowanie\#GitHub\routing-controllers-test\src\ActionParameterHandler.ts:185:40
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:208:7)
httpCode: 400,
name: 'BadRequestError',
message: 'Invalid queries, check \'errors\' property for more info.',
errors:
TypeError: Cannot read property 'prototype' of undefined
at E:\#Programowanie\#GitHub\routing-controllers-test\src\metadata\MetadataStorage.ts:79:36
at Array.filter (native)
at MetadataStorage.getTargetValidationMetadatas (E:\#Programowanie\#GitHub\routing-controllers-test\src\metadata\MetadataStorage.ts:75:61)
at ValidationExecutor.execute (E:\#Programowanie\#GitHub\routing-controllers-test\src\validation\ValidationExecutor.ts:44:54)
at Validator.coreValidate (E:\#Programowanie\#GitHub\routing-controllers-test\src\validation\Validator.ts:31:18)
at Validator.<anonymous> (E:\#Programowanie\#GitHub\routing-controllers-test\src\validation\Validator.ts:73:35)
at step (E:\#Programowanie\#GitHub\routing-controllers-test\node_modules\class-validator\validation\Validator.js:32:23)
at Object.next (E:\#Programowanie\#GitHub\routing-controllers-test\node_modules\class-validator\validation\Validator.js:13:53)
at E:\#Programowanie\#GitHub\routing-controllers-test\node_modules\class-validator\validation\Validator.js:7:71
at Promise (<anonymous>) }
@pleerock I assigned you to the issue as I have digged through the code and I have no idea why it won't work only with koa. Looks like a problem with metadatas and class-validator, so it would be nice if you help with this bug 😉
I solve it by using a middleware for koa which contains below code:
Reflect.setPrototypeOf(ctx.query, {})
The prototype of ctx.query is null and this caused class-transformer error.
https://github.com/pleerock/class-transformer/issues/64
This has been fixed in typestack/class-transformer#0cd39ed.
Stale issue message
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.