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
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
- #elixirconf-us
- #ai
- #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 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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