verify

Based on the decode library, verify provides an easy, composable way to validate data.

Examples

let str = "hello, world"
let verifier = {
   use <- verify.string_min_length(2)
   use <- verify.string_max_length(32)
   verify.finalize()
}

let result = verify.run(str, verifier)
assert result == Ok(str)

Types

A verifier is a value that can be used to test a set of rules against a piece of data passed into it.

Verifiers are easily combined to allow large rule sets to be used for more complex data requirements.

pub opaque type Verifier(t)

Functions

pub fn custom(
  function: fn(a) -> Result(a, List(String)),
  next: fn() -> Verifier(a),
) -> Verifier(a)

Creates a custom verifier. Pass in a function that returns the initial value on success, or a list of error messages on failure.

pub fn finalize() -> Verifier(a)

Finalize a verifier.

pub fn float_divisible_by(
  divisor: Float,
  next: fn() -> Verifier(Float),
) -> Verifier(Float)

Verifies that a float is divisible by a given divisor. May not always work as expected, due to floating point inaccuracies, use at your own discretion.

pub fn float_equal_to(
  compare_to: Float,
  next: fn() -> Verifier(Float),
) -> Verifier(Float)

Verifies that a float is equal to a given value.

pub fn float_max_value(
  value: Float,
  next: fn() -> Verifier(Float),
) -> Verifier(Float)

Verifies that a float is at most a given value.

pub fn float_min_value(
  value: Float,
  next: fn() -> Verifier(Float),
) -> Verifier(Float)

Verifies that a float is at least a given value.

pub fn float_not_equal_to(
  compare_to: Float,
  next: fn() -> Verifier(Float),
) -> Verifier(Float)

Verifies that a float is not equal to a given value.

pub fn float_value_range(
  min_value: Float,
  max_value: Float,
  next: fn() -> Verifier(Float),
) -> Verifier(Float)

Verifies that a float has a value that falls within a given inclusive range of values.

pub fn int_divisible_by(
  divisor: Int,
  next: fn() -> Verifier(Int),
) -> Verifier(Int)

Verifies that an int is divisible by a given divisor.

pub fn int_equal_to(
  compare_to: Int,
  next: fn() -> Verifier(Int),
) -> Verifier(Int)

Verifies that an int is equal to a given value.

pub fn int_max_value(
  value: Int,
  next: fn() -> Verifier(Int),
) -> Verifier(Int)

Verifies that an integer is at most a given value.

pub fn int_min_value(
  value: Int,
  next: fn() -> Verifier(Int),
) -> Verifier(Int)

Verifies that an integer is at least a given value.

pub fn int_not_equal_to(
  compare_to: Int,
  next: fn() -> Verifier(Int),
) -> Verifier(Int)

Verifies that an int is not equal to a given range of values.

pub fn int_value_range(
  min_value: Int,
  max_value: Int,
  next: fn() -> Verifier(Int),
) -> Verifier(Int)

Verifies that an integer has a value that falls within a given inclusive range of values.

pub fn run(
  data: a,
  verifier: Verifier(a),
) -> Result(a, List(String))

Run a verifier against a piece of data, returning a result containing either the data that was passed in, or a list of error messages from failed validations.

pub fn string_allowed_characters(
  characters: List(String),
  next: fn() -> Verifier(String),
) -> Verifier(String)

Verifies that a string contains only a set of allowed characters.

pub fn string_contains(
  substring: String,
  next: fn() -> Verifier(String),
) -> Verifier(String)

Verifies that a string contains a given substring.

pub fn string_disallowed_characters(
  characters: List(String),
  next: fn() -> Verifier(String),
) -> Verifier(String)

Verifies that a string does not contain a set of disallowed characters

pub fn string_does_not_contain(
  substring: String,
  next: fn() -> Verifier(String),
) -> Verifier(String)

Verifies that a string does not contain a given substring.

pub fn string_ends_with(
  end: String,
  next: fn() -> Verifier(String),
) -> Verifier(String)

Verifies that a string ends with a given string.

pub fn string_equal_to(
  compare_to: String,
  next: fn() -> Verifier(String),
) -> Verifier(String)

Verifies that a string is equal to a given string.

pub fn string_exact_length(
  length: Int,
  next: fn() -> Verifier(String),
) -> Verifier(String)

Verifies that a strings length is exactly the given length.

pub fn string_length_range(
  min_length: Int,
  max_length: Int,
  next: fn() -> Verifier(String),
) -> Verifier(String)

Verifies that a strings length falls within a given inclusive range.

pub fn string_max_length(
  length: Int,
  next: fn() -> Verifier(String),
) -> Verifier(String)

Verifies that a strings length is at most the given length. The max length value is inclusive.

pub fn string_min_length(
  length: Int,
  next: fn() -> Verifier(String),
) -> Verifier(String)

Verifies that strings length is at least the given length.

pub fn string_not_empty(
  next: fn() -> Verifier(String),
) -> Verifier(String)

Verifies that a string is not empty.

pub fn string_not_equal_to(
  compare_to: String,
  next: fn() -> Verifier(String),
) -> Verifier(String)

Verifies that a string is not equal to a given string.

pub fn string_starts_with(
  start: String,
  next: fn() -> Verifier(String),
) -> Verifier(String)

Verifies that a string starts with a given string.

Search Document