ember-validations
ember-validations copied to clipboard
Validation scopes
It would be really useful if the library offered the ability to specify scopes for a particular set of validations.
Example:
App.User = Ember.Object.extend(Ember.Validations, {
validations: {
scope: {
login: {
password: {
presence: true
}
}
editProfile: {
password: {
presence: true
}
passwordConfirmation: {
presence: true
}
}
}
// Normal, unscoped validations, which always applies.
name: {
presence: true
}
}
});
And then you validate the model like so:
var user = App.User.create({
username: 'user',
password: 'password',
passwordConfirmation: null
});
user.validate(); // true
user.validate('login'); // true
user.validate('editProfile'); // false
I grant that the example is not very plausible (one would not store a password in a model) but it does illustrate the general idea.
Or perhaps it would be better to use an 'options' argument, to allow for future extensions.
user.validate({ scope: 'editProfile' });
It will definitely be useful, and I like the API you're suggesting.
I also agree to use the validate method by passing an options argument instead of just the scope.