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
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
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
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
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
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
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
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #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.