View Source Exop.Validation (Exop v1.4.5)

Provides high-level functions for a contract validation. The main function is valid?/2 Mostly invokes Exop.ValidationChecks module functions.

Summary

Functions

Checks inner item of the contract param (which is a Map itself) with their own checks.

Checks items of the contract's list param with specified checks.

Validate received params over a contract.

Validate received params over a contract. Accumulate validation results into a list.

Types

@type validation_error() :: {:error, {:validation, map()}}

Functions

Link to this function

check_inner(check_items, item_name, checks)

View Source
@spec check_inner(map() | Keyword.t(), atom() | String.t(), map() | Keyword.t()) ::
  list()

Checks inner item of the contract param (which is a Map itself) with their own checks.

Examples

iex> Exop.Validation.check_inner(%{a: 1}, :a, [b: [type: :atom], c: [type: :string]])
[%{a: "has wrong type"}]

iex> Exop.Validation.check_inner(%{a: []}, :a, [b: [type: :atom], c: [type: :string]])
[[%{"a[:b]" => "is required"}, true], [%{"a[:c]" => "is required"}, true]]

iex> Exop.Validation.check_inner(%{a: %{b: :atom, c: "string"}}, :a, [b: [type: :atom], c: [type: :string]])
[[true, true], [true, true]]
Link to this function

check_list_item(check_items, item_name, checks)

View Source
@spec check_list_item(map() | Keyword.t(), atom() | String.t(), map() | Keyword.t()) ::
  list()

Checks items of the contract's list param with specified checks.

Examples

iex> Exop.Validation.check_list_item(%{a: 1}, :a, [type: :integer])
[%{a: "is not a list"}]

iex> Exop.Validation.check_list_item(%{a: []}, :a, [type: :integer])
[]

iex> Exop.Validation.check_list_item(%{a: [1, :atom]}, :a, [type: :integer])
[[true, true], [true, %{"a[1]" => "has wrong type; expected type: integer, got: :atom"}]]

iex> Exop.Validation.check_list_item(%{a: [1, 2]}, :a, [type: :integer])
[[true, true], [true, true]]
@spec errors_message(map()) :: String.t()
Link to this function

valid?(contract, received_params)

View Source
@spec valid?([map()], Keyword.t() | map()) :: :ok | validation_error()

Validate received params over a contract.

Examples

iex> Exop.Validation.valid?([%{name: :param, opts: [required: true]}], [param: "hello"])
:ok
Link to this function

validate(list, received_params, result)

View Source
@spec validate([map()], map() | Keyword.t(), list()) :: list()

Validate received params over a contract. Accumulate validation results into a list.

Examples

iex> Exop.Validation.validate([%{name: :param, opts: [required: true, type: :string]}], [param: "hello"], [])
[true, true]