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.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement