yurko
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 ![]()
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
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
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
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
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
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
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
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
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
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











First 5 of 5 Posts
arvidkahl
Usually, casting number values to String depends on the base used, as in
Integer.to_string. Check the iex help withh Integer.to_string/2for more information.Ecto does not cast this value because it is ambiguous.
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
@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
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
I created a custom Ecto.Type that I named
Stringablefor 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
Stringableis