bitovi/canjs — pull request #434 by retro (2013-06-16 23:22:13+00:00)
Title: Nested validations

This pull request implements nested validations for the `can.Observe`. API is backward compatible, but adds new features.

#### Validation of the nested property:

Person.validate('company.name', function(){
return 'company name validated from parent'
})

var person = new Person({company : {name : 'foo'}})

person.errors();

{'company.name' : [
'company name validated from parent'
]}

#### Validation of the nested property in combination with child objects validations:

Person.validate('company.name', function(){
return 'company name validated from parent'
})

Company.validate('name', function(){
return 'validation from company'
})

var person = new Person({company : {name : 'foo'}})

person.errors();

{'company.name' : [
'company name validated from parent',
'validation from company'
]}

company.errors()

{'company.name' : [
'validation from company'
]}

#### Validation of the `can.Observe.List` members:

Person.validate('phoneNumbers.*', function(val, attr){
return val > 2 ? 'number too big' : null;
})

var person = new Person({phoneNumbers : [1,2,3]})

person.attr('phoneNumbers').push(4);

person.errors()

{
"phoneNumbers.2" : ['number too big'],
"phoneNumbers.3" : ['number too big'],
}

#### Validation of the complex paths:

Person.validate('company.phoneNumbers.*.countryCode', function(val, attr){
if(val !== 1 && val !== 385){
return 'wrong country code'
}
})
var person = new Person;

person.attr('company', {
phoneNumbers : [{
number : 12345,
countryCode : 1
},{
number : 12345,
countryCode : 385
},{
number : 12345,
countryCode : 41
},{
number : 12345,
countryCode : 1
}]
});

person.errors()

{
'company.phoneNumbers.2.countryCode' : ['wrong country code']
}