javascript 断言(req.assert)如何在 nodejs 中工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23415475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does assert (req.assert) work in nodejs
提问by raju
I am currently working on MEAN stack using node, express and angularjs. I downloaded boiler plate code from mean.io and also using debugger while I explore the code.
我目前正在使用 node、express 和 angularjs 处理 MEAN 堆栈。我从 mean.io 下载了样板代码,并在探索代码时使用了调试器。
In the controller which gets req and res as parameters, how does req.assert work?
在以 req 和 res 为参数的控制器中,req.assert 是如何工作的?
In the file server/controllers/users.js
在文件 server/controllers/users.js 中
req.assert('username', 'Username cannot be more than 20 characters').len(1,20);
adds into validation error even when the username is empty or null. How do I check for current username value in the req? Where is the assert function of req defined.
即使用户名为空或为空,也会添加到验证错误中。如何检查请求中的当前用户名值?req 的 assert 函数在哪里定义。
I come from java background and find it tricky to find the function code some times as I wont be sure about where is it defined and how is it prototyped. How does one properly read the objects and browse the functions that are being used in javascript?
我来自 java 背景,发现有时很难找到函数代码,因为我不确定它在哪里定义以及它是如何原型化的。如何正确读取对象并浏览 javascript 中正在使用的函数?
采纳答案by Matt
It's defined in Express's dependency express-validator. Check here: https://github.com/ctavan/express-validator/blob/master/lib/express_validator.js
它在 Express 的依赖 express-validator 中定义。在这里查看:https: //github.com/ctavan/express-validator/blob/master/lib/express_validator.js
which depends on validator: https://github.com/chriso/validator.js
这取决于验证器:https: //github.com/chriso/validator.js
回答by Steve
Accepted answer refers to what is now the Legacy APIhence broken links.
接受的答案是指现在的旧 API,因此链接断开。
Check out the documentation for Sanitizationand the available sanitizing tools. You can make custom validators in there.