dfalling
I try to avoid nullable strings, so I don’t have to handle an extra scenario:
%Email{subject: ""}}
%Email{subject: "Hello"}
%Email{subject: nil}} # not permitted
I thought it was cleaner to allow the string to be whatever length and the empty state is "". This way I never have to first check if it’s nil before interacting with it. But I’ve spent the last few hours fighting with Ecto and Changesets interpretation / transformation of empty strings (eg adding a change of name: "" is transformed to name: nil), so I’m curious what other people do.
Do you allow nil values, and deal with converting nil throughout your application?
Thanks!
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
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
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
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #iex
- #elixirconf-us
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
That’s because Ecto deals with that on a completely different level. Ecto needs to support e.g. url encoded form data, which does only support string values. Therefore it needs to treat empty string and “no input” the exact same way, because the encoding by which the data is sent doesn’t allow differenciating both.
You can customize the behaviour with the
empty_valuesoption onEcto.Changeset.cast/4. The default behaviour makes sense for most use cases, where no submitted value means the value is missing. If you consider""to not be a missing value then use the option to adjust the behaviour.I’d however not consider
""a null value. It’s not the absense of a value, but a string like any other.sodapopcan
To add to @LostKobrakai’s response to this:
Even if you went and used
""to mean empty anyway, you’ll now just be checking for empty string instead instead ofnil, which doesn’t really solve anything. For example, you won’t be able to just dosend_email(to: ""). Email is of course a bit of a strange example since we usually make sure we have a validated email address before it gets into the system, but it applies to any nullable values. When it comes to presenting, in the scenarios where you just want to show a null value as nothing without any special messaging,<%= nil %>works just fine.joey_the_snake
I’ve seen more than one person thrown off by this as well. So the idea that Ecto needs to behave this way is a bit strong. It is a design choice to behave in this manner because many of it users find it to be a good default. But that does throw off/confuse other users.
dfalling
Yeah, though if I want to pass the string to a function, I now need to ensure there’s a base case for
nil. Eg, passing it to some regex match. heex and stringifying options are generally fine- they’ll coerce it to an empty string.dfalling
Agreed. I like the idea of it for fields that always exist but permit empty (I think email subject is the best example for this), but it’s obviously a poor choice for something like
user.homepage… in that case I’d expect either a validated URL ornil.But it does take a fair amount of fighting to treat it differently…
validate_changewill not run fornilvalues.validate_requiredtreats empty strings as missing.castconverts empty strings tonil. So if I want to use empty strings the way I was, I’m really going against the current. This is what made me step back…generally when I’m fighting Ecto, it means I’m using it wrong.sodapopcan
So now your regex has to accommodate for empty string which hides the fact that you’re dealing with a nullable field. A nullable field is a nullable field! Ideally most of your fields aren’t but you don’t want to hide it when they are.
dfalling
Very true.
So I’m guessing the way most people would address this is:
sodapopcan
Ya, which is what Ecto Changesets are for!
The other problem you have with
""for email is that now""is a special case valid email which is going to cause more complicated checks. You either want to have a valid value ornil. That keeps the consuming code simple and mostly focused on the happy path.dfalling
Agreed for something like email address. But for something like
email.subject, an empty value is valid. So by allowing null for that, I get the possibility of two empty values:niland"". That’s really all I was trying to avoid with having some non-null strings. I thought if it can be empty, it’s easier to know it’s always a string, rather than it’s a string OR anil(another type). It would make a counter below a text field easier- I can just toString.length(email.subject), rather thanString.length(email.subject || "").But everyone (and the Ecto fight) is convincing me that I should model the bottom case as null, and deal with the few instances where I need to treat that field as a string.
sodapopcan
Ohhhhh boy ok I gotta admit my brain sort of auto-corrected
EmailtoUserandsubjecttoemail, hahaSo, rewinding a bit, I’m actually not super opposed to subject being non-nullable with a default of
"". It could be argued that an email has three required fields:to,subject, andmessagewhere the latter two have defaults of"". The user isn’t really saying, “I don’t yet know what the subject is,” they are essentially making a conscious decision to enter them as blank (I believe this is where your mind was going). In an email draft thennilwould make sense there, but once it’s sent I think it’s ok to empty subject and message as"".Not taking that draft point into consideration (and it’s a shaky one), you can easily get a blank string like this:
I would enforce this in the db as well.
This is just my thought process, though, some may well disagree with me and I’m not even 100% sure I agree with me here
Basically it’s one of those things I see both arguments for. And I do have
default: ""in my projects but also plenty ofnils. It’s all situational!