验证在防爆preSS-验证 [英] Validation In Express-Validator

查看:175
本文介绍了验证在防爆preSS-验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用前preSS验证器进行验证。我使用的猫鼬的数据库,它也验证内置的。我想知道我应该使用哪一个呢?

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?

我也想知道,如果在EX preSS-验证验证平行。借此code,例如:

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设置()是一个检查,如果电子邮件尚未注册与否,查询数据库来验证这样一个自定义的验证方法。虽然在code上面没有提到,但我也有一些其他的要求后,我需要验证在这里的数据库查询将在他们每个人进行多个字段。

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.

推荐答案

前preSS-验证是为了验证输入浏览器/客户端传递;猫鼬的验证是为了验证新创建的文档。这两个服务于不同的目的,所以没有一个棱角分明的答案,你应该使用哪一个;你可以同时使用,甚至。

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.

至于验证的顺序:检查将在一系列执行。你可以使用 async.parallel()来使它的显示的仿佛检查并行执行,但在现实中,他们不会是自支票是同步的。

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.

修改节点验证(因此前preSS-验证)是一个字符串验证。的唯一性测试是不是一个字符串操作,但对你的数据模型进行操作,所以你不应该尝试使用节点验证吧(其实我不甚至认为你的可以的)。

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.

这篇关于验证在防爆preSS-验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆