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

Validate a computed property or native getter? #474

Closed
andreyfel opened this issue May 14, 2020 · 5 comments
Closed

Validate a computed property or native getter? #474

andreyfel opened this issue May 14, 2020 · 5 comments

Comments

@andreyfel
Copy link
Contributor

Let's say I have an object with 3 boolean fields:

class MyObj {
  isOptionOne;
  isOptionTwo;
  isOptionThree;
}

I want it to be valid when at least one of these 3 fields is true. How do I do it with the changeset validation?

I could make a getter

get isOptionSelected() {
  return this.isOptionOne || this.isOptionTwo || this.isOptionThree;
}

and validate against that new property.

isOptionSelected: validateInclusion({ list: [true] })

But it won't work with the changeset because the changes are in the changeset and when the getter is evaluated, this contains original object.
If I try to define that getter on the Changeset by extending it, its value is just undefined.

Any tips how I can do the desired validation?

@snewcomer
Copy link
Collaborator

@andreyfel Interesting! I'm trying to understand how we could accommodate such as use case. I would have thought extending the changeset would have worked (and perhaps the only viable solution). Happen to have a small reproduction?

@andreyfel
Copy link
Contributor Author

@snewcomer I've added a failing test here: #482.
Changeset doesn't even try to evaluate the computed property.
It only checks changes for a given key and then goes to the original object to look for the value there. https://github.com/validated-changeset/validated-changeset/blob/893f08209f8dcbcc05be12b78970173bb0aee6da/src/index.ts#L785

@andreyfel
Copy link
Contributor Author

@snewcomer here is an attempt to fix this scenario adopted-ember-addons/validated-changeset#54

@andreyfel
Copy link
Contributor Author

andreyfel commented May 28, 2020

@snewcomer I updated the test case and bumped the validated changeset version.
By some reason the test was passing even without bumping validated changeset version. Apparently they don't respect the lock file? Not sure if you consider this as an issue.

I think this PR #482 can be merged to verify the behavior on the level of ember-changeset?

@snewcomer
Copy link
Collaborator

3.5.0! Thanks for all your work!!!

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

2 participants