Talos v1.10.0 Talos.Types.MapType View Source

MapType for validation maps

custom options:

* `required_any_one`: true/false, if true - validation check if map has any valid field
* `required_groups`: list("key1", "key2", ...), list of keys from required group,
 required group can be simple field or field with dependendencies,
 you need to specify all field from the group to check dependencies

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
* `depends_on`: list("key1", "key2", ...), list of keys on which field depends,
  field pass validation only if this field and all dependent fields pass validation

For example:


  iex> import Talos
  iex> Talos.valid?(map(), %{foo: :bar})
  true
  iex> user_params = map(fields: [
  ...>  field(key: "email", type: string(min_length: 5, max_length: 255, regexp: ~r/.*@.*/)),
  ...>  field(key: "age", type: integer(gteq: 18, allow_nil: true)),
  ...>  field(key: "interests", type: list(type: string()), 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
  iex> deps = map(fields: [
  ...>  field(
  ...>   key: "lastname",
  ...>   type: string(),
  ...>   depends_on: ["birthdate", "firstname"]
  ...>  ),
  ...>  field(
  ...>   key: "firstname",
  ...>   type: string(),
  ...>   depends_on: ["birthdate", "lastname"]
  ...>  ),
  ...>  field(
  ...>   key: "birthdate",
  ...>   type: string(),
  ...>   depends_on: ["lastname", "firstname"]
  ...>  )
  ...>  ])
  iex> Talos.valid?(deps, %{})
  false
  iex> Talos.valid?(deps, %{"firstname" => "John", "lastname" => "Galt"})
  false
  iex> Talos.valid?(deps, %{"firstname" => "John", "lastname" => "Galt", "birthdate" => "1957-01-01"})
  true

Link to this section Summary

Link to this section Types

Link to this type

t()

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

Link to this section Functions

Callback implementation for Talos.Types.errors/2.

Link to this function

permit(map_type, value)

View Source
permit(t(), any()) :: any()
Link to this function

valid?(module, value)

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

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