Validation In Express-Validator

前端 未结 3 746
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 12:30

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

3条回答
  •  隐瞒了意图╮
    2020-12-29 12:46

    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.

提交回复
热议问题