okay

Types

The different types of error messages, these can be used to later build human readable messages like: The string "John" expected to be longer then 10 characters but was actually 4

pub type Error {
  IsGreater(value: Int, expected: Int)
  IsGreaterOrEqual(value: Int, expected: Int)
  IsLonger(value: String, actual: Int, expected: Int)
  IsLesser(value: Int, expected: Int)
  IsLesserOrEqual(value: Int, expected: Int)
  IsEqual(value: String, expected: String)
  IsIncludedIn(value: String, expected: String)
  IsBool(value: Bool, expected: Bool)
  IsSome
  IsNone(value: String)
}

Constructors

  • IsGreater(value: Int, expected: Int)
  • IsGreaterOrEqual(value: Int, expected: Int)
  • IsLonger(value: String, actual: Int, expected: Int)
  • IsLesser(value: Int, expected: Int)
  • IsLesserOrEqual(value: Int, expected: Int)
  • IsEqual(value: String, expected: String)
  • IsIncludedIn(value: String, expected: String)
  • IsBool(value: Bool, expected: Bool)
  • IsSome
  • IsNone(value: String)

The Okay type is used to store the list of validation errors

pub type Okay {
  Okay(errors: List(ValidationError))
}

Constructors

  • Okay(errors: List(ValidationError))

The ValidationError type consists of the field name and the error

pub type ValidationError {
  ValidationError(field: String, error: Error)
}

Constructors

  • ValidationError(field: String, error: Error)

Functions

pub fn field(
  okay: Okay,
  field: String,
  result: Result(Nil, Error),
) -> Okay

Based on the validation function result, it’ll append the Error to the Okay.errors list

pub fn is_equal(value: a, compare: a) -> Result(Nil, Error)

Checks if two values are equal to each other

Examples

let assert Ok(_) = okay.is_equal("hi", "hi")
let assert Error(_error) = okay.is_equal(1, 2)
pub fn is_false(value: Bool) -> Result(Nil, Error)

Checks if bool is False

Examples

let assert Ok(_) = okay.is_false(False)
let assert Error(_error) = okay.is_false(True)
pub fn is_gt(value: Int, compare: Int) -> Result(Nil, Error)

Checks if one Int is greater than another

Examples

let assert Ok(_) = okay.is_gt(10, 1)
let assert Error(_error) = okay.is_gt(1, 10)
pub fn is_gte(value: Int, compare: Int) -> Result(Nil, Error)

Checks if one Int is greater than or equal to another

Examples

let assert Ok(_) = okay.is_gte(10, 10)
let assert Error(_error) = okay.is_gte(9, 10)
pub fn is_included_in(
  value: a,
  whitelist: List(a),
) -> Result(Nil, Error)

Checks if values is included in the list

Examples

let assert Ok(_) = okay.is_included_in(1, [1, 2])
let assert Error(_error) = okay.is_included_in(3, [1, 2])
pub fn is_longer(
  value: String,
  length: Int,
) -> Result(Nil, Error)

Checks if string is longer than X amount

Examples

let assert Ok(_) = okay.is_longer("hello", 1)
let assert Error(_error) = okay.is_longer("A", 2)
pub fn is_lt(value: Int, compare: Int) -> Result(Nil, Error)

Checks if one Int is less than another

Examples

let assert Ok(_) = okay.is_lt(1, 2)
let assert Error(_error) = okay.is_lt(5, 1)
pub fn is_lte(value: Int, compare: Int) -> Result(Nil, Error)

Checks if one Int is less than or equal to another

Examples

let assert Ok(_) = okay.is_lte(8, 8)
let assert Error(_error) = okay.is_lte(9, 8)
pub fn is_none(value: Option(a)) -> Result(Nil, Error)

Checks if Option is None

Examples

let assert Ok(_) = okay.is_none(option.None)
let assert Error(_error) = okay.is_noe(option.Some(1))
pub fn is_some(value: Option(a)) -> Result(Nil, Error)

Checks if Option is Some

Examples

let assert Ok(_) = okay.is_some(option.Some(1))
let assert Error(_error) = okay.is_some(option.None)
pub fn is_true(value: Bool) -> Result(Nil, Error)

Checks if bool is True

Examples

let assert Ok(_) = okay.is_true(True)
let assert Error(_error) = okay.is_true(False)
pub fn new() -> Okay

Initializes a Okay record with an empty list of errors

pub fn run(okay: Okay) -> Result(Nil, Okay)

Checks if the Okay record contains any errors and responds with a Result

Search Document