-
-
Notifications
You must be signed in to change notification settings - Fork 616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
checkSchema - custom validator and exists errorMessage #527
Comments
Hm, the The This is a very minimal app with both validators working: const { checkSchema, validationResult } = require('./check');
const express = require('express');
const app = express();
app.use(express.json());
app.post('/*', checkSchema({
foo: {
in: ['body'],
exists: {
errorMessage: 'bla'
},
custom: {
options: value => (value || '').startsWith('foo')
}
}
}), (req, res) => {
res.json(validationResult(req).array());
});
app.listen(3000); |
@gustavohenke thank you! It's all right with Is there a way to use multiple custom validators with different error messages? check('password')
.custom(value => someValidationFunction(value))
.withMessage('Some validation error message')
.custom(value => anotherValidationFunction(value))
.withMessage('Another validation error message') |
If you don't use schema, then yes. |
@gustavohenke same story for custom sanitizers? Schema better for using, chains ugly :( |
Yes, the same for custom sanitizers. |
not working with async.js here is my code: email: {
errorMessage: 'Please enter a valid email address',
isEmail : true,
trim: true,
custom: {
options: (value) => {
async.waterfall([
function(callback) {
User.findByEmail(value, function (err, result) {
if (err) {
callback(new Error("failed getting something:" + err.message));
// we should return here
}
// since we did not return, this callback still will be called and
// `processData` will be called twice
callback(null, result);
});
},
//processData
], function (err, result) {
if(err) {
return false;
}
else{
if(result.length==0){
console.log('record not found');
return false;
}else{
console.log('record found');
return true;
}
}
}
)
},
errorMessage: 'email taken',
}
} Please help me to resolve this I always get the 'email taken message'. |
You must return a promise. |
can you please guide me how can I do this, coz I am new in node js. PLEASE HELP |
Sorry, I don't know about async.js, but you could post this on StackOverflow with the relevant tags. I'm pretty sure the popularity of async.js will make it easy for you to find get answers quickly though. |
Okay Thanks a lot gustavohenke!!!
|
@gustavohenke I think the usage for custom should be more clearly documented for |
Fair enough @dpkshrma. |
@gustavohenke Would it not be possible to allow for custom validators and sanitizers in the fields themselves without using a For example: app.use(expressValidator({
customValidators: {
myCustomValidator: value => true,
}
})); Then in the router.get("/:myParam",
checkSchema({
myParam: {
in: ['params'],
isString: true,
myCustomValidator: true
},
}),
... Would you entertain a pull request with such a change? |
Hi @prettymuchbryce, it's out of the plans for the check APIs to have any interactions with the legacy ones (the What I would like to do instead is something like // Use these, don't import them from `express-validator/check`!
const { check, checkSchema, validationResult, etc } = createCheckAPI({
customSanitizers: { ... },
customValidators: { ... },
someOtherOption: value
}) 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. |
@gustavohenke I understand. I didn't realize this middleware was legacy. I like your proposal, and I feel it would solve the problem with the awkwardness of the Do you have plans to do this yourself in the near term? Would you be open to receiving a pull request for this enhancement? |
Yes, any PRs are welcome! |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hi!
I'm trying to use Schema Validation with 5.0.0.
custom
validator throw validation errorvalidatorCfg.validator is not a function
exists
paramerrorMessage
ignoredcustomValidators
param and all in this doc section https://github.com/ctavan/express-validator#legacy-api are deprecated...What am I doing wrong?
The text was updated successfully, but these errors were encountered: