toy

Types

A decoder is a function that takes a Dynamic value and returns a tuple containing the default value of the same type and a Result with the decoded value or a list of errors

pub type Decoder(a) =
  fn(dynamic.Dynamic) -> #(a, Result(a, List(ToyError)))

Contains decoding or validation errors

pub type ToyError {
  ToyError(error: ToyFieldError, path: List(String))
}

Constructors

  • ToyError(error: ToyFieldError, path: List(String))

Each type of error that can be returned by the decoders

pub type ToyFieldError {
  InvalidType(expected: String, found: String)
  Missing(expected: String)
  ValidationFailed(
    check: String,
    expected: String,
    found: String,
  )
}

Constructors

  • InvalidType(expected: String, found: String)
  • Missing(expected: String)
  • ValidationFailed(check: String, expected: String, found: String)

Functions

pub fn bit_array(
  data: Dynamic,
) -> #(BitArray, Result(BitArray, List(ToyError)))

Decode a BitArray

pub fn decode(
  data: Dynamic,
  decoder: fn(Dynamic) -> #(a, Result(a, List(ToyError))),
) -> Result(a, List(ToyError))

Takes a Dynamic value and runs a Decoder on it, returning the result of the decoding process

pub fn decoded(
  value: a,
) -> fn(Dynamic) -> #(a, Result(a, List(ToyError)))

Creates a decoder which directly returns the provided value. It is useful when decoding records.

pub fn user_decoder() {
  use name <- toy.field("name', toy.string)
  toy.decoded(User(:name))
}
pub fn dynamic(data: a) -> #(Dynamic, Result(a, b))

Always decodes the provided value as Dynamic. Error is never returned from this decoder

pub fn fail(
  error: ToyFieldError,
  default: a,
) -> fn(Dynamic) -> #(a, Result(a, List(ToyError)))

Returns a decoder that always fails with the provided error

pub fn field(
  key: a,
  decoder: fn(Dynamic) -> #(b, Result(b, List(ToyError))),
  next: fn(b) -> fn(Dynamic) -> #(c, Result(c, List(ToyError))),
) -> fn(Dynamic) -> #(c, Result(c, List(ToyError)))

Decode a field from a Dynamic value

This function will index into dictionary with any key type, tuples with integer or javascript arrays and objects. The value found under the key will be decoded with the provided decoder.

pub fn user_decoder() {
  use name <- toy.field("name', toy.string)
  toy.decoded(User(:name))
}
pub fn float(
  data: Dynamic,
) -> #(Float, Result(Float, List(ToyError)))

Decode a Float value

pub fn float_max(
  dec: fn(Dynamic) -> #(Float, Result(Float, List(ToyError))),
  maximum: Float,
) -> fn(Dynamic) -> #(Float, Result(Float, List(ToyError)))

Validates that number is less than the provided maximum

Error type: float_max

pub fn float_min(
  dec: fn(Dynamic) -> #(Float, Result(Float, List(ToyError))),
  minimum: Float,
) -> fn(Dynamic) -> #(Float, Result(Float, List(ToyError)))

Validates that number is greater or equal to the provided minimum

Error type: float_min

pub fn float_range(
  dec: fn(Dynamic) -> #(Float, Result(Float, List(ToyError))),
  minimum: Float,
  maximum: Float,
) -> fn(Dynamic) -> #(Float, Result(Float, List(ToyError)))

Validates that the number is withing the provided range [minimum, maximum)

Error type: float_range

pub fn int(data: Dynamic) -> #(Int, Result(Int, List(ToyError)))

Decode an Int value

pub fn int_max(
  dec: fn(Dynamic) -> #(Int, Result(Int, List(ToyError))),
  maximum: Int,
) -> fn(Dynamic) -> #(Int, Result(Int, List(ToyError)))

Validates that number is less than the provided maximum

Error type: int_max

pub fn int_min(
  dec: fn(Dynamic) -> #(Int, Result(Int, List(ToyError))),
  minimum: Int,
) -> fn(Dynamic) -> #(Int, Result(Int, List(ToyError)))

Validates that number is greater or equal to the provided minimum

Error type: int_min

pub fn int_range(
  dec: fn(Dynamic) -> #(Int, Result(Int, List(ToyError))),
  minimum: Int,
  maximum: Int,
) -> fn(Dynamic) -> #(Int, Result(Int, List(ToyError)))

Validates that number is in the provided range: [minimum, maximum)

Error type: int_range

pub fn list(
  item: fn(Dynamic) -> #(a, Result(a, List(ToyError))),
) -> fn(Dynamic) -> #(List(a), Result(List(a), List(ToyError)))

Decode a list of values

pub fn fruits_decoder() {
  toy.list({
    use name <- toy.field("name", toy.string)
    toy.decoded(Fruit(:name))
  })
}
pub fn list_max(
  dec: fn(Dynamic) -> #(List(a), Result(List(a), List(ToyError))),
  maximum: Int,
) -> fn(Dynamic) -> #(List(a), Result(List(a), List(ToyError)))

Validates that the length of the list is less than the provided maximum

Error type: list_max

pub fn list_min(
  dec: fn(Dynamic) -> #(List(a), Result(List(a), List(ToyError))),
  minimum: Int,
) -> fn(Dynamic) -> #(List(a), Result(List(a), List(ToyError)))

Validates that the length of the list is at least the provided number

Error type: list_min

pub fn list_nonempty(
  dec: fn(Dynamic) -> #(List(a), Result(List(a), List(ToyError))),
) -> fn(Dynamic) -> #(List(a), Result(List(a), List(ToyError)))

Validates that the list is not empty (contains at least one element)

Error type: list_nonempty

pub fn map(
  dec: fn(Dynamic) -> #(a, Result(a, List(ToyError))),
  fun: fn(a) -> b,
) -> fn(Dynamic) -> #(b, Result(b, List(ToyError)))

Map the result of the decoder to a new value

pub type Unit {
  Centimeters(Int)
  Milimeters(Int)
}

pub type User {
  User(height: Unit)
}

pub fn user_decoder() {
  use height <- toy.field("height", toy.int |> toy.map(Centimeters))
  toy.decoded(User(:height))
}
pub fn nullable(
  of dec: fn(Dynamic) -> #(a, Result(a, List(ToyError))),
) -> fn(Dynamic) ->
  #(Option(a), Result(Option(a), List(ToyError)))

Creates a new decoder from an existing one, which will return None if the value is null or undefined on javascript, or nil, null, undefined on erlang. Otherwise it will return the result of the provided decoder wrapped in Some

pub fn option(
  of dec: fn(Dynamic) -> #(a, Result(a, List(ToyError))),
) -> fn(Dynamic) ->
  #(Option(a), Result(Option(a), List(ToyError)))

Decodes a gleam Option type. In erlang represented as {ok, Value} or none. In javascript represented as an instance of Some or None classes.

pub fn optional_field(
  key: a,
  decoder: fn(Dynamic) -> #(b, Result(b, List(ToyError))),
  next: fn(Option(b)) ->
    fn(Dynamic) -> #(c, Result(c, List(ToyError))),
) -> fn(Dynamic) -> #(c, Result(c, List(ToyError)))

Decode a field from a Dynamic value

This function will index into dictionary with any key type, tuples with integer or javascript arrays and objects. The value found under the key will be decoded with the provided decoder.

None is returned only if the field is missing. Otherwise the provided decoder is used to decode the value.

pub fn reservation_decoder() {
  use note <- toy.optional_field("note', toy.string)
  toy.decoded(User(:name))
}
pub fn refine(
  dec: fn(Dynamic) -> #(a, Result(a, List(ToyError))),
  fun: fn(a) -> Result(Nil, List(ToyError)),
) -> fn(Dynamic) -> #(a, Result(a, List(ToyError)))

Refine the result of the decoder with a validation function

pub fn user_decoder() {
  use name <- toy.field("name", toy.string |> toy.refine(fn(name) {
    case name {
      "toy" -> Error([toy.ToyError(toy.ValidationFailed("name_taken", "new_name", name), [])])
      _ -> Ok(Nil)
    }
  }))
 toy.decoded(User(:name))
}
pub fn string(
  data: Dynamic,
) -> #(String, Result(String, List(ToyError)))

Decode a String value

pub fn string_email(
  dec: fn(Dynamic) -> #(String, Result(String, List(ToyError))),
) -> fn(Dynamic) -> #(String, Result(String, List(ToyError)))

Validates that the string contains an email address. This is done by checking if the string contains the “@” character.

Error type: string_email

pub fn string_max(
  dec: fn(Dynamic) -> #(String, Result(String, List(ToyError))),
  maximum: Int,
) -> fn(Dynamic) -> #(String, Result(String, List(ToyError)))

Validates that the length of the string is less than the provided number

Error type: string_max

pub fn string_min(
  dec: fn(Dynamic) -> #(String, Result(String, List(ToyError))),
  minimum: Int,
) -> fn(Dynamic) -> #(String, Result(String, List(ToyError)))

Validates that the length of the string is at least the provided number

Error type: string_min

pub fn string_nonempty(
  dec: fn(Dynamic) -> #(String, Result(String, List(ToyError))),
) -> fn(Dynamic) -> #(String, Result(String, List(ToyError)))

Validates that the string contains some characters that are not whitespace.

Error type: string_nonempty

pub fn try_map(
  dec: fn(Dynamic) -> #(a, Result(a, List(ToyError))),
  default: b,
  fun: fn(a) -> Result(b, List(ToyError)),
) -> fn(Dynamic) -> #(b, Result(b, List(ToyError)))

Map the result of the decoder to a new value or return an error

pub fn user_decoder() {
  use name <- toy.field("name", toy.string |> toy.try_map("", fn(name) {
    case name {
      "toy" -> Error([toy.ToyError(toy.ValidationFailed("name_taken", "new_name", name), [])])
      _ -> Ok(string.uppercase(name))
    }
  }))
 toy.decoded(User(:name))
}
Search Document