View Source Exop.ValidationChecks (Exop v1.4.5)
Provides low-level validation functions:
- check_type/3
- check_required/3
- check_numericality/3
- check_in/3
- check_not_in/3
- check_format/3
- check_length/3
- check_struct/3
- check_func/3
- check_equals/3
- check_exactly/3
- check_allow_nil/3
Summary
Functions
Checks whether a parameter's value exactly equals given value (with type equality).
The alias for check_equals/3
.
Checks whether a parameter's value exactly equals given value (with type equality).
Checks whether an item_name conforms the given format.
Checks whether an item is valid over custom validation function.
Checks whether an item_name is a memeber of a list.
Checks whether a check_item has been provided. Returns a boolean.
Checks an item_name over length constraints.
Checks whether an item_name is not a memeber of a list.
Checks an item_name over numericality constraints.
The alias for check_format/3
.
Checks whether an item_name conforms the given format.
Checks if an item_name presents in params if its required (true).
Checks whether an item is expected structure.
Checks the type of an item_name.
Returns an check_item's value from either a Keyword or a Map by an atom-key.
Types
Functions
@spec check_allow_nil(map(), atom() | String.t(), boolean()) :: true | check_error()
@spec check_equals(map(), atom() | String.t(), any()) :: true | check_error()
Checks whether a parameter's value exactly equals given value (with type equality).
Examples
iex> Exop.ValidationChecks.check_equals(%{a: 1}, :a, 1)
true
@spec check_exactly(map(), atom() | String.t(), any()) :: true | check_error()
The alias for check_equals/3
.
Checks whether a parameter's value exactly equals given value (with type equality).
Examples
iex> Exop.ValidationChecks.check_exactly(%{a: 1}, :a, 1)
true
@spec check_format(map(), atom() | String.t(), Regex.t()) :: true | check_error()
Checks whether an item_name conforms the given format.
Examples
iex> Exop.ValidationChecks.check_format(%{a: "bar"}, :a, ~r/bar/)
true
@spec check_func( map(), atom() | String.t(), ({atom() | String.t(), any()}, map() -> any()) ) :: true | check_error()
Checks whether an item is valid over custom validation function.
Examples
iex> Exop.ValidationChecks.check_func(%{a: 1}, :a, fn({:a, value}, _all_param)-> value > 0 end)
true
iex> Exop.ValidationChecks.check_func(%{a: 1}, :a, fn({:a, _value}, _all_param)-> :ok end)
true
iex> Exop.ValidationChecks.check_func(%{a: 1}, :a, fn({:a, value}, _all_param)-> is_nil(value) end)
%{a: "not valid"}
iex> Exop.ValidationChecks.check_func(%{a: 1}, :a, fn({:a, _value}, _all_param)-> :error end)
%{a: "not valid"}
iex> Exop.ValidationChecks.check_func(%{a: -1}, :a, fn({:a, _value}, _all_param)-> {:error, :my_error} end)
%{a: :my_error}
@spec check_in(map(), atom() | String.t(), list()) :: true | check_error()
Checks whether an item_name is a memeber of a list.
Examples
iex> Exop.ValidationChecks.check_in(%{a: 1}, :a, [1, 2, 3])
true
Checks whether a check_item has been provided. Returns a boolean.
Examples
iex> Exop.ValidationChecks.check_item_present?(%{a: 1, b: 2}, :a)
true
iex> Exop.ValidationChecks.check_item_present?([a: 1, b: 2], :b)
true
iex> Exop.ValidationChecks.check_item_present?(%{a: 1, b: 2}, :c)
false
iex> Exop.ValidationChecks.check_item_present?(%{a: 1, b: nil}, :b)
true
@spec check_length(map(), atom() | String.t(), map()) :: true | [check_error()]
Checks an item_name over length constraints.
Examples
iex> Exop.ValidationChecks.check_length(%{a: "123"}, :a, %{min: 0})
[true]
iex> Exop.ValidationChecks.check_length(%{a: ~w(1 2 3)}, :a, %{in: 2..4})
[true]
iex> Exop.ValidationChecks.check_length(%{a: ~w(1 2 3)}, :a, %{is: 3, max: 4})
[true, true]
@spec check_not_in(map(), atom() | String.t(), list()) :: true | check_error()
Checks whether an item_name is not a memeber of a list.
Examples
iex> Exop.ValidationChecks.check_not_in(%{a: 4}, :a, [1, 2, 3])
true
@spec check_numericality(map(), atom() | String.t(), map()) :: true | check_error()
Checks an item_name over numericality constraints.
Examples
iex> Exop.ValidationChecks.check_numericality(%{a: 3}, :a, %{ equal_to: 3 })
true
iex> Exop.ValidationChecks.check_numericality(%{a: 5}, :a, %{ greater_than_or_equal_to: 3 })
true
iex> Exop.ValidationChecks.check_numericality(%{a: 3}, :a, %{ less_than_or_equal_to: 3 })
true
@spec check_regex(map(), atom() | String.t(), Regex.t()) :: true | check_error()
The alias for check_format/3
.
Checks whether an item_name conforms the given format.
Examples
iex> Exop.ValidationChecks.check_regex(%{a: "bar"}, :a, ~r/bar/)
true
@spec check_required(map(), atom() | String.t(), boolean()) :: true | check_error()
Checks if an item_name presents in params if its required (true).
Examples
iex> Exop.ValidationChecks.check_required(%{}, :some_item, false)
true
iex> Exop.ValidationChecks.check_required([a: 1, b: 2], :a, true)
true
iex> Exop.ValidationChecks.check_required(%{a: 1, b: 2}, :b, true)
true
@spec check_struct(map(), atom() | String.t(), struct()) :: true | check_error()
Checks whether an item is expected structure.
Examples
defmodule SomeStruct1, do: defstruct [:a, :b]
defmodule SomeStruct2, do: defstruct [:b, :c]
Exop.ValidationChecks.check_struct(%{a: %SomeStruct1{}}, :a, %SomeStruct1{})
# true
Exop.ValidationChecks.check_struct(%{a: %SomeStruct1{}}, :a, %SomeStruct2{})
# false
@spec check_subset_of(map(), atom() | String.t(), list()) :: true | check_error()
@spec check_type(map(), atom() | String.t(), atom()) :: true | check_error()
Checks the type of an item_name.
Examples
iex> Exop.ValidationChecks.check_type(%{a: 1}, :a, :integer)
true
iex> Exop.ValidationChecks.check_type(%{a: "1"}, :a, :string)
true
iex> Exop.ValidationChecks.check_type(%{a: nil}, :a, :string)
%{:a => "has wrong type; expected type: string, got: nil"}
Returns an check_item's value from either a Keyword or a Map by an atom-key.
Examples
iex> Exop.ValidationChecks.get_check_item(%{a: 1, b: 2}, :a)
1
iex> Exop.ValidationChecks.get_check_item([a: 1, b: 2], :b)
2
iex> Exop.ValidationChecks.get_check_item(%{a: 1, b: 2}, :c)
nil