Exop.ValidationChecks (Exop v1.4.4) View Source

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

Link to this section 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.

Link to this section Types

Specs

check_error() :: %{required(atom() | String.t()) => String.t()}

Link to this section Functions

Link to this function

check_allow_nil(check_items, item_name, bool)

View Source

Specs

check_allow_nil(map(), atom() | String.t(), boolean()) :: true | check_error()
Link to this function

check_equals(check_items, item_name, check_value)

View Source

Specs

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
Link to this function

check_exactly(check_items, item_name, check_value)

View Source

Specs

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
Link to this function

check_format(check_items, item_name, check)

View Source

Specs

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
Link to this function

check_func(check_items, item_name, check)

View Source

Specs

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}
Link to this function

check_in(check_items, item_name, check_list)

View Source

Specs

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
Link to this function

check_item_present?(check_items, item_name)

View Source

Specs

check_item_present?(map(), atom() | String.t()) :: boolean()

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
Link to this function

check_length(check_items, item_name, checks)

View Source

Specs

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]
Link to this function

check_not_in(check_items, item_name, check_list)

View Source

Specs

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
Link to this function

check_numericality(check_items, item_name, checks)

View Source

Specs

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
Link to this function

check_regex(check_items, item_name, check)

View Source

Specs

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
Link to this function

check_required(check_items, item_name, bool)

View Source

Specs

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
Link to this function

check_struct(check_items, item_name, check)

View Source

Specs

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
Link to this function

check_subset_of(check_items, item_name, check_list)

View Source

Specs

check_subset_of(map(), atom() | String.t(), list()) :: true | check_error()
Link to this function

check_type(check_items, item_name, check)

View Source

Specs

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"}
Link to this function

get_check_item(check_items, item_name)

View Source

Specs

get_check_item(map(), atom() | String.t()) :: any() | 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