question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom validation with database

See original GitHub issue

It 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
gustavohenkecommented, Sep 25, 2017

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 as value => { return value }, but value => { value } is simply a function that evaluates value, 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.

1reaction
gustavohenkecommented, Sep 29, 2017

You should use promises for this. They must be rejected for the validation to be considered as failed.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found