express-validator: checkSchema - custom validator and exists errorMessage
Hi!
I’m trying to use Schema Validation with 5.0.0.
customvalidator throw validation errorvalidatorCfg.validator is not a function- For
existsparamerrorMessageignored
checkSchema({
title: {
in: ['body'],
exists: {
errorMessage: 'title is required', // <- doesn't work
},
custom(value) { // <- doesn't work
console.log(value);
return true;
}
},
password: {
isLength: {
errorMessage: 'Password should be at least 7 chars long', // <- works
},
},
});
customValidators param and all in this doc section https://github.com/ctavan/express-validator#legacy-api are deprecated…
What am I doing wrong?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 17 (8 by maintainers)
Hm, the
existsone is strange, by “doesn’t work” do you mean the error message is stillInvalid value? It shouldn’t be, so there might be something else wrong with your setup.The
customone should be specified by passingoptions. I will add an example to help clear future questions about it.This is a very minimal app with both validators working:
Hi @prettymuchbryce, it’s out of the plans for the check APIs to have any interactions with the legacy ones (the
expressValidatormiddleware you used), as they are gonna be phased out at some point. The less state we store in the request, the better.What I would like to do instead is something like
validatorResult.withDefaults(options): a factory for the check API that you can inject custom defaults, like validators:For now, what you can do is move these validators or schemas (or whatever you like) into separate files, and import them in your schemas.
Yes, the same for custom sanitizers.