Javascript 如果请求主体是单个json数组,如何验证请求主体?
我正在尝试使用Javascript 如果请求主体是单个json数组,如何验证请求主体?,javascript,node.js,express,express-validator,Javascript,Node.js,Express,Express Validator,我正在尝试使用快速验证器验证请求主体。孔主体是单个数组,因此我没有字段名 我正在使用expressvalidator和express版本4的新API 身体看起来像这样: ["item1","item2"] 我的代码: app.post('/mars/:Id/Id', [ check('id') .isLength({ max: 10 }) .body() //try many ways to get the body. most examples i fou
快速验证器
验证请求主体。孔主体是单个数组,因此我没有字段名
我正在使用expressvalidator
和express
版本4的新API
身体看起来像这样:
["item1","item2"]
我的代码:
app.post('/mars/:Id/Id', [
check('id')
.isLength({ max: 10 })
.body() //try many ways to get the body. most examples i found were for the old api
.custom((item) => Array.isArray(item))
],
(req, res, next) => {
const data: string = matchedData(req); //using this method to only pass validated data to the business layer
return controller.mars(data); //id goes in data.id. i expect there should be an data.body once the body is validated too.
}
如何验证正文?我按照文档中的说明进行验证,代码如下: 只需在代码中的expressValidator引用之后声明自定义验证器
app.use(expressValidator());
app.use(expressValidator({
customValidators: {
isArray: function(value) {
return Array.isArray(value);
}
}
}));
之后,您可以这样检查有效性:
req.checkBody('title', 'title é obrigatório').notEmpty();
req.checkBody('media','media must be an array').isArray();
if (req.body.constructor === Array) {
req.body[0].employee_fk = tk.employee_id;
}
req.assert('item', 'The body from request must be an array').isArray();
var errors = req.validationErrors();
if (errors) {
var response = { errors: [] };
errors.forEach(function(err) {
response.errors.push(err.msg);
});
return res.status(400).json(response);
}
return next();
};
我在我的项目中使用了3.2.0版本,我可以实现这种行为。
以下是我的请求主体的一个示例:
exports.validateDarrayItem=函数(req、res、next){
{
标题:"富",,
媒体:[1,2,3]
}
另外,如果你不想改变你的回答,我曾经做过这样的验证:
req.checkBody('title', 'title é obrigatório').notEmpty();
req.checkBody('media','media must be an array').isArray();
if (req.body.constructor === Array) {
req.body[0].employee_fk = tk.employee_id;
}
req.assert('item', 'The body from request must be an array').isArray();
var errors = req.validationErrors();
if (errors) {
var response = { errors: [] };
errors.forEach(function(err) {
response.errors.push(err.msg);
});
return res.status(400).json(response);
}
return next();
};
以下是我的请求主体的一个示例:
[{
employeefk: 1,
item: 4
}]
我是按照文档中的说明做的,代码如下: 只需在代码中的expressValidator引用之后声明自定义验证器
app.use(expressValidator());
app.use(expressValidator({
customValidators: {
isArray: function(value) {
return Array.isArray(value);
}
}
}));
之后,您可以这样检查有效性:
req.checkBody('title', 'title é obrigatório').notEmpty();
req.checkBody('media','media must be an array').isArray();
if (req.body.constructor === Array) {
req.body[0].employee_fk = tk.employee_id;
}
req.assert('item', 'The body from request must be an array').isArray();
var errors = req.validationErrors();
if (errors) {
var response = { errors: [] };
errors.forEach(function(err) {
response.errors.push(err.msg);
});
return res.status(400).json(response);
}
return next();
};
我在我的项目中使用了3.2.0版本,我可以实现这种行为。
以下是我的请求主体的一个示例:
exports.validateDarrayItem=函数(req、res、next){
{
标题:"富",,
媒体:[1,2,3]
}
另外,如果你不想改变你的回答,我曾经做过这样的验证:
req.checkBody('title', 'title é obrigatório').notEmpty();
req.checkBody('media','media must be an array').isArray();
if (req.body.constructor === Array) {
req.body[0].employee_fk = tk.employee_id;
}
req.assert('item', 'The body from request must be an array').isArray();
var errors = req.validationErrors();
if (errors) {
var response = { errors: [] };
errors.forEach(function(err) {
response.errors.push(err.msg);
});
return res.status(400).json(response);
}
return next();
};
以下是我的请求主体的一个示例:
[{
employeefk: 1,
item: 4
}]
如果您使用的是ajax,请尝试将数组放入对象中,如下所示:
$.ajax({
type: "POST",
url: url,
data: { arr: ["item1", "item2"] },
success: function (data) {
// process data here
}
});
现在,您可以使用arr
标识符应用验证规则:
const { check, body, validationResult } = require('express-validator/check');
...
app.post('/mars/:Id/Id', [
check('chatId').isLength({ max: 10 }),
body('arr').custom((item) => Array.isArray(item))
], (req, res, next) => {
const data: string = matchedData(req);
return controller.mars(data);
});
如果您使用的是ajax,请尝试将数组放入对象中,如下所示:
$.ajax({
type: "POST",
url: url,
data: { arr: ["item1", "item2"] },
success: function (data) {
// process data here
}
});
现在,您可以使用arr
标识符应用验证规则:
const { check, body, validationResult } = require('express-validator/check');
...
app.post('/mars/:Id/Id', [
check('chatId').isLength({ max: 10 }),
body('arr').custom((item) => Array.isArray(item))
], (req, res, next) => {
const data: string = matchedData(req);
return controller.mars(data);
});
正在使用什么模式验证?我猜如果它使用json模式,那么您可以像
schema={type:'array',items:{type:'string'}那样进行验证
。模式验证库可以是,邮递员也可以使用。例如,我没有设置json模式。我将尝试使用您所说的模式。考虑到这是我第一次使用express validator。不起作用。它说:“名称类型的验证器不存在”它不受支持。我在@ByteArtisan创建了一个改进问题。您正在使用ajax发布数据吗?正在使用什么模式验证?我猜如果它使用json模式,那么您可以像schema={type:'array',items:{type:'string'}那样进行验证
。模式验证库可以是,邮递员也可以使用。例如,我没有设置json模式。我将尝试使用您所说的模式。考虑到这是我第一次使用express validator。不起作用。它说:“名称类型的验证器不存在”它不受支持。我在@ByteArtisan创建了一个需要改进的问题。你在使用ajax发布数据吗?