Kurisu

Kurisu

How to validate that a decimal number is an integer in an Ecto changeset?

I’m using the Decimal package to work with a field in my changeset.

field :amount, :decimal

I can validate it with validate_number so that it should be greater or equal to a given number.

But for now I would like the user to submit an integer for this field (:amount).
I do not want to change the Ecto type to :integer but just want to have a decimal number with zero as decimal part.

Please any suggestion on how to achieve this with a custom Ecto validator, given that the field is of type :decimal?

Marked As Solved

Aetherus

Aetherus

Maybe you can check if it equals to the rounded version of itself.

defp validate_integer(changeset, field) do
  if integer?(get_field(changeset, field)) do
    changeset
  else
    add_error(changeset, field, "An integer is expected.")
  end
end

defp integer?(decimal) do
  decimal
  |> Decimal.round()
  |> Decimal.equal?(decimal)
end

Also Liked

Kurisu

Kurisu

Thank you @Aetherus!

it works fine. I just had to add one more clause to the integer?/1 function:

defp integer?(nil), do: false

otherwise the Decimal.round/1 function throws an exception when rendering a new empty form.

Aetherus

Aetherus

My pleasure.

I was assuming that the field should not be nil and is validated with Ecto.Changeset.validate_required before piping into this validate_integer. My fault.

al2o3cr

al2o3cr

FWIW, Ecto.Changeset.validate_change would give you the "don’t bother doing this if the value is nil" behavior as well.

Where Next?

Popular in Questions Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement