View Source Dsv.Inclusion (Dsv v0.2.1)
Check if the given value is on the list of possible values. Dsv.Inclusion module provides a function to determine if a value is present in a list.
Summary
Functions
The valid?/2
function evaluates whether a given value is present in a provided list.
The validate/2
function evaluates whether a given value is present in a provided list.
The validate/3
function evaluates whether a given value is present in a provided list.
Functions
The valid?/2
function evaluates whether a given value is present in a provided list.
Parameters
value
- The value to be checked.list
- The list in which the presence of the value is checked.
Returns
A boolean value:
true
ifvalue
is present inlist
.false
ifvalue
is not present inlist
.
Examples
iex> Dsv.Inclusion.valid?("test", ["a", :b, "c", %{a: :b}])
:false
iex> Dsv.Inclusion.valid?("test", ["test", :b, "c", %{a: :b}])
:true
iex> Dsv.Inclusion.valid?(%{a: :b}, ["test", :b, "c", %{a: :b}])
:true
iex> Dsv.Inclusion.valid?(%{a: :b}, ["test", :b, "c", %{a: :b, c: :d}])
:false
iex> Dsv.Inclusion.valid?("test", [])
:false
iex> Dsv.Inclusion.valid?(nil, [nil, 1, 2, 3])
:true
iex> Dsv.Inclusion.valid?(nil, [1, 2, 3])
:false
The validate/2
function evaluates whether a given value is present in a provided list.
Parameters
value
- The value to be checked.list
- The list in which the presence of the value is checked.
Returns
:ok
ifvalue
is present inlist
.{:error, message}
ifvalue
is not present inlist
.
Examples
iex> Dsv.Inclusion.validate("test", ["a", :b, "c", %{a: :b}])
{:error, ~s(Value "test" must be one of the ["a", :b, "c", %{a: :b}])}
iex> Dsv.Inclusion.validate("test", ["test", :b, "c", %{a: :b}])
:ok
iex> Dsv.Inclusion.validate(%{a: :b}, ["test", :b, "c", %{a: :b}])
:ok
iex> Dsv.Inclusion.validate(%{a: :b}, ["test", :b, "c", %{a: :b, c: :d}])
{:error, ~s(Value %{a: :b} must be one of the ["test", :b, "c", %{c: :d, a: :b}])}
iex> Dsv.Inclusion.validate("test", [])
{:error, ~s(Value "test" must be one of the [])}
iex> Dsv.Inclusion.validate(nil, [nil, 1, 2, 3])
:ok
iex> Dsv.Inclusion.validate(nil, [1, 2, 3])
{:error, ~s(Value nil must be one of the [1, 2, 3])}
The validate/3
function evaluates whether a given value is present in a provided list.
Parameters
value
- The value to be checked.list
- The list in which the presence of the value is checked.message
- Custom message that will be returned in case of failure.
Returns
:ok
ifvalue
is present inlist
.{:error, message}
ifvalue
is not present inlist
.
Examples
iex> Dsv.Inclusion.validate("test", ["a", :b, "c", %{a: :b}], "Provided value is not accepted.")
{:error, "Provided value is not accepted."}
iex> Dsv.Inclusion.validate("test", ["test", :b, "c", %{a: :b}], "Provided value is not accepted.")
:ok
iex> Dsv.Inclusion.validate(%{a: :b}, ["test", :b, "c", %{a: :b}], "Provided value is not accepted.")
:ok
iex> Dsv.Inclusion.validate(%{a: :b}, ["test", :b, "c", %{a: :b, c: :d}], "Provided value is not accepted.")
{:error, "Provided value is not accepted."}
iex> Dsv.Inclusion.validate("test", [], "Provided value is not accepted.")
{:error, "Provided value is not accepted."}
iex> Dsv.Inclusion.validate(nil, [nil, 1, 2, 3], "Provided value is not accepted.")
:ok
iex> Dsv.Inclusion.validate(nil, [1, 2, 3], "Provided value is not accepted.")
{:error, "Provided value is not accepted."}