Exop v0.4.6 Exop.ValidationChecks

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

Summary

Functions

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 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

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 item_name from either a Keyword or a Map by an atom-key

Types

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

Functions

check_equals(check_items, item_name, check_value)

Specs

check_equals(Keyword.t | map, atom, 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
check_format(check_items, item_name, check)

Specs

check_format(Keyword.t | map, atom, 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
check_func(check_items, item_name, check)

Specs

check_func(Keyword.t | map, atom, (Keyword.t | map, any -> true | false)) ::
  true |
  check_error

Checks whether an item is valid over custom validation function.

Examples

iex> Exop.ValidationChecks.check_func(%{a: 1}, :a, fn(_contract, param)-> param > 0 end)
true
iex> Exop.ValidationChecks.check_func(%{a: 1}, :a, fn(_contract, param)-> is_nil(param) end)
%{a: "isn't valid"}
iex> Exop.ValidationChecks.check_func(%{a: -1}, :a, fn(_contract, _param)-> {:error, :my_error} end)
%{a: :my_error}
check_in(check_items, item_name, check_list)

Specs

check_in(Keyword.t | map, atom, 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
check_length(check_items, item_name, checks)

Specs

check_length(Keyword.t | map, atom, 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]
check_not_in(check_items, item_name, check_list)

Specs

check_not_in(Keyword.t | map, atom, 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
check_numericality(check_items, item_name, checks)

Specs

check_numericality(Keyword.t | map, atom, 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
check_required(check_items, item_name, bool)

Specs

check_required(Keyword.t | map, atom, 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
check_struct(check_items, item_name, check)

Specs

check_struct(Keyword.t | map, atom, 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
check_type(check_items, item_name, check)

Specs

check_type(Keyword.t | map, atom, 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
get_check_item(check_items, item_name)

Specs

get_check_item(Keyword.t | map, atom) :: any | nil

Returns an item_name 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