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

Make illegal state unrepresentable construction validation #830

Closed
wltiii opened this issue Dec 23, 2022 · 8 comments
Closed

Make illegal state unrepresentable construction validation #830

wltiii opened this issue Dec 23, 2022 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@wltiii
Copy link

wltiii commented Dec 23, 2022

Is your feature request related to a problem? Please describe.
It seems there is no capability for construction validation, thus I cannot apply use "illegal states unrepresentable" principles.

Describe the solution you'd like
Say I want a TimeEntry class that has a start and end time. The domain invariant is that end time must be after the start. If it is, then the object constructs. If not, then the object does not construct throwing an error, preferably an exception of my choice.

Describe alternatives you've considered
The only alternatives I see is to not use freezed at all.

Additional context
none

@wltiii wltiii added enhancement New feature or request needs triage labels Dec 23, 2022
@rrousselGit
Copy link
Owner

Are you looking for asserts?

@wltiii
Copy link
Author

wltiii commented Dec 24, 2022 via email

@rrousselGit
Copy link
Owner

Asserts (which indeed aren't enabled in release mode) is generally the way to do that in Dart.

There's currently no alternative using Freezed, yes

@wltiii
Copy link
Author

wltiii commented Dec 24, 2022

First, I wish to thank you for taking the time to answer my question on which is very possibly a holiday weekend for you as it is for me.

Also, I want to make sure you understand I really admire this package. I ran into this issue, have made compromises, but hoped there was something I was missing. I am very much a designer/architect thinker, but love to code. Invariants, in my opinion, in OO languages, should be enforced in the object/class, ideally at construction.

So, I have a few further questions if you don't mind, though there is no rush. I would like to explore some further options on my own. I do believe that I can do as I desire if I use equatable and serializable. Assuming that is true, is there a specific thing that makes what I wish to do impossible, such as union types or pattern matching? Is it the requirements of a certain package you are using that makes what I wish to do not possible?

If what makes this not possible because of code built specifically for the package, would you mind pointing me towards it? I might clone it and look at it myself as time permits, not that I think you have done something wrong.

One possible solution that comes to mind, and likely not the best, would be to create an assert. Let's call it InvariantValidator. This would look like the Assert validator - in my mind. It would inject a method that would automatically get called on construction. If the object is mutable, it would also get called when mutated.

Any thoughts or suggestions you may have would be most appreciated.

Hope you enjoy the weekend!

@rrousselGit
Copy link
Owner

It's not impossible to implement something to support this in Freezed. It's just not there

It's low priority for me. Especially considering with the upcoming Dart features, Freezed probably won't be useful for long

@wltiii
Copy link
Author

wltiii commented Dec 25, 2022

Your last statement tells me I need to do a better job keeping up with roadmap docs.

Thanks for the feedback!

@rrousselGit rrousselGit self-assigned this May 10, 2023
@rrousselGit
Copy link
Owner

I have no plan to work on this, so closing

@martinsellergren
Copy link

Support for this would be very nice. In my mind, the biggest drawback of this package currently.
Workaround:

Person(
  name: 'Martin',
)..validate()

i.e add ..validate() to every constructor and copyWith call, where the validate function will throw if the state is illegal. It's quite inconvenient. Finally, thanks for the great package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants