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 badges
Add a ment  | 

2 Answers 2

Reset to default 3

This 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