Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependent keys for custom validators #27

Closed
mitchlloyd opened this issue Sep 14, 2015 · 3 comments
Closed

Dependent keys for custom validators #27

mitchlloyd opened this issue Sep 14, 2015 · 3 comments

Comments

@mitchlloyd
Copy link

Why not let custom validators use fields on the model at their discretion? This would let me support a lot of more "meta" type validations that I have had to deal with.

Looking at https://github.com/offirgolan/ember-cp-validations/blob/v1.1.0/addon/utils/validations-factory.js#L176-L203 I can see that each built-in validator that needs custom dependent keys is hard-coded. Is a general API for this being discussed?

@offirgolan
Copy link
Collaborator

I believe we have two options here. The first is to expand the dependent validator which currently adds a dependent key on dependent.isTruelyValid by also adding dependent to the list.

var Validations = buildValidations({
   x: [ validator('x-custom'), validator('dependent', { on: ['foo', 'bar.x']}) ]
});

The second, is to expose a public api to add custom dependent keys.

var Validations = buildValidations({
   x: validator('x-custom', { dependentKeys: ['foo', 'bar.x'] })
});

@blimmer @stefanpenner thoughts?

@mitchlloyd
Copy link
Author

@offirgolan One thing to keep in mind is that a validator may be dependent on another validator or a property on the model. For instance, the dependent validator uses on to specify a path to another validator and the confirmation validator uses on to specify a path to a _model attribute.

One use case I'm thinking of is "make sure this model has least one hasMany model with this property". The workaround today could be to create a computed property on the model:

hasAtLeastOneFeaturedPost: computed('posts.@each.isFeatured', function() {
  return _.some(this.get('posts'), (p) => p.get('isFeatured'));
}

And then validate that that is true, but it would be nice to be able to express that with just a validator.

var Validations = buildValidations({
  featuredPost: validator('has-some', { collection: 'posts', attribute: 'isFeatured' })
});

@offirgolan
Copy link
Collaborator

Im not sure I follow...

Wouldn't you be able to achieve the same functionality by doing something like this

var Validations = buildValidations({
  featuredPost: validator(function(value, options, model) {
     return _.some(model.get('posts'), (p) => p.get('isFeatured'));
  }, { dependentKeys: ['posts.@each.isFeatured'] })
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants