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.

Allow Custom Validator to return Promise

See original GitHub issue

I’ve been using this library with typescript. Then, I had found a semantic problem when I was trying to use Promises in the custom validators.

I have a custom validator to check if a username is already registered in the database. That’s why I need to implement the custom validation returning Promise.

I’m trying to do something like that:

router.use(validator({
  customValidators: {
    isUsernameTaken : (param, _id) => {
      return new Promise((resolve, reject) => {
        let conditions = {
          username: param
        };
        if (typeof _id !== 'undefined') {
          conditions['_id'] = {$ne: _id};
        }
        return UserService.findOne(conditions)
          .then(user => {
            if (user) {
              return reject(user);
            }
            return resolve(true);
          })
          .catch(err => {
            resolve(err)
        });
      });
    }
  }
}));

Thanks.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
kvcprcommented, Apr 6, 2017

Why you don’t return true or false from UserService.findOne promise? I think it should work.

return UserService.findOne(conditions)
    .then(user => {
         if (user) {
            return false;
        }
        return true;
})
1reaction
dirkcuyscommented, Jun 9, 2017

@kacepe didn’t work for me - have a look at the code, it specifically checks if the promises were rejected or not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expected validator to return Promise or Observable
In angular Reactive form validation done by using in-built validators which could given in array in 2nd postion, when multiple validators used.
Read more >
Using Custom Async Validators in Angular Reactive Forms
First, let's talk about Validators. ... An AsyncValidatorFn must return either a promise or an Observable of type ValidationErrors .
Read more >
Custom validators/sanitizers - express-validator
Custom validators may return Promises to indicate an async validation (which will be awaited upon), or throw any value/reject a promise to use...
Read more >
Angular Custom Form Validators: Complete Guide
All about custom form validators, including synchronous and asynchronous, ... A validator function returns true if the form field is valid ...
Read more >
Custom Form Validators • Angular - codecraft.tv
A validator in Angular is a function which returns null if a control is valid or an error object if it's invalid. For...
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