I'm experimenting with Ecto and I have issues with validating data in the changeset()
function.
The Schema
is as follows:
defmodule Barakuda.TestData do
use Ecto.Schema
schema "test_data" do
field :username, :string
field :age, :integer
end
def changeset(data, params \\ %{}) do
data
|> Ecto.Changeset.cast(params, [:username, :age])
|> Ecto.Changeset.validate_required([:username, :age])
end
end
Now lets try with invalid data:
iex(125)> d1=%Barakuda.TestData{id: 1, username: "polo"}
%Barakuda.TestData{__meta__: #Ecto.Schema.Metadata<:built, "test_data">,
age: nil, id: 1, username: "polo"}
iex(126)> Barakuda.TestData.changeset(d1).valid?
false
That's ok as the age
field is missing. The same happens if I remove the username
. Fine!
Now, I add the following line at the end of changeset()
(and, yes, I recompiled):
data
|> Ecto.Changeset.cast(params, [:username, :age])
|> Ecto.Changeset.validate_required([:username, :age])
|> Ecto.Changeset.validate_number(:age, less_than: 20)
This is supposed to be true if age
is strictly less than 20, e.i.: 19, 18, ... and false otherwise. Right? Lets give it a try:
iex(19)> d1=%Barakuda.TestData{id: 1, username: "polo", age: 15}
%Barakuda.TestData{__meta__: #Ecto.Schema.Metadata<:built, "test_data">,
age: 15, id: 1, username: "polo"}
iex(20)> Barakuda.TestData.changeset(d1).valid?
true
which is OK. Yet
iex(130)> d1=%Barakuda.TestData{id: 1, username: "polo", age: 22}
%Barakuda.TestData{__meta__: #Ecto.Schema.Metadata<:built, "test_data">,
age: 22, id: 1, username: "polo"}
iex(131)> Barakuda.TestData.changeset(d1).valid?
true
The same actually happens for other validate_*
functions as for example (with or without count: :codepoints
):
Ecto.Changeset.validate_length(:username, min: 6, count: :codepoints)
So, what am I doing wrong ?
NB: Elixir 1.5.1 and Ecto v2.2.6 (2017-09-30)