yurko

yurko OP

iex(1)> import Ecto.Type
Ecto.Type
iex(2)> cast(:integer, "3")
{:ok, 3}
iex(3)> cast(:string, 3)
:error

What’s the reason for this behavior?

I had some data I passed to changeset and was sure it will handle the types, though numbers instead of strings (Poison’s “fault”) triggered errors which was not expected so I’d like to know more to be able to adjust my expectations :slight_smile:

First 5 of 5 Posts Switch mode

arvidkahl

arvidkahl

Usually, casting number values to String depends on the base used, as in Integer.to_string. Check the iex help with h Integer.to_string/2 for more information.

Ecto does not cast this value because it is ambiguous.

michalmuskala

michalmuskala

Ecto will cast from strings since when using HTML forms strings is all you get from browsers. Casting to string, on the other hand, is rarely required. If it’s something you need, the best way would probably be with a custom type.

yurko

yurko OP

@arvidkahl there’s also to_string function that uses String.Chars protocol and inspect that casts anything to string though it would not be ambiguous if ecto did it since Ecto.Type.cast is a low level function that is used by changeset’s cast where I just list the fields and expect them to be casted. To get around it I have to cast data before passing it to changeset or use custom Ecto types.

@michalmuskala thanks that’s what I thought. Though it breaks when the data comes from other source (in my case from a channel via Poison), I think casting given data to given type without making further assumptions would be more predictable and consistent.

aselder

aselder

I know this is an old discussion, but I believe it’s worth rethinking. I believe the assumption that the primary method of obtaining input is HTML is faulty. JSON bodies, either with the application acting as an API server or via channel, are an extremely common use case. Since JSON can pass typed data, the conversion from something to a string is needed. Otherwise, everywhere you need to construct a query against a string field, you need to add defensive programming to ensure that a bad query does not cause an exception and a 500 response.

gregvaughn

gregvaughn

I created a custom Ecto.Type that I named Stringable for this very purpose. We take in the same logical data from different sources and have to map things into common Ecto structs. When we want to preserve the source system’s own id, we use a string field, but some source systems give us integer ids. This custom type removed a lot of defensive calls in our code.

The core of the custom Stringable is

  def cast(s) when is_binary(s), do: {:ok, s}

  def cast(val) do
    if String.Chars.impl_for(val) do
      {:ok, to_string(val)}
    else
      {:error, "cannot convert to string"}
    end
  end
— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement