>我正在使用来自快速验证器的检查来验证用户输入,想知道我是否可以从测试中排除字符?我有
check("profile.about").trim().escape()
它将 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);
}
}
然后像这样使用...
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")
);
我相信这应该可以解决问题...