Custom validation with database
See original GitHub issueIt works:
module.exports.postSignup = [
check('email', 'E-mail already in use').custom((value, { req }) => value !== 'barrosfilipe@test.com'),
...
It doesn’t:
module.exports.postSignup = [
check('email', 'E-mail already in use').custom((value, { req }) => {
value !== 'barrosfilipe@test.com'
}),
...
The reason why I want to use the example that doesn’t work is because I want to use MySQL query to validate if e-mail address is already in use. Something like this:
module.exports.postSignup = [
check('email', 'E-mail already in use').custom((value, { req }) => {
const db = require('../db');
db.query('SELECT email FROM `users` WHERE email = ?'
, [req.body.email], function(error, results, fields) {
if (error) throw error;
if (results.length > 0) {
return value !== results[0].email;
};
});
}),
...
How should I do that? Any ideas?
Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Tutorial: Custom Validation Rules - Flyway
This brief tutorial will teach you how to customize your validation rules. Introduction. Flyway validates your migrations according to its own conventions, ...
Read more >Using a database with Custom Validators - Stack Overflow
Here's a custom validator that uses a stringHelper service which is used in the validator. Calling the validator using Microsoft.Extensions.
Read more >Custom validation with database in NestJS
Easy, just quick look at the documentation and you can write your own rules and can use it with @Validate() decorator. Better! It's...
Read more >Custom validation with database in NestJS - Medium
Easy, just quick look at the documentation and you can write your own rules and can use it with @Validate() decorator. Better! It's...
Read more >Validation - Laravel - The PHP Framework For Web Artisans
Laravel includes a wide variety of convenient validation rules that you may apply to data, even providing the ability to validate if values...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Eae @barrosfilipe 😉,
The example you gave as not working is because you’re using an arrow function without a
return
.value => value
is the same asvalue => { return value }
, butvalue => { value }
is simply a function that evaluatesvalue
, without returning anything.In the full example for DB validation, you are trying to return from within an async callback. In this case, you should use promises instead. For a detailed explanation, see this popular StackOverflow question.
You should use promises for this. They must be rejected for the validation to be considered as failed.