validators: Composable validations for your Haskell data types

[ data, library, mit ] [ Propose Tags ]

Composable validations for your Haskell data types.

[Skip to Readme]


Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Versions [RSS] 0.0.1
Change log
Dependencies base (>=4.12 && <5), containers (<1), text (>=1 && <2) [details]
License MIT
Copyright 2019 Luc Tielen
Author Luc Tielen
Category Data
Home page
Uploaded by luc_tielen at 2019-10-23T11:12:11Z
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 545 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for validators-0.0.1

[back to package description]



A library for validating input data in Haskell, inspired partly by elm-validate.



The library provides a Validator datatype for checking assertions on input data. A validator is parametrized by 2 type variables:

  1. err: the type of error a validator can return,
  2. subject: the type being inspected/validated.

Execute validators by using the validate function:

data Error = TooSmall | TooBig

>>> let validator = assert (> 10) [TooSmall]
>>> validate validator 11  -- Success 11
>>> validate validator 4   -- Failure [TooSmall]

Validators return a Validation type as result, which can be further chained or checked for the final result. This is explained in the Validation section.

Validators can also be combined using the Semigroup instance, resulting in a validator that accumulates errors from both sub-validators:

>>> let validator = maxSize 3 [TooBig] <> minSize 2 [TooSmall]
>>> validate validator [1, 2, 3]     -- Success [1,2,3]
>>> validate validator [1, 2, 3, 4]  -- Failure [TooBig]
>>> validate validator [1]           -- Failure [TooSmall]

The library provides helper functions for commonly used validation checks. See the docs for a complete overview which validators are available.


Once a Validator has been executed, it will return a Validation data type. This value will contain either all accumulated errors found during the checking of assertions, or the given input value.

Validations are composed using their Applicative instance. This can be useful for example when validating product types. Like with Validator, this will also accumulate all encountered errors.

The following is an example for validating a Person data type:

data PersonError = MissingName | InvalidAge

type Name = String
type Age = Int
data Person = Person Name Age

>>> let nameValidator = ifEmpty [MissingName]
>>> let ageValidator = assert (> 0) [InvalidAge]
>>> Person <$> validate nameValidator "" <*> validate ageValidator (subtract 1)       -- Failure [MissingName, InvalidAge]
>>> Person <$> validate nameValidator "Alice" <*> validate ageValidator (subtract 1)  -- Failure [InvalidAge]
>>> Person <$> validate nameValidator "Alice" <*> validate ageValidator 25            -- Success (Person "Alice" 25)


The easiest way to start developing is by using Nix.

$ git clone
$ cd validators
$ nix-shell
$ cabal new-configure  # run inside the nix-shell
$ cabal new-build      # run inside the nix-shell

The most often used commands are also provided by a Makefile.