ember-validations icon indicating copy to clipboard operation
ember-validations copied to clipboard

Validation scopes

Open ghost opened this issue 12 years ago • 2 comments

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.

ghost avatar Feb 10 '13 08:02 ghost

Or perhaps it would be better to use an 'options' argument, to allow for future extensions.

user.validate({ scope: 'editProfile' });

ghost avatar Feb 10 '13 08:02 ghost

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.

lcoq avatar Feb 13 '13 12:02 lcoq