routing-controllers: Routing-Controllers + class-validator bug?
When using the automatic validation from routing-controllers for some reason the email “patch@patch” is valid when we are using these validators and the “patch” validator group
@IsEmail({
allow_display_name: false,
allow_utf8_local_part: true,
require_tld: true
}, {
groups: ["post", "patch", "login"]
})
@IsOptional({
groups: ["patch"]
})
public email;
Doing it the manual way like so works however
let user = new UserValidation();
user.email = "patch@patch";
validate(user, {groups: ["patch"]}).then(console.log);
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 8
- Comments: 39 (12 by maintainers)
I think I might be seeing similar issue too. Tried running a version of the auto validation based on the example in the README:
No matter what I send in the POST request I get a success back. Not sure if I’ve missed something in the above code but for the life of me I can’t get it to validate and throw an error.
Validating manually through the class-validator does work though.
So I have done few test and this is what I found:
normal as RC requires CV ^0.7.0
normal as RC requires CV ^0.8.1
So it seems to be a mix of class-validation / routing-controller changes breaking the validation, as the newer version of class-transformer works with routing-controller 0.7.6 and class-validator 0.7.3
@pleerock @NoNameProvided would you have any chance looking at this issue as it makes the stack (routing controller, class validator, class transfomers) non useable with latest version?
Thanks again 👍
Temporarily solved this problem. I don’t know why it works.
I found that it is sufficient to declare the dependency to class-validator to the same version as in routing-controllers:
"class-validator": "^0.8.1",. I had to remove my node_modules folder and the lock file and run npm install again. After that here was no additional class-validator package in the node_modules folder for routing-controllers. That way the app and routing-controllers were using the exactly same version from the root nod_modules folder. No error anymore.I’ve published
@gritcode/routing-controllersthat includes this fix (ie:class-validatoras a peer dependency).To use it:
npm i @gritcode/routing-controllers class-validatorI’ve sent a PR back to this abandoned lib, but doubt it’ll ever be merged
What i did to fix it was to look in
node_modules\routing-controllers\package.json, and copy the versions forclass-validatorandclass-transformer(if you use that) over to my own package.json. And then download the dependencies againA temporary fix that’s working for me is to remove
class-validatorfrom package.json, and instead just import from routing-controllers module. E.g.import { IsNotEmpty } from 'routing-controllers/node_modules/class-validator';The issue is caused because the version of class-validator that you have installed does not match the version of class-validator that routing-controllers is using. (see issue #424 and typestack/class-validator#166)
For a temporary fix you can run the following commands:
Edit: This will not work if you are using class-transformer. If you are using class-transformer read below for a solution
Anyone have better solution than this ? so far this is the only working solution for me.
Dependencies are
"class-validator": "^0.9.1",uninstalled packageI think the reason for the bug is that
class-validatormaintains an internal registry of the contracts that you decorate. If you installclass-validatoras a package dependency, you’ll likely get the newer one where your contracts will be registered; but whenrouting-controllersruns it uses its internalclass-validator(v0.8.5) that would have an empty registry and hence not validate anything.A proposed solution to
routing-controllers: addclass-validatoras anoptional-peer-dependencyand force consumers that want to validate to add it themselves.A proposed solution to people who declare contracts in their api project: don’t add
class-validatorto your project. By default if you addrouting-controllersyou’ll also getclass-validatorautomatically, so just use tahtA proposed solution to people who keep contracts in a separate package that gets installed to their api: add
class-validatoras adev-dependencyin your contracts package. addrouting-controllersto your api@nolazybits I don’t think you will have a fix soon, looking at latest commit date. I forked it and just updated package.json versions to latest and it seems to work. This is the modified version you can use from npm until official release: https://www.npmjs.com/package/@mardari/routing-controllers
@Andrew5569 Temporary fix works. Cheers!
I created an example: https://github.com/Andrew5569/rc-body-validation
This is working for me
The problem might not be in
routing-controllersdirectly but with transformation of plain object to class instance usingclass-transformer.