A handy validation library made on top of Data.Validation from Folktale.
const isValid = greaterThan(5)
isValid(10) // true
isValid(2) // false
const validateNum = is(greaterThan(5), "Must be greater than 5")
validateNum(10) // Validation.Success(10)
validateNum(2) // Validation.Failure(["Must be greater than 5"])
const validateNum = all(
is(number(), "Invalid number")
is(greaterThan(5), "Must be greater than 5"),
is(lessThan(15), "Must be less than 15")
)
validateNum(10) // Validation.Success(10)
validateNum(20) // Validation.Failure(["Must be less than 15"])
validateNum("huh?") // Validation.Failure(["Invalid number", "Must be greater than 5", "Must be less than 15"])
const validateLocation = all(
is(notBlank(), "Cannot be blank"),
any(
is(ip4(), "Invalid IP address"),
is(hostname(), "Invalid hostname")
)
)
validateLocation("109.173.208.224") // Validation.Success("109.173.208.224")
validateLocation("500.120.120.120") // Validation.Failure(["Invalid IP address", "Invalid hostname"])
const errors = validate("john-snow", all(
is(notBlank(), "Must not be blank"),
is(alphaNum(), "Must contain only letters and numbers")
))
// ["Must contain only letters and numbers"]
alphaNum :: () -> String -> Bool
Accepts a string containing only Arabic numbers and Latin letters.
email :: () -> String -> Boolean
Accepts a valid email address.
equalsTo :: String -> String -> Boolean
equalsTo :: Number -> Number -> Boolean
Passes if both values are strictly equal.
exactLength :: Number -> String -> Boolean
exactLength :: Number -> [*] -> Boolean
Passes if validated value has exactly a given length.
greaterThan :: Number -> Number -> Boolean
Passes if validated number is greater than a given number.
hostname :: () -> String -> Boolean
Accepts a valid host name.
integer :: () -> String -> Boolean
integer :: () -> Number -> Boolean
Accepts a valid integer value.
ip4 :: () -> String -> Boolean
Accepts a valid IPv4 address.
lessThan :: Number -> Number -> Boolean
Passes if validated number is smaller than a given number.
like :: RegExp -> String -> Boolean
Passes if validated string matches a given regular expression.
longerThan :: Number -> String -> Boolean
longerThan :: Number -> [*] -> Boolean
Passes if validated value is longer than a given length.
notBlank :: () -> String -> Boolean
notBlank :: () -> Number -> Boolean
Passes if validated value is not blank.
number :: () -> String -> Boolean
number :: () -> Number -> Boolean
Accepts a valid number.
shorterThan :: Number -> String -> Boolean
shorterThan :: Number -> [*] -> Boolean
Passes if validated value is shorter than a given length.
strictFalse :: () -> Boolean -> Boolean
Accepts a strict false value.
strictTrue :: () -> Boolean -> Boolean
Accepts a strict false value.
url :: () -> String -> Boolean
Accepts a valid URL.
is :: (v -> Boolean, String) -> v -> Validation[α, β]
Makes a function validating value v
with a given validator function
v -> Boolean
and returns a Folktale's Validation
object
Example:
const isNotBlank = is(notBlank(), "Must not be blank")
isNotBlank("foo") // Validation.Success("foo")
isNotBlank("") // Validation.Failure(["Must not be blank"])
not :: (v -> Boolean) -> v -> Boolean
Makes a function validating value v
with given validator but returning
an opposite result.
const isBlank = not(notBlank());
isBlank("") // true
isBlank("foo") // false
all :: ...Validation[α, β] -> v -> Validation[γ, δ]
Makes a function validating value v
with any number of validators
Validation[α, β]
. Returns Validation.Success
if all of the given
validators pass or Validation.Failure
otherwise.
any :: ...Validation[α, β] -> v -> Validation[γ, δ]
Makes a function validating value v
with any number of validators
Validation[α, β]
. Returns Validation.Success
if any of the given
validators pass or Validation.Failure
otherwise.
validate :: (v, v -> Validation[α, β]) -> undefined|[String]
Validates v
with validator v -> Validation[α, β]
and returns
a list of errors if validation fails or undefined
otherwise.
The MIT License (MIT). Copyright (c) 2017 mirz (mirz.hq@gmail.com)