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

Add validation messages #45

Closed nicolasazrak closed 8 years ago

nicolasazrak commented 9 years ago

Solves Issue #44

Now you can

var  rules = {
  name: [ new Validator.Assert().Length( { min: 10 }, 'Please complete the name with more than 10 characters' ) ],
  email: [ new Validator.Assert().NotBlank('This email is invalid') ],
  lastname: [new Assert().IsString('The lastname must be a valid string')]
};

var model = {
  name: 'Mike',
  lastname: 'Perez'
};

var constraint = new Constraint(rules)
constraint.check(model).messages
constraint.getErrorMessages(validations)

and get:

{
  name: ['Please complete the name with more than 10 characters'],
  email: ['This email is invalid']
}

I didn't update the documentation but all assertions now have message as the last argument. This solves my problem but I don't know if it covers all posible cases.

nicolasazrak commented 9 years ago

Sorry there are a few test failing and the documentation is outdated. I will be fixing that in the next days, are you interested in this feature?

guillaumepotier commented 9 years ago

Could you please squash your commits into one? Diff is really hard to read with two previous obsolete commits. Thank you

guillaumepotier commented 9 years ago

Oh and careful with CS, I use:

if ( value ) {
for ( value ) { 
while ( value ) { 

it seems it lacks some spaces after your if and for statements :)

guillaumepotier commented 9 years ago

Oh, and last but not least, it seems there are no tests for the i18n method right?

auro1 commented 8 years ago

Status on this one? :-)

nicolasazrak commented 8 years ago

@auro1 I wasn't with much time at that time and couldn't think a great solution. I'm closing the pull request and maybe re implement this later.