bind
Binds the ValidationResult to the Raise computation.
You can bind the result of the validation to a Raise
computation:
val validateBookName = Validator<String> { isNotBlank() }
val validateAuthorName = Validator<String> { isNotBlank() }
fun combineBookAndAuthor(bookName: String, authorName: String) = either {
val book = bind(validateBookName(bookName))
val author = bind(validateAuthorName(authorName))
"${book.trim()} by ${author.trim()}"
}
Content copied to clipboard
Non-blank strings returns Either.Right because the validation was successful and the either block could complete:
combineBookAndAuthor(" The Lord of the Rings ", " J. R. R. Tolkien ")
// Returns: Either.Right(value = "The Lord of the Rings by J. R. R. Tolkien")
Content copied to clipboard
Whereas, a single blank string fails the validation and interrupts the either block, returning a Either.Left of constraint violations:
normalizeBookName(" ", " J. R. R. Tolkien ") // Returns: Either.Left<NonEmptySet<ConstraintViolation>>
Content copied to clipboard
Return
The validated value on successful result.