-
Notifications
You must be signed in to change notification settings - Fork 39
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
Add support for polymorphic list validation #59
Labels
Comments
This would be good to have support for. It's probably relatively easy to do, I think some thing(s) are not marked as covariant when they should be. An existing workaround for this is: @Supress("UNCHECKED_CAST")
fun <T: Event> eventValidation = Validation<Event> {
Event::stage required {}
} as Validation<T>
val theaterEventValidation = eventValidation<TheaterEvent> |
dhoepelman
added
good first issue
Good for newcomers
enhancement
New feature or request
labels
May 10, 2024
Ran into a similar issue today. This is is how I solved it: class Canvas(val shapes: List<Shape>)
sealed interface Shape
class Circle(val radius: Int): Shape
class Square(val sideLength: Int): Shape
class ShapeValidation : Validation<Shape> {
private val circleValidation = Validation {
Circle::radius {
minimum(0)
}
}
private val squareValidation = Validation {
Square::sideLength {
minimum(0)
}
}
override fun validate(value: Shape): ValidationResult<Shape> {
return when (value) {
is Circle -> circleValidation(value)
is Square -> squareValidation(value)
}
}
}
val canvasValidation = Validation {
Canvas::shapes onEach {
run(ShapeValidation())
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, I have one use-case that I cannot achieve with the library right now. I have a list of multiple common types and I want to have specific validation for each type. For example:
Currently, if I want to add validation for Subscribe.events I can do this only by validating
Event
fields withI think is going to be very useful if we have something like this
What do you think? Is it possible to achieve similar result? Do you think there would be a better approach?
PS. Konform is great! I love it.
The text was updated successfully, but these errors were encountered: