Skip to content

notgiorgi/data.validation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Validation library for Algebraic Data Types

data Validation a b = Failure a
                    | Success b

API

Validation type implements Functor and Applicative typeclasses in fantasy-land spec

instance Functor (Validation a)
instance Monoid a => Applicative (Validation a)

additional methods:

fold :: Validation a b ~> (a -> c) -> (b -> c) -> c

isSuccess :: Validation t a ~> bool

fail :: t -> Validation t a

example:

const Person = R.curry(
  daggy.tagged('Person', ['name', 'age'])
)

const checkName = name =>
  name.length > 2
  ? Success(name)
  : Failure(["name should be more then 2 characters long"])

const checkAge = age =>
  age >= 13
  ? Success(age)
  : Failure(["you should be at least 13 years old"])

const lift2 = ctor => x => y => x.map(ctor).ap(y)

const mkPerson = (name, age) => lift2(Person)(name)(age)

mkPerson(
  checkName("George Martin"),
  checkAge(68)
).toString() // Validation.Success(Person("George Martin", 68))

mkPerson(
  checkName("f"),
  checkAge(11),
).toString() // Validation.Failure(["name should be more then 2 characters long", 
             //                     "you should be at least 13 years old"])

Releases

No releases published

Packages

No packages published