admin管理员组文章数量:1429756
I am trying to validate an array of objects with express-validator.
I have been using the new "wildcard" along with the "custom", to iterate over the array of objects paring keys on the object.
Here is the problem, lets say my object looks like this:
flavors:[
{ name: '', percentage: '0', ratio: '0' },
{ name: 'Strawberry', percentage: '2', ratio: '0' },
{ name: '', percentage: '3', ratio: '0' }
]
How do I only check if "name" exists "if" the percentage > 0?
req.checkBody("flavors","Your recipe has no flavor!").notEmpty();
req.checkBody("flavors.*","Please enter a name for this flavor.").custom(function (value) {
return (!(value.percentage > 0) && !value.name);
});
This works but the "errors" output would be something like:
{ 'flavors[2]': {
location: 'body',
param: 'flavors[2]',
msg: 'Please enter a name for this flavor.',
value: { name: '', percentage: '3', ratio: '0' }
}}
Which makes it hard when displaying in my EJS template.
How do I get the output to look something like this, with the added key?
{ 'flavors[2].name': {
location: 'body',
param: 'flavors[2].name',
msg: 'Please enter a name for this flavor.',
value: { name: '', percentage: '3', ratio: '0' }
}}
Hope someone can help me here, thanks! :-)
I am trying to validate an array of objects with express-validator.
I have been using the new "wildcard" along with the "custom", to iterate over the array of objects paring keys on the object.
Here is the problem, lets say my object looks like this:
flavors:[
{ name: '', percentage: '0', ratio: '0' },
{ name: 'Strawberry', percentage: '2', ratio: '0' },
{ name: '', percentage: '3', ratio: '0' }
]
How do I only check if "name" exists "if" the percentage > 0?
req.checkBody("flavors","Your recipe has no flavor!").notEmpty();
req.checkBody("flavors.*","Please enter a name for this flavor.").custom(function (value) {
return (!(value.percentage > 0) && !value.name);
});
This works but the "errors" output would be something like:
{ 'flavors[2]': {
location: 'body',
param: 'flavors[2]',
msg: 'Please enter a name for this flavor.',
value: { name: '', percentage: '3', ratio: '0' }
}}
Which makes it hard when displaying in my EJS template.
How do I get the output to look something like this, with the added key?
{ 'flavors[2].name': {
location: 'body',
param: 'flavors[2].name',
msg: 'Please enter a name for this flavor.',
value: { name: '', percentage: '3', ratio: '0' }
}}
Hope someone can help me here, thanks! :-)
Share Improve this question asked Jun 30, 2018 at 10:23 Carl SmithCarl Smith 1,6212 gold badges12 silver badges18 bronze badges2 Answers
Reset to default 3This is not natively supported at the moment, but it may be partially available when this issue gets implemented.
For now, with some help from lodash's _.toPath()
, you can achieve it:
req.checkBody('flavors.*.name').custom((name, { req, location, path }) => {
const index = _.toPath(path)[1];
const { percentage } = req[location].flavors[index];
// If percentage is 0, then it's always valid.
return percentage > 0 ? name !== '' : true;
});
For now, can be done this way also
req
.checkBody("flavors","Please enter a name for this flavor.")
.custom(data =>
Array.isArray(data)
&&
data.length
&&
data.every(item => item.name && item.percentage > 0));
I hope it helps :)
本文标签: javascriptExpressValidator 520Validate Wildcard Array of ObjectsCompareStack Overflow
版权声明:本文标题:javascript - Express-Validator 5.2.0 - Validate Wildcard Array of Objects - Compare - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745538735a2662392.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论