dbern

dbern

What's in your Ecto ChangesetHelpers module?

Heyo!

I’ve been noticing across my Elixir projects that use Ecto.Changeset that I have some common code between them. I usually have some extra validation functions, functions that conform some values (like cleaning up strings, eg: turning "null" into nil), and functions that help turn the changeset errors into a string readable by the end-user (like in a flash message on the web page if it’s a web app).

Here are some that I have:

  • full_sentence_errors - Turns an Ecto.Changeset with errors into a humanized string. This has lots of options like excluding keys and flattening embedded changesets’ errors.
  • update_cast_error_message - I found myself needing change Ecto’s casting error messages (something like "my_number_field is invalid") into something else like "my_number_field must be a number" .
  • ensure_uuid - Sometimes (like with Arc/Waffle) you need to have the ID field already present on the changeset so you can upload files while you’re inserting a record.
  • ensure_slug - Create a slug from another field, like “name”. This assumes you would have some unique constraints too on that field.
  • downcase_values - If string values are provided, then downcase them.
  • trim_values - if string values are provided, then trim them.
  • validate_url - Just validating that URI.parse(value) works.
  • validate_phone_number - I’ve used ExPhoneNumber for some stricter phone number validations.
  • normalize_phone_number - to keep user-input on phone numbers more consistent, like always using the national format
  • validate_required_if_any - if one of the fields is present, then require all the fields. For example, an address is optional but if you provide the state then you have to provide the rest of the address.
  • validate_required_any_of - One of the provided of fields is required.
  • validate_date_earlier_than - require a datetime field to be earlier than another field or a given datetime

Is there any interest in a library that includes these? Do you have any yourself that might be good to contribute?

(sidenote: ChangesetHelpers is a terrible name; if you have a suggestion on that I’m all ears)

Most Liked

cnck1387

cnck1387

For trimming, I ended up going with a custom ecto type because it’s such a common thing to want. So now just about every time I would use :string in my schema, I end up with a TrimmedString custom type that does the trimming.

Where Next?

Popular in Discussions Top

WolfDan
After doing a port from a c++ library to my project in phoenix I’ve seen that I need a faster way to run this algorithm and I found this ...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 13924 124
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19568 166
New
AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
Crowdhailer
I’ve been hearing much about the new formatter and it’s something I have been keen to try. I find examples buy far the most illuminating...
248 19204 150
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement