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

Provide a means for supplying a validator #41

Closed
talios opened this issue Jul 23, 2016 · 6 comments
Closed

Provide a means for supplying a validator #41

talios opened this issue Jul 23, 2016 · 6 comments

Comments

@talios
Copy link
Contributor

talios commented Jul 23, 2016

I was thinking it would be handy to have some means of providing a validator over the ADT values, I often use this feature of Immutables and keep thinking it would be useful here.

Currently, derive4j checks for nullness in its created instances, but it would be nice if we could define something like:

protected|public void validate() {
  this.match(....)
}

or

protected|public static Predicate<MyAdtType> validator = it -> ....;

which if existed, gets called before returning the constructed value.

Thoughts?

@jbgi
Copy link
Member

jbgi commented Jul 23, 2016

I think smart constructors can do what you want (using visibility = Smart). eg. https://github.com/jbgi/Softshake2015Demo/blob/70c23ac39640ef678361c9066d37373dfff1f712/src/main/java/com/github/jbgi/softshake/domain/model/PersonName.java#L85

Would that style suit your needs?

@talios
Copy link
Contributor Author

talios commented Jul 24, 2016

Hard to tell, since whenever I add an @Derive I get errors about a missing make attribute, even if I specify it ( using 0.8.2 ).

Without seeing that that generates hard to say, the problem I have with smart constructors on the abstract class, is then you've got two locations for dealing with things - the pluralized class with get/mod methods etc, and the abstract with constructors.

@jbgi
Copy link
Member

jbgi commented Jul 24, 2016

@talios about the error: could it be because you depends on an older version of the derive4j-annotation artifact? if that is not the problem could you point me to a project or sample test-case that exhibit the problem?

as for validation. I see that generating the validation code would be nice indeed. However, unlike with null, I want them to be safe: generated smart constructors would return something like Validation<E, Adt>. I think it is doable at least for product types. For sum types I am not sure, because one may want to make some constructors 'smart' but not some others. To simplify the API, all constructors of a sum type that take parameters would be smart. Value constructors (without parameters) would be normal ones.

The validation code would be taken from an static instance of the visitor of the ADT that return either aBoolean, an optional error value, or a list of errors.

@talios
Copy link
Contributor Author

talios commented Jul 24, 2016

@jbgi
Copy link
Member

jbgi commented Jul 24, 2016

@talios using the following one Rel works for me:

@Data(value = @Derive(withVisibility = Visibility.Smart),
      flavour = Flavour.Javaslang, arguments = ArgOption.checkedNotNull)

What Smart visibility does is that it makes all constructor and setter/modifier methods package private.

@jbgi
Copy link
Member

jbgi commented Aug 9, 2016

closing in favor of #44

@jbgi jbgi closed this as completed Aug 9, 2016
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