question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible to validate array of objects?

See original GitHub issue

Say I’ve got an array of rooms, each room requires a room-number, nr of adults, nr of childs. It it currently possible to use dot-syntax to validate for rooms inside array?

req.checkBody(rooms.roomNumber).isEmpty() doesn’t seem to work, which I understand because it probably checks for property roomNumber on the array-object itself. Is there any way to do it instead?

I’m ok with iterating the separate rooms in a loop and checking, but I can’t use req.checkBody for that I belive. I’d need something like validator.checkParam(room).

Any suggestions?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
Jakeiicommented, Apr 9, 2015

You could combine a custom validator with validator.js.

expressValidator = require('express-validator');
validator = require('validator');

app.use(expressValidator({
 customValidators: {
    eachIsEmpty: function(values, prop) {
      return values.every(function(val) {
         return validator.isEmpty(val.prop);
      });
    }
 }   
}));
req.checkBody('rooms').eachIsEmpty('roomNumber');

You could probably improve on this to add an array version for each method programmatically.

1reaction
bryanCoteChangcommented, Aug 21, 2017

Hi! I had a similar problem writing a generic validator for all routes. This seems to be working so far, but please let me know if I’m leaving a gaping hole somewhere:

var escape = require('escape-html');

function validate(input) {
  if (Array.isArray(input)) {
    return input.map(validate);
  }
  
  const inputType = typeof input;
  if (inputType === 'string') {
    return escape(input);
  } else if (inputType !== 'object' || !input) {
    return input;
  }

  var output = {};
  Object.keys(input).forEach((key) => {
    output[key] = validate(input[key]);
  });
  return output;
};

module.exports = {
  validate: validate,
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel validate array of objects - Stack Overflow
You can use array validation of Laravel. eg. $validator = Validator::make($request->all(), [ 'row.*.title' => 'required', 'row.
Read more >
Validating an array of objects - Laracasts
Validating an array of objects. Hello,. I'm making a REST endpoint which sends email, and for each recipient there needs to be both...
Read more >
Validating an object or array of similarly-shaped objects with ...
It is a common requirement for a single API endpoint to accept both a single object and an array of similarly shaped objects....
Read more >
How to Validate an Array of Objects in Laravel - Black Deer Dev
To validate an array of objects in Laravel, you can use the * identifier for each item in an array, and use the...
Read more >
Nested Objects and Arrays - VeeValidate
Similar to objects, you can also nest your values in an array, using square brackets just like how you would do it in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found