Open Lysander opened 3 months ago
After some first attempts to implement this, it appears to be harder than thought!
As the benefit is not so huge, we have decided to downgrade its importance. Until then you must craft the delegatingValidation
by yourself - which is not so hard as shown.
In addition to the problem described in #874 there arises another problem with validation in sealed class hiearchies: Using a
ValidatingStore
managing such types!Consider the example hierarchy from #874 shortened for better readability:
Now let us create some
ValidatingStore
for this types in order to manage the domain state of some app:Two problems arises here: the type mismatch and the lack of runtime polymorphism! The type of the provided validation is
Validation<WebFramework, Unit, ComponentValidationMessage>
, butValidation<Product, Unit, ComponentValidationMessage>
ist required in order to satisfy the store's requirements. Even if the type would be accepted - how about thePizza.validation
-Validation? If the object inside the store would change at runtime - which this is all about! - the validation ofWebFramework
would remain!So in order to solve this problem, we must provide some
Validation
-object, that is based uponProduct
and delegates to the appropriate validation based upon the current type!This could be implemented like this:
Now we can provide this validation to the store:
Imagine some state change of the store:
The store will automatically call the validation provided, in this case the delegating one. The latter will recognize that the type is now
Pizza
and will call the providedValidation
within thewhen
-branch.But as this is a common problem, can we provide some tooling support?
I think this is not so hard and possible!
Let us recap, that we support this only for
sealed
hierarchies - that way we can be sure, that we know at compile time every implementation! So we can create at compile time such adelegatingValidation
, that is fully functional.We could create new annotations
@DelegatingValidation
and@Validation
that needs to be applied as follows:This should provide our annotation processor with the following information:
sealed
constraint!)metadata
andmessage
generics of the type sigature of theValidation
-type. (Those need to be the same for all annotated validations)This shoud be sufficient to implement a delegating validation as shown above!
Because of the annotations on the sub-type validation properties, the user is free to choose any name and is not forced to use
validation
. We might consider to allow a custom name-property for the@DelegatingValidation
to override the default namedelegatingValidation
- which is nice imho:@DelegatingValidation(name = "myAwesomeDelegationValidation")
Is something missing? Are the conclusions all valid?