Hi! I am using routing-controllers with the amazing boilerplate by @w3tech https://github.com/w3tecch/express-typescript-boilerplate, it has routing-controllers, class-validator, typeorm and type-graphql (among many other cool libraries). But I cannot get the validation to work either on the @Body parameters and many other things. I have specified validation on my creation of the Express server this way:
const expressApp: Application = createExpressServer({
cors: true,
classTransformer: true,
// Log validation errors.
validation: {
forbidUnknownValues: true,
},
routePrefix: env.app.routePrefix,
defaultErrorHandler: false,
controllers: env.app.dirs.controllers,
middlewares: env.app.dirs.middlewares,
interceptors: env.app.dirs.interceptors,
authorizationChecker: authorizationChecker(connection),
currentUserChecker: currentUserChecker(connection),
});
Which now throws:
{
"name": "BadRequestError",
"message": "Invalid body, check 'errors' property for more info.",
"errors": [
{
"target": {},
"children": [],
"constraints": {
"unknownValue": "an unknown value was passed to the validate function"
}
}
]
}
This is my controller function (using @JsonController):
@Post()
public create(@CurrentUser({ required: true }) user: User, @Body({ required: true }) client: Client): Promise<Client> {
client.createdBy = user;
return this.clientService.create(client);
}
At this point, I can't understand why validation doesn't work and I am assuming it _might_ be the boilerplate stack having some dependency conflict or some kind of incompatibility among versions. Is there something missing on the boilerplate's main configuration? I've tried some _custom_ routing-controllers npm packages to flatten dependencies with class-validator, which was an error I was running into earlier today, but the main problem is I cannot figure how to diagnose the source of the problem and, as far as I am concerned, it is something to do with routing-controller's configuration or integration with class-validator _or_ with typeorm. Are there any guidance about this issue?
Thanks in advance!
(And sorry for the awful english).
I figured it out, just disabled the typedi container on class-validator and everything works out fine for me. If someone is using some boilerplate like this, just go to the iocLoader.ts file and comment out the line 15 classValidatorUseContainer and everything should be fine now. Closing as solved.
Having the same issue, however removing classValidatorUseContainer from iocLoader.ts did not fix the issue for me
@humbertowoody's solution worked for me as well.
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.
Most helpful comment
@humbertowoody's solution worked for me as well.