Fl4m3Ph03n1x

Fl4m3Ph03n1x

Changeset is not validating data

Background

I am trying to create a schema with some basic validations. However, when I spin up my app, it looks like the validations are not running.

Code

This is the schema for a reservation.

defmodule MyApp.Reserve do
  @moduledoc """
  Represents a reservation for a guest.
  """

  use Ecto.Schema

  import Ecto.Query

  alias Ecto.Changeset

  schema "reserves" do
    field :guest_id, Ecto.UUID
    field :reserved, :decimal
    field :paid, :decimal
    field :category, Ecto.Enum, values: [:new, :returning]
  end

  def changeset(reserve, params \\ %{}) do
    reserve
    |> Changeset.cast(params, [:guest_id, :reserved, :paid, :category])
    |> Changeset.validate_required([:guest_id, :reserved, :paid, :category])
    |> Changeset.validate_number(:reserved, greater_than_or_equal_to: 0)
    |> Changeset.validate_number(:paid, greater_than_or_equal_to: 10)
    |> Changeset.validate_inclusion(:category, ["new", "returning"])
  end

end

Iex

alias MyApp.Repo
alias MyApp.Reserve

reserve = %Reserve{guest_id: "bcf6dba1-8542-462e-ac16-6cfbe7be4cf6", reserved: 0, paid: -10}

Reserve.changeset(reserve)
#Ecto.Changeset<action: nil, changes: %{}, errors: [], data: #MyApp.Reserve<>, valid?: true, ...>

Question

This should be valid?: false since paid is a negative number. What am I missing?

Marked As Solved

dimitarvp

dimitarvp

To have Ecto.Changeset work as designed you should give it an empty %Reserve{} and then pass parameters in the form of a regular map that contain all the pieces of data you want validated. Or a mix.

Also Liked

LostKobrakai

LostKobrakai

That’s not the issue. Validations run at the time the validation function is called.

Validations generally only validate changes, not the existing data on the reserve struct – they’re expected to be valid as supplied. You’re calling the changeset/2 function with no changes. validate_required is the only exception to that for Ecto.Changeset supplied validations.

michallepicki

michallepicki

oh, right, I got confused because Phoenix is not showing errors when the action is not set

dimitarvp

dimitarvp

Yeah. First parameter to cast is data you already trust.

Last Post!

dimitarvp

dimitarvp

Yeah. First parameter to cast is data you already trust.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement