Skip to content

Time to Go Global

Compare
Choose a tag to compare
@offirgolan offirgolan released this 08 Apr 18:40
· 406 commits to main since this release

If you have specific options you want to propagate throughout 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, local 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']

Upgrade Notes

If you have a custom validator and are using the buildOptions hook, please add globalOptions to your parameters to be able to use this feature.

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

To:

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