10

I am using express-validator for validation. I am using mongoose for database, it also has validation built in. I want to know which one should I use?

I also want to know if the validation in express-validator is parallel. Take this code for example:

req.checkBody('email', 'Invalid email').notEmpty().isEmail().isUnique(); req.checkBody('password', 'Invalid possword').notEmpty().len(8, 30); req.checkBody('first_name', 'Invalid first_name').notEmpty().isAlpha(); req.checkBody('last_name', 'Invalid last_name').notEmpty().isAlpha(); req.checkBody('dateofbirth', 'Invalid dateofbirth').notEmpty.isDate(); 

isUnique() is a custom validation method that checks if the email has not already been registered or not, it queries to database to validate so. Though not mentioned in the code above but I also have few other post request where I need to validate multiple fields where database queries will be made in each of them.

So I wanted to know if its possible to run each of the above check method in parallel as that would make it faster and would also me more node like. I obviously will like to use a module for running these in parallel like async. I would also like to know if already these check methods are running in parallel already?

Please help me figure this out? Thanks in advance.

3 Answers 3

10

express-validator is meant to validate input passed by the browser/client; Mongoose's validation is meant to validate newly created documents. Both serve a different purpose, so there isn't a clean-cut answer to which one you should use; you could use both, even.

As for the order of validation: the checks will be performed in series. You could use async.parallel() to make it appear as if the checks are performed in parallel, but in reality they won't be since the checks are synchronous.

EDIT: node-validator (and therefore express-validator) is a string validator. Testing for uniqueness isn't a string operation but operates on your data model, so you shouldn't try to use node-validator for it (in fact, I don't even think you can).

Instead, I would suggest using Mongoose's unique feature to ensure that an e-mail address only occurs once in your database.

Alternatively, use a validator module that supports async operations, like async-validate.

Sign up to request clarification or add additional context in comments.

15 Comments

Why I can't make them work in parallel? I mean if do it async.parallel(), then why will it still be synchronous? It seems to me they are functions which create a error which we check after these validations are done. If I am correct then we can use the parallel and check for error after all validations are done. Please clarify me the probs
async.parallel will only parallelize asynchronous calls (I/O), which the Express-validator-calls aren't. Check out the relevant documentation on how you can handle errors (which is also done after all validations are done).
Is this a problem with express-validator or node-validator? If its node-validator than I think its very difficult to fix it? But if its with express-validator than I hope something can be done.
What 'problem' are you referring to? What should be fixed?
You said that express-validator-calls are not asynchronous. So is it the express-validator or node-validator whose calls are not asynchronous?
|
6

Using both mongodb unique constraint and express-validator will cause a little bit complicated errors processing: you need to analyse errors in different places and translate it to common format for rest response. So another approach will be creating custom express validator which will find record with such value in mongodb:

router.use(expressValidator({ customValidators: { isUsernameAvailable(username) { return new Promise((resolve, reject) => { User.findOne({ username: username }, (err, user) => { if (err) throw err; if(user == null) { resolve(); } else { reject(); } }); }); } } }) ); ... req.checkBody('username', 'Username is required').notEmpty(); req.checkBody('username', 'Username already in use').isUsernameAvailable(); req.asyncValidationErrors().then(() => { ... 

If you need, see total example with this code on my website: https://maketips.net/tip/161/validate-username-available-using-express-validator

Conclusion: express-validator supports asynchronous validation as well so you can perform any data and string validation you need without mixing user validation and low-level db backend validation

Comments

0

There is no way that Express-Validator can check a email address is unique or not, on the other hand MongoDB is not responsible for checking your entered email address is in correct format.

So you must have to use both of them when they are in need because they serve completly diffrent purpose.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.