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