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

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

Open
andrewpye opened this issue Mar 22, 2024 · 0 comments

Comments

@andrewpye
Copy link
Contributor

andrewpye commented Mar 22, 2024

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 🤔

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

1 participant