View Source Flint.Extensions.EctoValidations (Flint v0.4.0)

Shorthand options for common validations found in Ecto.Changeset

Just passthrough the option for the appropriate validation and this extension will take care of calling the corresponding function from Ecto.Changeset on your data.

Options

Aliases

By default, the following aliases are also available for convenience:

config Flint, aliases: [
  lt: :less_than,
  gt: :greater_than,
  le: :less_than_or_equal_to,
  ge: :greater_than_or_equal_to,
  eq: :equal_to,
  ne: :not_equal_to
]

Example

defmodule Person do
  use Flint.Schema

  embedded_schema do
    field! :first_name, :string,  max: 10, min: 5
    field! :last_name, :string, min: 5, max: 10
    field :favorite_colors, {:array, :string}, subset_of: ["red", "blue", "green"]
    field! :age, :integer, greater_than: 0, less_than: max_age
  end
end
Person.changeset(
  %Person{},
  %{first_name: "Bob", last_name: "Smith", favorite_colors: ["red", "blue", "pink"], age: 101},
  [max_age: 100]
)
#Ecto.Changeset<
  action: nil,
  changes: %{
    age: 101,
    first_name: "Bob",
    last_name: "Smith",
    favorite_colors: ["red", "blue", "pink"]
  },
  errors: [
    first_name: {"should be at least %{count} character(s)",
    [count: 5, validation: :length, kind: :min, type: :string]},
    favorite_colors: {"has an invalid entry", [validation: :subset, enum: ["red", "blue", "green"]]},
    age: {"must be less than %{number}", [validation: :number, kind: :less_than, number: 100]}
  ],
  data: #Person<>,
  valid?: false,
  ...
>

Summary

Functions

Applies validations to each field according to the options passed in the schema specification.

Functions

Link to this function

apply_validations(changeset, bindings \\ [])

View Source

Applies validations to each field according to the options passed in the schema specification.

See the Field Validations section of the README for more information on validation details.