adopted-ember-addons / ember-cp-validations

Ember computed property based validations

Home Page:https://adopted-ember-addons.github.io/ember-cp-validations/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dependentKeys as a function?

erikmellum opened this issue · comments

My dependentKeys are dynamic and can't be known at run time. Is it possible to use a function that returns an array of dependentKeys?

Alternatively, is there a way to force revalidation on validators?

I was able to force this validation to retrigger with some hacking, but surely there is a better way:

// Force a revalidation by changing customFields to a new ember object with the same content
        items.forEach(i => {
          i.set('customFields', EmberObject.create(i.get('customFields')));
        });

I also tried strategies to no avail such as: items[0].validations._validators.customFields[0].validate(items[0].get('customFields'), { dependentKeys: ["model.type", "model.customFields"] }, items[0]) (ugly but this was just for testing purposes).

Last thing I'll add in case it helps, the actual inline validator:

customFields: validator('inline', {
    dependentKeys: ['model.type', 'model.customFields'],
    validate(value, options, model) {
      const fields = getTypeFields(model);
      const errors = [];
      fields.forEach(field => {
        if (field.get('isRequired') && isBlank(model.get(`customFields.${field.get('name')}`))) {
          errors.push(field.get('name'));
        }
      });
      model.set('errorMessages', errors);
      if (Object.values(errors).length) {
        return Object.values(errors).join(', ');
      }
      return true;
    },
  }),