Talos.Types.EnumType (Talos v1.12.1) View Source

Enum type is used to check value to be one of enumerable

For example:

  iex> import Talos
  iex> genders = enum(members: ["male", "female"])
  iex> Talos.valid?(genders, "male")
  true
  iex> Talos.valid?(genders, "female")
  true
  iex> Talos.valid?(genders, "heli")
  false

Also there can be another Talos types:


  iex> digit_string = %Talos.Types.StringType{regexp: ~r/^\d+$/}
  iex> numbers = %Talos.Types.EnumType{members: [digit_string, %Talos.Types.IntegerType{}]}
  iex> Talos.valid?(numbers, "1")
  true
  iex> Talos.valid?(numbers, 1)
  true
  iex> Talos.valid?(numbers, "One")
  false

Additional parameters:

allow_nil - allows value to be nil

members - list of possible values or TalosTypes

Link to this section Summary

Link to this section Types

Specs

t() :: %{
  __struct__: atom(),
  allow_nil: boolean(),
  members: maybe_improper_list(),
  example_value: any()
}

Link to this section Functions

Link to this function

errors(enum_type, value)

View Source

Specs

errors(t(), any()) :: list()

Callback implementation for Talos.Types.errors/2.

Link to this function

permit(enum_type, value)

View Source

Specs

permit(t(), any()) :: any()

Specs

valid?(t(), any()) :: boolean()

Callback implementation for Talos.Types.valid?/2.