Skip to content

Conditional validations don't update when values used in condition change #43

@andrewpye

Description

@andrewpye

This behaviour isn't necessarily a bug – I understand why it happens, and what the workaround for it is, but I wanted to flag it for visibility and in case anybody has the time and willingness to work on a solution.

Suppose we have a class like

class MyClass {
  count = 0;
  shouldValidateCount = true;
}

and validations for that class like

{
  count: validateSometimes([
    validateNumber({ gt: 0 })
  ], function() {
    return this.get('shouldValidateCount');
  })
}

Then if we create a changeset around an instance of MyClass (with count set to 0 and shouldValidateCount set to true) using those validations, the following happens:

(async function showValidationGotcha() {
  await changeset.validate();
  console.log(changeset.get('isValid')); // false

  changeset.set('shouldValidateCount', false);
  console.log(changeset.get('isValid')); // false, when really should be true
})();

We can of course work around this by explicitly calling changeset.validate() or changeset.validate('count') whenever shouldValidateCount gets set to a new value, but this is clunky and easily forgotten – and gets hard to manage when an object has a large number of conditional validations.

I'm not familiar with the internals of tracked properties etc., but it'd be awesome if it was possible to tap into a mechanism like that to ensure that the validations for properties with conditions associated with them get recomputed whenever a dependency of that condition is updated 🤔

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions