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
:greater_than
(Ecto.Changeset.validate_number/3
):less_than
(Ecto.Changeset.validate_number/3
):less_than_or_equal_to
(Ecto.Changeset.validate_number/3
):greater_than_or_equal_to
(Ecto.Changeset.validate_number/3
):equal_to
(Ecto.Changeset.validate_number/3
):not_equal_to
(Ecto.Changeset.validate_number/3
):format
(Ecto.Changeset.validate_format/4
):subset_of
(Ecto.Changeset.validate_subset/4
):in
(Ecto.Changeset.validate_inlusion/4
):not_in
(Ecto.Changeset.validate_exclusion/4
):is
(Ecto.Changeset.validate_length/3
):min
(Ecto.Changeset.validate_length/3
):max
(Ecto.Changeset.validate_length/3
):count
(Ecto.Changeset.validate_length/3
):when
- Let's you define an arbitrary boolean condition on the field which can refer to anyfield
defined above it or itself. NOTE The:when
option will output a generic error on failure, so if verbosity is desired, an advanced validation is more appropriate.
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
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.