okay

Types

The different types of failure 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 Failure {
  IsGreaterFailure(value: Int, expected: Int)
  IsGreaterOrEqualFailure(value: Int, expected: Int)
  IsLongerFailure(value: String, actual: Int, expected: Int)
  IsLesserFailure(value: Int, expected: Int)
  IsLesserOrEqualFailure(value: Int, expected: Int)
  IsEqualFailure(value: String, expected: String)
  IsIncludedInFailure(value: String, expected: String)
  IsBoolFailure(value: Bool, expected: Bool)
  IsSomeFailure
  IsNoneFailure(value: String)
  CustomFailure(message: String)
}

Constructors

  • IsGreaterFailure(value: Int, expected: Int)

    Returned when is_gt failed

  • IsGreaterOrEqualFailure(value: Int, expected: Int)

    Returned when is_gte failed

  • IsLongerFailure(value: String, actual: Int, expected: Int)

    Returned when is_longer failed

  • IsLesserFailure(value: Int, expected: Int)

    Returned when is_lt failed

  • IsLesserOrEqualFailure(value: Int, expected: Int)

    Returned when is_lte failed

  • IsEqualFailure(value: String, expected: String)

    Returned when is_equal failed

  • IsIncludedInFailure(value: String, expected: String)

    Returned when is_included_in failed

  • IsBoolFailure(value: Bool, expected: Bool)

    Returned when is_true/is_false failed

  • IsSomeFailure

    Returned when is_some failed

  • IsNoneFailure(value: String)

    Returned when is_none failed

  • CustomFailure(message: String)

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

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

Constructors

  • Okay(failures: List(ValidationError))

The ValidationError type consists of the field name and the error

pub type ValidationError {
  ValidationError(field: String, failure: Failure)
}

Constructors

  • ValidationError(field: String, failure: Failure)

Alias to Result(Nil, Failure) for convenience

pub type ValidationResult =
  Result(Nil, Failure)

Functions

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

Based on the validation function result, it’ll append the Error to the Okay.failures list The field argument is just used as an identifier and will be paired with the error at the end

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

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, Failure)

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, Failure)

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, Failure)

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, Failure)

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, Failure)

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, Failure)

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, Failure)

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, Failure)

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, Failure)

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, Failure)

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 failures

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

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

Search Document