Allow Custom Validator to return Promise
See original GitHub issueI’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:
- Created 7 years ago
- Comments:5 (1 by maintainers)
Top 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 >
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 Free
Top 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
Why you don’t return
true
orfalse
fromUserService.findOne
promise? I think it should work.@kacepe didn’t work for me - have a look at the code, it specifically checks if the promises were rejected or not.