Vex.Validators.Exclusion (Vex v0.9.2) View Source

Ensure a value is not a member of a list of values.

Options

  • :in: The list.
  • :message: Optional. A custom error message. May be in EEx format and use the fields described in "Custom Error Messages," below.

The list can be provided in place of the keyword list if no other options are needed.

Examples

iex> Vex.Validators.Exclusion.validate(1, [1, 2, 3])
{:error, "must not be one of [1, 2, 3]"}
iex> Vex.Validators.Exclusion.validate(1, [in: [1, 2, 3]])
{:error, "must not be one of [1, 2, 3]"}
iex> Vex.Validators.Exclusion.validate(1, [in: [1, 2, 3], message: "<%= value %> shouldn't be in <%= inspect list %>"])
{:error, "1 shouldn't be in [1, 2, 3]"}
iex> Vex.Validators.Exclusion.validate(4, [1, 2, 3])
:ok
iex> Vex.Validators.Exclusion.validate("a", ~w(a b c))
{:error, ~S(must not be one of ["a", "b", "c"])}
iex> Vex.Validators.Exclusion.validate("a", in: ~w(a b c), message: "must not be abc, talkin' 'bout 123")
{:error, "must not be abc, talkin' 'bout 123"}

Custom Error Messages

Custom error messages (in EEx format), provided as :message, can use the following values:

iex> Vex.Validators.Exclusion.__validator__(:message_fields)
[value: "The bad value", list: "List"]

An example:

iex> Vex.Validators.Exclusion.validate("a", in: ~w(a b c), message: "<%= inspect value %> is a disallowed value")
{:error, ~S("a" is a disallowed value)}

Link to this section Summary

Functions

Callback implementation for c:Vex.Validator.Behaviour.validate/2.

Callback implementation for c:Vex.Validator.Behaviour.validate/3.

Link to this section Functions

Link to this function

validate(value, options)

View Source

Callback implementation for c:Vex.Validator.Behaviour.validate/2.

Link to this function

validate(data, context, options)

View Source

Callback implementation for c:Vex.Validator.Behaviour.validate/3.