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?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
michallepicki
The changes will be validated and you may see errors after “applying an action” - it can be e.g. when you call
Repotoinsertorupdate, or if you need to you can callapply_action/2yourselfedit: 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
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
reservestruct – they’re expected to be valid as supplied. You’re calling thechangeset/2function with no changes.validate_requiredis the only exception to that forEcto.Changesetsupplied validations.dimitarvp
To have
Ecto.Changesetwork 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
oh, right, I got confused because Phoenix is not showing errors when the action is not set
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
Yeah. First parameter to
castis data you already trust.