is v1.0.0 Is View Source
Fast, extensible and easy to use data structure validation with nested structures support.
Link to this section Summary
Functions
Returns alias associate with given value if any
Returns validator associate with given id if any
Returns given errors normalized
Returns true if data is valid, else false
Validate data with schema and return list of errors
Validate data with schema and return list of errors prepend with path, or internal status if nested
Link to this section Functions
Returns alias associate with given value if any.
Returns validator associate with given id if any.
Returns given errors normalized.
Returns true if data is valid, else false.
Examples
iex> valid?(true, :boolean)
true
iex> valid?(true, boolean: true)
true
iex> valid?(true, boolean: false)
false
iex> valid?(true, or: [:boolean, :atom])
true
iex> valid?(:ok, or: [:boolean, :atom])
true
iex> data = Enum.map(1..10, &(%{
...> a: "a",
...> b: true,
...> c: ["a", "b", false],
...> d: [[1, 2, 3], [4, 5, 6]],
...> index: &1,
...> }))
iex> schema = [list: [map: %{
...> a: :binary,
...> b: :boolean,
...> c: [list: [or: [:binary, :boolean]]],
...> d: [list: [list: :integer]],
...> e: [and: [:optional, :binary]],
...> index: [and: [:integer, in_range: [min: 0]]],
...> }]]
iex> valid?(data, schema)
true
iex> data = Enum.map(1..2, &(%{
...> a: 1,
...> b: "b",
...> c: {"a", "b", false},
...> d: [[1, 2, "3"], [4, false, 6]],
...> e: -1,
...> f: "1234567891011",
...> index: &1 - 10,
...> }))
iex> schema = [list: [map: %{
...> a: :binary,
...> b: :boolean,
...> c: [list: [or: [:binary, :boolean]]],
...> d: [list: [list: :integer]],
...> e: [and: [:optional, :binary]],
...> index: [and: [:integer, in_range: [min: 0]]],
...> }]]
iex> valid?(data, schema)
false
iex> valid?(true, {:boolean, true})
true
iex> valid?(%{a: true}, %{a: :boolean})
true
iex> valid?(%{}, :unknown)
{:error, "Validator :unknown does not exist"}
Validate data with schema and return list of errors.
Examples
iex> validate(true, :boolean)
[]
iex> validate(true, boolean: true)
[]
iex> validate(true, boolean: false)
[{:error, [], "must not be a boolean"}]
iex> validate(true, or: [:boolean, :atom])
[]
iex> validate(:ok, or: [:boolean, :atom])
[]
iex> data = Enum.map(1..10, &(%{
...> a: "a",
...> b: true,
...> c: ["a", "b", false],
...> d: [[1, 2, 3], [4, 5, 6]],
...> index: &1,
...> }))
iex> schema = [list: [map: %{
...> a: :binary,
...> b: :boolean,
...> c: [list: [or: [:binary, :boolean]]],
...> d: [list: [list: :integer]],
...> e: [and: [:optional, :binary]],
...> index: [and: [:integer, in_range: [min: 0]]],
...> }]]
iex> validate(data, schema)
[]
iex> data = Enum.map(1..2, &(%{
...> a: 1,
...> b: "b",
...> c: {"a", "b", false},
...> d: [[1, 2, "3"], [4, false, 6]],
...> e: -1,
...> f: "1234567891011",
...> index: &1 - 10,
...> }))
iex> schema = [list: [map: %{
...> a: :binary,
...> b: :boolean,
...> c: [list: [or: [:binary, :boolean]]],
...> d: [list: [list: :integer]],
...> e: [and: [:optional, :binary]],
...> index: [and: [:integer, in_range: [min: 0]]],
...> }]]
iex> validate(data, schema)
[
{:error, [0, :a], "must be a binary"},
{:error, [0, :b], "must be a boolean"},
{:error, [0, :c], "must be a list"},
{:error, [0, :d, 0, 2], "must be an integer"},
{:error, [0, :d, 1, 1], "must be an integer"},
{:error, [0, :e], "must be a binary"},
{:error, [0, :index], "must at least be 0"},
{:error, [1, :a], "must be a binary"},
{:error, [1, :b], "must be a boolean"},
{:error, [1, :c], "must be a list"},
{:error, [1, :d, 0, 2], "must be an integer"},
{:error, [1, :d, 1, 1], "must be an integer"},
{:error, [1, :e], "must be a binary"},
{:error, [1, :index], "must at least be 0"},
]
iex> validate(%{}, :unknown)
{:error, "Validator :unknown does not exist"}
Validate data with schema and return list of errors prepend with path, or internal status if nested.
Use given path to prepend all errors.
If nested is true will return internal status (like :break
),
else will always return normalized errors.