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
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










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.