def changeset(%User{} = user, attrs) do
user
#mark new data(attrs) and columns to update
|> cast(attrs, [:name, :email, :bio, :age])
#ensure fields present
|> validate_required([:name, :email, :bio])
|> validate_length(:bio, min: 2)
|> validate_format(:email, ~r/@/)
|> validate_inclusion(:age, 18..100)
|> unique_constraint(:email)
end
changeset = Ecto.Changeset.cast(%User{}, %{"age" => "0"}, [:age])
user = Repo.insert!(changeset)
Validations vs Constraints
def changeset(chat_channel, attrs) do
chat_channel
|> cast(attrs, [:type, :user_id, :event_id, :subevent_id, :group_id])
|> validate_required([:type])
|> (fn cs ->
case get_field(cs, :type) do
"event" -> cs |> validate_required([:event_id])
"group" -> cs |> validate_required([:subevent_id, :group_id])
"user" -> cs |> validate_required([:user_id])
_ -> cs
end
end).()
end