Talos v0.3.2 Talos.Types.MapType View Source

MapType for validation maps

Fields are tuples {key, type, options \ []}:

key - string or atom, key of map

type - Talos defined Type

options:

* `optional`: true/false, if false - there will be error on key missing

For example:


  iex> alias Talos.Types.MapType
  iex> alias Talos.Types.StringType
  iex> alias Talos.Types.ArrayType
  iex> alias Talos.Types.IntegerType
  iex> any_map = %MapType{}
  iex> Talos.valid?(any_map, %{foo: :bar})
  true
  iex> user_params = %MapType{fields: [
  ...>  {"email", %StringType{min_length: 5, max_length: 255, regexp: ~r/.*@.*/}},
  ...>  {"age", %IntegerType{gteq: 18, allow_nil: true}},
  ...>  {"interests", %ArrayType{type: %StringType{}}, optional: true}
  ...> ]}
  iex> Talos.valid?(user_params, %{})
  false
  iex> Talos.valid?(user_params, %{"email" => "bob@gmail.com", "age" => 30})
  true
  iex> Talos.valid?(user_params, %{"email" => "bob@gmail.com", "age" => 30, interests: ["elixir"]})
  true

Link to this section Summary

Functions

Callback implementation for Talos.Types.errors/2.

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

Link to this section Types

Link to this type

field()

View Source
field() :: {any(), struct() | module(), keyword()}
Link to this type

short_field()

View Source
short_field() :: {any(), struct() | module()}
Link to this type

t()

View Source
t() :: %atom(){
  allow_nil: boolean(),
  allow_blank: boolean(),
  fields: [field() | short_field()] | nil
}

Link to this section Functions

Callback implementation for Talos.Types.errors/2.

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