Does Ecto.Changeset.cast provide a global configuration for empty_strings_to_nil?
0 reputation · 22 Jan 2024, 11:44 UTC
When processing form data or API requests, Ecto.Changeset.cast/4 allows developers to convert empty strings to nil using the empty_strings_to_nil option. This prevents blank strings from being persisted in database columns where NULL is the preferred representation of missing data.
Currently, this behavior is typically managed on a per-changeset basis. In large applications with numerous schemas, applying this option to every individual changeset increases the risk of inconsistency, where some fields store empty strings and others store nil for the same logical state.
What is the recommended approach for enforcing a project-wide policy on empty string conversion without manually adding the option to every changeset? Is there a supported global configuration within Ecto to standardize this behavior across all casts?