lukejagodzinski / meteor-astronomy-validators

https://atmospherejs.com/jagi/astronomy-validators
MIT License
11 stars 13 forks source link

Validate a list of fields #14

Closed ghost closed 9 years ago

ghost commented 9 years ago

Hey jagi,

Validators provide a way to validate a single field or all of them, but i doesn't look like it's possible to do a list validation. For example, like this :

if(class.validate(['field1', 'field2', 'field3'])) {
    // Do whatever here
}

Right now, i'm using it like this :

if(_.each(fields, function(value) {
    return class.validate(value);
}))

From what i've read from your code, doesn't look like it's possible right now, but maybe i've missed something ?

lukejagodzinski commented 9 years ago

Hi, the problem was already discussed here and was solved. The ability to validate a list of fields will appear in the next release. But for now you can use this workaround:

var validateMany = function(doc, fields) {
  return _.every(fields, function(field) {
    return doc.validate(field);
  });
};

// Usage
var user = new User();
if (validateMany(user, ['name', 'date'])) {
  // Do something
}

Notice, that it uses the _.every function instead of _.each because we want to stop on the first error.

I will probably publish the next version in the next month. I have to make sure that everything works correctly. It was a huge release. I'm finishing creating unit tests. I'm also working on the documentation. I don't want to hurry.

ghost commented 9 years ago

Oh ok, i've really missed the already closed issue on this. Sorry about that. I actually want not to stop on first error, so will it be possible with the new version your building ?

And definitely, take your time, you're building an awesome tool (already awesome actually) !

lukejagodzinski commented 9 years ago

In the new version you have several ways of calling the validate function:

So yes it will be possible :). Thanks for good words :)

ghost commented 9 years ago

Ok, awesome ;)

Thanks a lot for your work again.