sheerlox
I have a Phoenix Live component that renders multiple string inputs to match the {:array, :string} Ash type:
Previously my codebase was using Ecto, and I’ve found a difference in the way empty_values works between the two: in Ecto, all empty values found in empty_values/0 are removed from the list, whereas in Ash the empty_values constraint is matched directly against the list.
My first guess was to declare my resource attribute as follows:
attribute :skills, {:array, :string}, constraints: [empty_values: [[nil], [""]]]
which works fine if the user does not fill the default blank line on the form.
However, if another blank line is added, or if blank lines are mixed with filled ones, they are inserted as nil in the database:
I’m looking for a way to trim these values in the same way I would in Ecto, but from reading the documentation and reading the code, it doesn’t seem to me like it’s possible.
I’d like to know if there’s a motivation behind this design choice, or if a discussion & PR to make that possible could be entertained (if so, I’d be more than happy to contribute to this amazing framework
).
Thanks!
Trending in Questions
Other Trending Topics
Latest Ash Threads
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
- #ai
- #graphql
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex














Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachdaniel
Honestly, that was added so long ago its hard to say what the original rationale was
nil.nil_items?constraint, which rejects nil values. For usempty_valuesis a separate kind of thing, representing when the entire list is empty.remove_nil_items?that will remove nil items instead of making them an error if they exist.sheerlox
Adding a
remove_nil_items?constraint should be pretty straightforward.But now that you mention it, it’s also unexcepted behavior that
nilvalues make it through to the database if they are initially empty strings.To circumvent that issue, I guess the
is_nilcheck would benefit from being rerun after applying the item’s type constraints here:https://github.com/ash-project/ash/blob/v2.21.14/lib/ash/type/type.ex#L704-L713
But that would probably be considered a breaking change, right?
Maybe we should continue that discussion in a GitHub issue or draft PR, let me know!
zachdaniel
I would say that we should move the
is_nilcheck to after applying constraints to the inner items, primarily because types could even theoretically do the reverse, taking anilitem and casting it to a value.I also wouldn’t personally consider it a breaking change, but rather a bug fix. If
nil_items?: falsecan still produce an array with nil items, that counts as a bug in my bookPlease open an issue for these so we can make sure to address them (or of course a PR
)
sheerlox
GitHub links for posterity: