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’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
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
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
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
- #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.