guillaumepotier / validator.js

Powerful objects and strings validation in javascript for Node and the browser
http://validatorjs.org
MIT License
255 stars 39 forks source link

Constraints for String Arrays #23

Closed fernandogmar closed 9 years ago

fernandogmar commented 9 years ago

Hi Guillaume,

I have problems to validate an array of strings. I was trying to use Assert Collection, but this needs as parameter a Constraint, but the Constraint only accepts a valid mapping object :(.

So this is the example:

var Assert = Validator.Assert,
        Constraint = Validator.Constraint,
        validator = new Validator.Validator();

var emails = ['my@email.com', 'this is not an email'];
var email_constraint = new Constraint(new Assert().Email());
var emails_constraint = new Assert().Collection(email_constraint);

var validation_result = validator.validate(emails, emails_constraint);

Is it possible to define a Constraint for Arrays of strings? could it be soon?

Thanks a lot!

fernandogmar commented 9 years ago

And using an assert for the Collection(new Assert().Email()) doesn't work either http://jsfiddle.net/6785byju/

guillaumepotier commented 9 years ago

Hi @fernandogmar,

Thanks for using this lib! :) I see, this is quite frustrating and should be possible to do so functionally speaking. I'll try to have a look today and see if difficult to implement.

fernandogmar commented 9 years ago

Hi @guillaumepotier,

I have just found the 'bug',

Collection returns an object like {0: Violation, 1: Violation} but _validateString is expeciting a Violation instance directly

result = assert[ i ].check( string, group );
if ( result instanceof Violation )
    failures.push( result );

a quick fix thinking out loud could be:

result = assert[ i ].check( string, group );
if ( result !== true)
    failures.push( result );
guillaumepotier commented 9 years ago

Hi,

I might have fixed your problem in latest commit, thanks to your hint! Do you mind to check? :)

Thanks

fernandogmar commented 9 years ago

Thanks @guillaumepotier

It works for me ;)

P.S don't forget to update the validator.js on the 'dist' folder :D, thanks

guillaumepotier commented 9 years ago

Yes, done ! https://github.com/guillaumepotier/validator.js/releases/tag/1.1.0