Ecto.Changeset.validate_length

You're seeing just the function validate_length, go back to Ecto.Changeset module for more information.
Link to this function

validate_length(changeset, field, opts)

View Source

Specs

validate_length(t(), atom(), Keyword.t()) :: t()

Validates a change is a string or list of the given length.

Note that the length of a string is counted in graphemes by default. If using this validation to match a character limit of a database backend, it's likely that the limit ignores graphemes and limits the number of unicode characters. Then consider using the :count option to limit the number of codepoints (:codepoints), or limit the number of bytes (:bytes).

Options

  • :is - the length must be exactly this value
  • :min - the length must be greater than or equal to this value
  • :max - the length must be less than or equal to this value
  • :count - what length to count for string, :graphemes (default), :codepoints or :bytes
  • :message - the message on failure, depending on the validation, is one of:
    • for strings:
      • "should be %{count} character(s)"
      • "should be at least %{count} character(s)"
      • "should be at most %{count} character(s)"
    • for binary:
      • "should be %{count} byte(s)"
      • "should be at least %{count} byte(s)"
      • "should be at most %{count} byte(s)"
    • for lists:
      • "should have %{count} item(s)"
      • "should have at least %{count} item(s)"
      • "should have at most %{count} item(s)"

Examples

validate_length(changeset, :title, min: 3)
validate_length(changeset, :title, max: 100)
validate_length(changeset, :title, min: 3, max: 100)
validate_length(changeset, :code, is: 9)
validate_length(changeset, :topics, is: 2)
validate_length(changeset, :icon, count: :bytes, max: 1024 * 16)