Johann Pardanaud
12/06/2023, 11:31 AMvalidate(someDataClass)
, which returns a ValidationResult.
I want my users to be able to smoothly incorporate the validation result with Arrow's typed results. Here are the current ideas I had.
toEither()
val result = validate(someDataClass)
val either: Either<ConstraintViolation, SomeDataClass> = result.toEither()
val eitherMessage = either.fold(
ifLeft = { "Failure: $it" },
ifRight = { "Success: $it" },
)
In case of failure, it
is the first ConstraintViolation encountered
In case of success, it
is the value being validated
toEitherNonEmptySet()
val result = validate(someDataClass)
val either: Either<NonEmptySet<ConstraintViolation>, SomeDataClass> = result.toEitherNonEmptySet()
val eitherMessage = either.fold(
ifLeft = { "Failure: $it" },
ifRight = { "Success: $it" },
)
In case of failure, it
is a NonEmptySet<ConstraintViolation>
In case of success, it
is the value being validated
bind()
val either: Either<NonEmptySet<ConstraintViolation>, SomeDataClass> = either {
val result = validate(someDataClass)
bind(result)
}
val eitherMessage = either.fold(
ifLeft = { "Failure: $it" },
ifRight = { "Success: $it" },
)
In case of failure, it
is a NonEmptySet<ConstraintViolation>
In case of success, it
is the value being validated
I'm not a frequent user of Arrow, I would really appreciate what you think about this API.
One thing that already bothers me, is how toEither
defaults to a single violation constraint (unless explicitely using toEitherNonEmptySet
), meanwhile bind
raises multiple violation constraints. What do you think about this?stojan
12/06/2023, 11:40 AMValidationResult
has a Set
of errors in case of failure..... I would expect
fun <A> ValidationResult.toEither(): Either<NonEmptySet<ConstraintViolation>, A> = TODO()
as the only function.
Once you have an Either
you can use the either
syntax normally.....
Next level would be to create your own DSL for ValidationResult
e.g. https://arrow-kt.io/learn/typed-errors/own-error-types/Alejandro Serrano.Mena
12/06/2023, 11:52 AMtoEither()
return NonEmptyList<ConstraintViolation>
Johann Pardanaud
12/06/2023, 11:52 AMJohann Pardanaud
12/06/2023, 11:52 AMNext level would be to create your own DSL forI already read the guide about creating your own DSL, but I'm not sure I see the point?e.g. https://arrow-kt.io/learn/typed-errors/own-error-types/ValidationResult
Alejandro Serrano.Mena
12/06/2023, 11:52 AMtoEither
+ bind
is everything you need to integrate AkkurateJohann Pardanaud
12/06/2023, 11:53 AMAlejandro Serrano.Mena
12/06/2023, 11:54 AMJohann Pardanaud
12/06/2023, 11:55 AMAlejandro Serrano.Mena
12/06/2023, 11:59 AMeither {
... do something ...
val validatedTitle = akkurate(title) {
it.isNotEmpty() otherwise { "Missing title" }
}
}
essentially if would amount to
fun <A> Raise<...> akkurate(subject: A, validator: Validatable<ValueType>.() -> Unit): A =
Validator<A>(validator)(subject).bind()
Johann Pardanaud
12/06/2023, 12:11 PMAlejandro Serrano.Mena
12/06/2023, 12:43 PMJohann Pardanaud
12/06/2023, 12:58 PMAlejandro Serrano.Mena
12/06/2023, 1:31 PMJohann Pardanaud
12/06/2023, 1:33 PM