Ecto.Validator
Validates a given struct or dict given a set of predicates.
Ecto.Validator.struct(user,
name: present() when on_create?(user),
age: present(message: "must be present"),
age: greater_than(18),
also: validate_other
)
Validations are passed as the second argument in the attribute-predicate
format. Each predicate can be filtered via the when
operator. Note when
here is not limited to only guard expressions.
The predicates above are going to receive the attribute being validated
and its current value as argument. For example, the present
predicate
above is going to be called as:
present(:name, user.name)
present(:age, user.age, message: "must be present")
The validator also handles a special key :also
, which is used to pipe
to predicates without a particular attribute. Instead, such predicates
receive the struct as argument. In this example, validate_other
will
be invoked as:
validate_other(user)
Note all predicates must return a keyword list, with the attribute error as key and the validation message as value.
A handful of predicates can be found at Ecto.Validator.Predicates
.
Summary↑
bin_dict(value, opts) | Validates a given dict, with binary keys, given a set of predicates |
dict(value, opts) | Validates a given dict given a set of predicates |
struct(value, opts) | Validates a given struct given a set of predicates |
Macros
Validates a given dict, with binary keys, given a set of predicates.
Validates a given dict given a set of predicates.
Validates a given struct given a set of predicates.