Skip to content

Backbone Validation: Nested validation: automatic validation of attributes of Backbone.Model type #109

@Lobon18

Description

@Lobon18

Hello, Mr. Pedersen,

We are building an app with three models: PersonModel, CompanyModel, and AddressModel.

Please, see the attached "models.js" file. Each model has its own validation rules.
We are not pleasant with this:
http://thedersen.com/projects/backbone-validation/#configure-validation-rules-on-the-model

Because it forces us to repeat AddressModel's validation rules inside CompanyModel's and PersonModel's validation rules.

It would be nice if the validation mechanism would be recursive: validate a Model instance
implies validate its validation rules for simple types (String, Number, etc.) and, if an
attribute is of type Backbone.Model or Backbone.Collection, then it's validated again (recursivity is on). So, each validation rules are in its right place: each Model or each Collection has its own rules.

Could you balance to implement my suggestions?
Thanks in advance.

var PersonModel = Backbone.Model.extend({
defaults: {
name: null,//String
age: null,//Integer
email: null,//String
address: null//AddressModel
}
validation: {
name: {
required: true
},
age: {
range: [1, 80]
},
email: {
pattern: 'email'
},
address: {
required: false
}
}
});

var CompanyModel = Backbone.Model.extend({
defaults: {
name: null,//String
location: null//AddressModel
}
validation: {
name: {
required: true
},
location: {
required: true
}
}
});

var AddressModel = Backbone.Model.extend({
defaults: {
street: null,//String
zip: null//String
}
validation: {
street: {
required: true
},
zip: {
length: 4
}
}
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions