Ecto.Validator.Predicates

A handful of predicates to be used in validations.

The examples in this module use the syntax made available via Ecto.Model.Validations in your model.

Source

Summary

absent(field, value, opts \\ [])

Validates the attribute is absent (i.e. nil, an empty list or an empty string)

between(field, value, arg3, opts \\ [])

Validates the given number is between the value. Expects a range as value, raises otherwise

greater_than(field, value, check, opts \\ [])

Validates the given number is greater than the given value. Expects numbers as value, raises otherwise

greater_than_or_equal_to(field, value, check, opts \\ [])

Validates the given number is greater than or equal to the given value. Expects numbers as value, raises otherwise

has_format(field, value, match_on, opts \\ [])

Validates the attribute has a given format. Nil values are not matched against (skipped)

has_length(field, value, match_on, opts \\ [])

Validates the attribute has a given length according to Unicode (i.e. it uses String.length under the scenes). That said, this function should not be used to validate binary fields

less_than(field, value, check, opts \\ [])

Validates the given number is less than the given value. Expects numbers as value, raises otherwise

less_than_or_equal_to(field, value, check, opts \\ [])

Validates the given number is less than or equal to the given value. Expects numbers as value, raises otherwise

member_of(field, value, enum, opts \\ [])

Validates the attribute is member of the given enumerable

not_member_of(field, value, enum, opts \\ [])

Validates the attribute is not a member of the given enumerable

present(field, value, opts \\ [])

Validates the attribute is present (i.e. not nil, nor an empty list nor an empty string)

Types

field :: atom

maybe_error :: nil | binary

Functions

absent(field, value, opts \\ [])

Specs:

Validates the attribute is absent (i.e. nil, an empty list or an empty string).

Options

  • :message - defaults to "must be blank"

Examples

validate user,
  honeypot: absent()
Source
between(field, value, arg3, opts \\ [])

Specs:

Validates the given number is between the value. Expects a range as value, raises otherwise.

Options

  • :message - defaults to "must be between X and Y"

Examples

validates user,
    age: between(18..21)
Source
greater_than(field, value, check, opts \\ [])

Specs:

Validates the given number is greater than the given value. Expects numbers as value, raises otherwise.

Options

  • :message - defaults to "must be greater than X"

Examples

validates user,
    age: greater_than(18)
Source
greater_than_or_equal_to(field, value, check, opts \\ [])

Specs:

Validates the given number is greater than or equal to the given value. Expects numbers as value, raises otherwise.

Options

  • :message - defaults to "must be greater than or equal to X"

Examples

validates user,
    age: greater_than_or_equal_to(18)
Source
has_format(field, value, match_on, opts \\ [])

Specs:

Validates the attribute has a given format. Nil values are not matched against (skipped).

Options

  • :message - defaults to "is invalid"

Examples

validate user,
  email: has_format(~r/@/)
Source
has_length(field, value, match_on, opts \\ [])

Specs:

Validates the attribute has a given length according to Unicode (i.e. it uses String.length under the scenes). That said, this function should not be used to validate binary fields.

The length can be given as a range (indicating min and max), as an integer (indicating exact match) or as keyword options, indicating, min and max values.

Raises if the given argument is not a binary.

Options

  • :too_long - message when the length is too long (defaults to "is too long (maximum is X characters)")
  • :too_short - message when the length is too short (defaults to "is too short (minimum is X characters)")
  • :no_match - message when the length does not match (defaults to "must be X characters")

Examples

validate user,
  password: has_length(6..100)

validate user,
  password: has_length(min: 6, too_short: "requires a minimum length")

validate user,
  code: has_length(3, no_match: "needs to be 3 characters")
Source
less_than(field, value, check, opts \\ [])

Specs:

Validates the given number is less than the given value. Expects numbers as value, raises otherwise.

Options

  • :message - defaults to "must be less than X"

Examples

validates user,
    age: less_than(18)
Source
less_than_or_equal_to(field, value, check, opts \\ [])

Specs:

Validates the given number is less than or equal to the given value. Expects numbers as value, raises otherwise.

Options

  • :message - defaults to "must be less than or equal to X"

Examples

validates user,
    age: less_than_or_equal_to(18)
Source
member_of(field, value, enum, opts \\ [])

Specs:

Validates the attribute is member of the given enumerable.

This validator has the same semantics as calling Enum.member?/2 with the given enumerable and value.

Nil values are not matched against (skipped).

Options

  • :message - defaults to "is not included in the list"

Examples

validate user,
  gender: member_of(~w(male female other))

validate user,
  age: member_of(0..99)
Source
not_member_of(field, value, enum, opts \\ [])

Specs:

Validates the attribute is not a member of the given enumerable.

This validator has the same semantics as calling not Enum.member?/2 with the given enumerable and value.

Nil values are not matched against (skipped).

Options

  • :message - defaults to "is reserved"

Examples

validate user,
  username: not_member_of(~w(admin superuser))

validate user,
  password: not_member_of([user.username, user.name],
                          message: "cannot be the same as username or first name")
Source
present(field, value, opts \\ [])

Specs:

Validates the attribute is present (i.e. not nil, nor an empty list nor an empty string).

Options

  • :message - defaults to "can't be blank"

Examples

validate user,
  name: present()
Source