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

Global options support #146

Merged
merged 5 commits into from
Mar 24, 2016
Merged

Global options support #146

merged 5 commits into from
Mar 24, 2016

Conversation

offirgolan
Copy link
Collaborator

If you have specific options you want to propagate throught all your validation rules, you can do so by passing in a global options object.
This is ideal for when you have a dependent key that each validator requires such as the current locale from your i18n implementation, or
you want easily toggle your validations on/off.

const Validations = buildValidations(validationRules, globalOptions);
import Ember from 'ember';
import { validator, buildValidations } from 'ember-cp-validations';
const Validations = buildValidations({
  firstName: {
    description: 'First Name'
    validators: [
      validator('presence', {
        presence: true,
        dependentKeys: ['foo', 'bar']
      })
     ]
   },
  lastName: validator('presence', true)
}, {
  description: 'This field'
  dependentKeys: ['i18n.locale', 'disableValidations'],
  disabled() {
    return this.get('model.disableValidations');
  }
});

Just like in the default options, locale validator options will always take precedence over default options and default options will always take precedence
over global options. This allows you to declare global rules while having the ability to override them in lower levels.
This rule does not apply to dependentKeys, instead they all are merged. In the example above, firstName's dependentKeys will be
['i18n.locale', 'disableValidations', 'foo', 'bar']

@offirgolan
Copy link
Collaborator Author

Im not too sure if this requires a major version upgrade or not since there was a minor api change.

From:

buildOptions(options = {}, defaultOptions = {}})

To:

buildOptions(options = {}, defaultOptions = {}, globalOptions = {})

Which means that anyone that has a custom validator which uses buildOptions will have to make this change.

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

Successfully merging this pull request may close these issues.

1 participant