Module

Data.Validation.Semigroup

Package
validation
Repository
purescript/purescript-validation

This module defines an applicative functor for applicative validation.

Applicative validation differs from monadic validation using Either in that it allows us to collect multiple errors using a Semigroup, whereas Either terminates on the first error.

#V Source

newtype V err result

The V functor, used for applicative validation

The Applicative instance collects multiple failures in an arbitrary Semigroup.

For example:

validate :: Person -> V (Array Error) Person
validate person = { first: _, last: _, email: _ }
  <$> validateName person.first
  <*> validateName person.last
  <*> validateEmail person.email

Constructors

Instances

#validation Source

validation :: forall err result r. (err -> r) -> (result -> r) -> V err result -> r

Takes two functions an a V value, if the validation failed the error is applied to the first function, if the validation succeeded the inner value is applied to the second function.

#unV Source

unV :: forall err result r. Warn (Text "\'unV\' is deprecated, use \'validation\' instead") => (err -> r) -> (result -> r) -> V err result -> r

Deprecated previous name of validation.

#invalid Source

invalid :: forall err result. err -> V err result

Fail with a validation error.

#isValid Source

isValid :: forall err result. V err result -> Boolean

Test whether validation was successful or not.

#toEither Source

toEither :: forall err result. V err result -> Either err result

#andThen Source

andThen :: forall err a b. V err a -> (a -> V err b) -> V err b

Apply a function if successful, to enable chaining of validation.

Similar to a monadic bind, except it is inconsistent with Apply - that is, where as apply accumulates failures: apply (invalid x) (invalid y) = invalid (x <> y), andThen has fail-fast semantics: andThen (invalid x) (\_ -> invalid y) = invalid x (>>= would be expected to be consistent).