Pum*_*ath 6 javascript validation node.js express
我正在使用来自express-validator的检查来验证用户输入,并且想知道是否可以从测试中排除字符?我有
check("profile.about").trim().escape()
Run Code Online (Sandbox Code Playgroud)
它将 HTML 中使用的任何字符转换为其等效的 HTML 实体,但如果用户输入撇号,则会导致不良结果。有没有办法从escape()方法中过滤掉撇号?如果没有,没有人对我如何模仿escape()并放弃'. 谢谢
可以立即添加另一个中间件来完成此任务......
unescape-middleware.js
module.exports = (escaped, char) => function (req, res, next) {
unescapeCharacter(req.params, escaped, char);
unescapeCharacter(req.query, escaped, char);
unescapeCharacter(req.body, escaped, char);
return next();
}
function unescapeCharacter (obj, escaped, char) {
for (const key in obj) {
// Replace the escaped version with the character
obj[key] = obj[key].replace(new Regex(escaped, "g"), char);
}
}
Run Code Online (Sandbox Code Playgroud)
然后像这样使用...
app.js
const { check } = require("express-validator");
const unescape = require("./path/to/unescape/middleware.js");
app.get(
"/some/route",
check("profile.about").trim().escape(),
unescape("'", "'"),
require("./path/to/router/handler.js")
);
Run Code Online (Sandbox Code Playgroud)
我相信这应该可以解决问题......
| 归档时间: |
|
| 查看次数: |
5662 次 |
| 最近记录: |