Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Data.Validation

Validation, the error-accumulating sibling of Result.

Where Result short-circuits on the first Err, Validation collects every error, so checking several independent fields reports all their failures at once instead of only the first. Combine independent validations with validate2 or validation_ap; both concatenate the error lists of whatever failed. Opt-in: not in Base.

Types

Validation

type Validation(e, a) = Valid(a) | Invalid(List(e)) deriving (Eq, Show)

A validated a, or the list of accumulated errors e.

Functions and Values

is_valid

is_valid : forall a b. (Data.Validation.Validation(a, b)) -> Bool

True when the value validated.

is_invalid

is_invalid : forall a b. (Data.Validation.Validation(a, b)) -> Bool

True when there were errors.

map_valid

map_valid : forall e0 a b c. ((c) -> b ! {e0}, Data.Validation.Validation(a, c)) -> Data.Validation.Validation(a, b) ! {e0}

Apply f to a Valid value, leaving accumulated errors untouched.

validation_ap

validation_ap : forall e0 a b c. (Data.Validation.Validation(a, (c) -> b ! {e0}), Data.Validation.Validation(a, c)) -> Data.Validation.Validation(a, b) ! {e0}

Applicative apply: when both sides validate, apply the function; otherwise keep every error from both sides, in order. This is the accumulation.

validation_ap(Invalid(["bad function"]), Invalid(["bad argument"]))
Data.Validation.Invalid([bad function, bad argument])

validate2

validate2 : forall e0 a b c d. ((c, d) -> b ! {e0}, Data.Validation.Validation(a, c), Data.Validation.Validation(a, d)) -> Data.Validation.Validation(a, b) ! {e0}

Combine two validations with the two-argument f, accumulating the errors of whichever failed.

validate2(\(a, b) -> a + b, Valid(1), Invalid(["oops"]))
Data.Validation.Invalid([oops])

validation_or

validation_or : forall a b. (a, Data.Validation.Validation(b, a)) -> a

The Valid value, or d when there were errors.

(validation_or(0, Valid(7)), validation_or(0, Invalid(["bad"])))
(7, 0)

validation_of_result

validation_of_result : forall a b. (Result(a, b)) -> Data.Validation.Validation(b, a)

Turn a Result into a single-error Validation.

result_of_validation

result_of_validation : forall a b. (Data.Validation.Validation(a, b)) -> Result(b, List(a))

Turn a Validation back into a Result, keeping the whole error list on the Err side.

sequence_validation

sequence_validation : forall a b. (List(Data.Validation.Validation(a, b))) -> Data.Validation.Validation(a, List(b))

Collapse a list of validations into a validation of the list, accumulating every error across all of them.

sequence_validation([Valid(1), Invalid(["first"]), Invalid(["second"])])
Data.Validation.Invalid([first, second])