exchema v0.2.0 Exchema.Predicates
Exschema default predicates library
Link to this section Summary
Functions
Ensure the value is not in a list of values
Checks against a specific regex format
Just applies the function as if it was a predicate. It also checks for exceptions to allow simpler functions
Ensure the value is in a list of values
Checks against system guards like is_integer
or is_float
Checks whether or not the given value is a struct or a specific struct
Checks the length of the input. You can pass a max, a min, a range or a specific lenght
Checks wheter a value is a list.
It can also check the types of the elemsts of the list by
passing the :element_type
param
Checks a map for its key types, value types or specific value types (for a given key)
Link to this section Functions
Ensure the value is not in a list of values
Examples
iex> Exchema.Predicates.exclusion("apple", ["apple", "banana"])
{:error, :invalid}
iex> Exchema.Predicates.exclusion(5, 1..10)
{:error, :invalid}
iex> Exchema.Predicates.exclusion("horse", ["apple", "banana"])
:ok
Checks against a specific regex format
Examples
iex> Exchema.Predicates.format("starts-with", ~r/^starts-/)
:ok
iex> Exchema.Predicates.format("does-not-starts-with", ~r/^starts-/)
{:error, :invalid}
Just applies the function as if it was a predicate. It also checks for exceptions to allow simpler functions.
Examples
iex> Exchema.Predicates.fun(1, &is_integer/1)
true
iex> Exchema.Predicates.fun("1", &is_integer/1)
false
iex> Exchema.Predicates.fun(1, &(&1 > 0))
true
iex> Exchema.Predicates.fun(0, &(&1 > 0))
false
iex> Exchema.Predicates.fun(1, fn _ -> {:error, :custom_error} end)
{:error, :custom_error}
iex> Exchema.Predicates.fun(1, fn _ -> raise RuntimeError end)
{:error, :thrown}
Ensure the value is in a list of values
Examples
iex> Exchema.Predicates.inclusion("apple", ["apple", "banana"])
:ok
iex> Exchema.Predicates.inclusion(5, 1..10)
:ok
iex> Exchema.Predicates.inclusion("horse", ["apple", "banana"])
{:error, :invalid}
Checks against system guards like is_integer
or is_float
.
Examples
iex> Exchema.Predicates.is(1, :integer)
:ok
iex> Exchema.Predicates.is(1.0, :float)
:ok
iex> Exchema.Predicates.is(1, :nil)
{:error, :not_nil}
iex> Exchema.Predicates.is(1, :atom)
{:error, :not_an_atom}
iex> Exchema.Predicates.is(nil, :binary)
{:error, :not_a_binary}
iex> Exchema.Predicates.is(nil, :bitstring)
{:error, :not_a_bitstring}
iex> Exchema.Predicates.is(nil, :boolean)
{:error, :not_a_boolean}
iex> Exchema.Predicates.is(nil, :float)
{:error, :not_a_float}
iex> Exchema.Predicates.is(nil, :function)
{:error, :not_a_function}
iex> Exchema.Predicates.is(nil, :integer)
{:error, :not_an_integer}
iex> Exchema.Predicates.is(nil, :list)
{:error, :not_a_list}
iex> Exchema.Predicates.is(nil, :map)
{:error, :not_a_map}
iex> Exchema.Predicates.is(nil, :number)
{:error, :not_a_number}
iex> Exchema.Predicates.is(nil, :pid)
{:error, :not_a_pid}
iex> Exchema.Predicates.is(nil, :port)
{:error, :not_a_port}
iex> Exchema.Predicates.is(nil, :reference)
{:error, :not_a_reference}
iex> Exchema.Predicates.is(nil, :tuple)
{:error, :not_a_tuple}
Checks whether or not the given value is a struct or a specific struct.
Note: It’s named is_struct
to avoid conflict with Kernel.struct
.
Examples
iex> Exchema.Predicates.is_struct(%{}, [])
{:error, :not_a_struct}
iex> Exchema.Predicates.is_struct(nil, [])
{:error, :not_a_struct}
Also, keep in mind that many internal types are actually structs
iex> Exchema.Predicates.is_struct(DateTime.utc_now, nil)
:ok
iex> Exchema.Predicates.is_struct(NaiveDateTime.utc_now, nil)
:ok
iex> Exchema.Predicates.is_struct(DateTime.utc_now, DateTime)
:ok
iex> Exchema.Predicates.is_struct(DateTime.utc_now, NaiveDateTime)
{:error, :invalid_struct}
iex> Exchema.Predicates.is_struct(NaiveDateTime.utc_now, DateTime)
{:error, :invalid_struct}
Checks the length of the input. You can pass a max, a min, a range or a specific lenght.
Can check length of either lists, strings or tuples.
Examples
iex> Exchema.Predicates.length("123", 3)
:ok
iex> Exchema.Predicates.length([1,2,3], 3)
:ok
iex> Exchema.Predicates.length({1,2,3}, 3)
:ok
iex> Exchema.Predicates.length([1,2,3], min: 2)
:ok
iex> Exchema.Predicates.length([1,2,3], max: 3)
:ok
iex> Exchema.Predicates.length([1,2,3], 2..4)
:ok
iex> Exchema.Predicates.length([1,2,3], min: 2, max: 4)
:ok
iex> Exchema.Predicates.length([1,2,3], min: 4)
{:error, :invalid_length}
iex> Exchema.Predicates.length([1,2,3], max: 2)
{:error, :invalid_length}
iex> Exchema.Predicates.length([1,2,3], min: 1, max: 2)
{:error, :invalid_length}
iex> Exchema.Predicates.length([1,2,3], 2)
{:error, :invalid_length}
iex> Exchema.Predicates.length([1,2,3], 1..2)
{:error, :invalid_length}
Checks wheter a value is a list.
It can also check the types of the elemsts of the list by
passing the :element_type
param.
Examples
iex> Exchema.Predicates.list("", [])
{:error, :not_a_list}
iex> Exchema.Predicates.list([], [])
:ok
iex> Exchema.Predicates.list(["",1,""], element_type: Exchema.Types.Integer)
{:error, {
:nested_errors,
[
{0, [{{Exchema.Predicates, :is}, :integer, :not_an_integer}]},
{2, [{{Exchema.Predicates, :is}, :integer, :not_an_integer}]}
]}
}
iex> Exchema.Predicates.list([1,2,3], element_type: Exchema.Types.Integer)
:ok
Checks a map for its key types, value types or specific value types (for a given key).
Examples
iex> Exchema.Predicates.map("", [])
{:error, :not_a_map}
iex> Exchema.Predicates.map(%{}, [])
:ok
iex > Exchema.Predicates.map(%{1 => "value"}, [key: Exchema.Types.Integer])
:ok
iex > Exchema.Predicates.map(%{"key" => 1}, [values: Exchema.Types.Integer])
:ok
iex > Exchema.Predicates.map(%{"key" => 1}, [keys: Exchema.Types.Integer])
{:error, {
:key_errors,
[{"key", [{{Exchema.Predicates, :is}, :integer, :not_an_integer}]}]
}}
iex > Exchema.Predicates.map(%{1 => "value"}, [values: Exchema.Types.Integer])
{:error, {
:value_errors,
[{"value", [{{Exchema.Predicates, :is}, :integer, :not_an_integer}]}]
}}
iex> Exchema.Predicates.map(%{foo: :bar}, fields: [foo: Exchema.Types.Integer])
{:error, {
:nested_errors,
[{:foo, [{{Exchema.Predicates, :is}, :integer, :not_an_integer}]}]
}}