Fl4m3Ph03n1x

Fl4m3Ph03n1x

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?

Showing Posts 1 to 6

michallepicki

michallepicki

The changes will be validated and you may see errors after “applying an action” - it can be e.g. when you call Repo to insert or update, or if you need to you can call apply_action/2 yourself

edit: also, if you’re making changes to a struct you would pass the changes in the params map - and those would then get casted and get validated

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.

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.

michallepicki

michallepicki

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

garrison

garrison

Note there are times where it is okay to pass in a struct with fields set, namely when you explicitly don’t want to validate them because they come from your own code. Sometimes I’ll throw in something which is set statically (e.g. Post{type: :something}) or a foreign key that way.

dimitarvp

dimitarvp

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to &lt;%= web_namespace %&gt;.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews