Check() Object in an Array

So I am passing an array of objects to the server and would like to use check to make sure the value / patterns are correct. However the only way I can get the check to pass is if I use check(subscriptions, Match.Any);

Here is what the array looks like…

[ 
  { 
	product: { 
		_id: '...',
		sku: 'mens',
		title: 'Mens',
		gender: 'mens'
	},
	quantity: 1,
	gift_wrap: 'yes'
  },
  {
	product: { 
		_id: '...',
		sku: 'womens',
		title: 'Womens',
		gender: 'womens'
	},
	quantity: 1,
	gift_wrap: 'yes'
  }
]

Is there a better way of doing this? I know it’d be better to pass along an _id and then get the collection data, but it is a local collection so I need to pass it along this way, I think.

You can use the Match.Where pattern to customize your check:

var subscriptions = [
  {/* ... */},
  {/* ... */},
  {/* ... */}
];

check(subscriptions, Match.Where(function(subscriptions){
  _.each(subscriptions, function (doc) {
    /* do your checks and return false if there is a problem */
  });
  // return true if there is no problem
  return true;
}));

Here’s further information:

http://docs.meteor.com/#/full/matchpatterns

3 Likes

This works perfectly. Exactly what I was looking for. Thanks again!
:smile:

1 Like

this also worked for me
check( keys, [ mySchema ] );

1 Like

@farid, that worked like a charm - thanks!