Talos v1.0.3 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.ListType
  iex> alias Talos.Types.IntegerType
  iex> alias Talos.Field
  iex> any_map = %MapType{}
  iex> Talos.valid?(any_map, %{foo: :bar})
  true
  iex> user_params = %MapType{fields: [
  ...>  %Field{key: "email", type: %StringType{min_length: 5, max_length: 255, regexp: ~r/.*@.*/}},
  ...>  %Field{key: "age", type: %IntegerType{gteq: 18, allow_nil: true}},
  ...>  %Field{key: "interests", type: %ListType{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

t()

View Source
t() :: %atom(){
  allow_nil: boolean(),
  allow_blank: boolean(),
  fields: [Talos.Field.t()] | nil
}

Link to this section Functions

Callback implementation for Talos.Types.errors/2.

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