sheerlox

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 :heart:).

Thanks!

Showing Posts 1 to 4

zachdaniel

zachdaniel

Creator of Ash

Honestly, that was added so long ago its hard to say what the original rationale was :laughing:

  1. Ash strings (by default), cast to nil.
  2. We have the nil_items? constraint, which rejects nil values. For us empty_values is a separate kind of thing, representing when the entire list is empty.
  3. I think what you want is a new constraint, called remove_nil_items? that will remove nil items instead of making them an error if they exist.
sheerlox

sheerlox OP

Adding a remove_nil_items? constraint should be pretty straightforward.

But now that you mention it, it’s also unexcepted behavior that nil values make it through to the database if they are initially empty strings.

To circumvent that issue, I guess the is_nil check 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

zachdaniel

Creator of Ash

I would say that we should move the is_nil check to after applying constraints to the inner items, primarily because types could even theoretically do the reverse, taking a nil item and casting it to a value.

I also wouldn’t personally consider it a breaking change, but rather a bug fix. If nil_items?: false can still produce an array with nil items, that counts as a bug in my book :slight_smile:

Please open an issue for these so we can make sure to address them (or of course a PR :slight_smile: )

— 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
Blokh
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
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
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
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

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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews