sax

sax

Ecto.Email - an ecto type for validating email address fields

Hello!

Just wanted to post about a library I published over the weekend. It’s a very simple Ecto.Type for email addresses, with automatic validations: Ecto.Email.

After looking around at a lot of email validation code as well as RFCs for email address formats, my business partner and I settled on the email_validator library. I’ve been seeing other people posting about custom ecto types in the past few weeks, and have had very good experiences with them in terms of automatically casting between unit types, enforcing validations, etc.

In this case, what goes into and out of the email type is are strings, but it gives a bit of benefit in terms of automatically applying validations during casting. I’ve found that sometimes one (i.e. me or my teammates) can forget to add validator functions when adding new changesets to existing schemas.

Hopefully this is helpful to others, even if only as a reference for creating simple ecto types.

Most Liked

krasenyp

krasenyp

Unless you are a part of the Ecto team, please don’t define modules that are not in your “namespace”, as is the case with Ecto.

halostatue

halostatue

Looks good and useful. I, too, have started shifting toward Ecto.Type usage over repeated validations in various changeset functions.

A couple of small suggestions:

  • I would remove the suggestion to enable the citext extension, as its use is discouraged in recent versions of PostgreSQL (since version 12). It’s probably OK for ASCII text, but if there’s any chance of Unicode text in your field, it’s not good. As unicode is supported in the address part (äddrëss@, since RFC6531 in 2012), this isn’t entirely theoretical.The recommended solutions are to either:

    1. Use collations (I’m still working out how to do this myself). There is a useful, if Django-specific blog post that talks about this in more depth.

    2. Perform the transformations during your casting. That is, always store the type as String.downcase/2 on the string. This could be configurable, but then you’d need to implement Ecto.ParameterizedType instead. Note that you may need to use :greek or :turkic case folding some cases.

  • Look at your equal?/2 implementation as it won’t work for case differences (e.g., equal?("A@B.COM", "a@b.com") should be true, but that will not happen since you’re essentially doing a byte comparison).

  • Decide whether you want to support punycode (domain parts, @example.com) cannot contain Unicode and have to be converted to punycode if they do). Parameterization here would be useful because it is not free (see nameprep, which requires both case-folding and NKFC normalization). See also the casting transformations noted above for this, since the address part and the domain part should be handled separately.

It may be that email_validator does not handle these correctly, but I haven’t looked at that. I’m pretty sure that the validation functions that I have previously implemented don’t handle address part Unicode at all, and I know that I don’t have support for punycode transformations on the domain part, although I did implement that as part of my regex matching at the time.

sax

sax

Good point with the Ecto namespace. I was mis-remembering how other libraries named their modules prior to them being incorporated into Ecto itself. I’ll ship a new version moving everything to EctoEmail.

Regarding unicode, I’ve had such positive experience with citext that I had not followed the issues surrounding it. Definitely will be looking at that, and while I might retain a reference or two to it will remove it from the example code. Also you have a very good point about the equal?/2 callback. Will incorporate that into the next version as well.

Where Next?

Popular in Libraries Top

josevalim
Hi everyone, We would like to announce that Plataformatec is working on a new MySQL driver called MyXQL. Our goal is to eventually integ...
New
mathieuprog
Hello :wave: Allow me to introduce you to Tz, an alternative time zone database support to Tzdata. Why another library? First and fore...
New
kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
jakub-zawislak
Hi everyone, I’m coming from the Symfony (PHP) framework. I like Phoenix, but it has a one thing that was build much better in the Symfo...
New
danschultzer
In short Plug n’ play OAuth 2.0 provider library. Just set up a resource owner schema with Ecto (your user schema), install the dependen...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
mtrudel
Bandit is an HTTP server for Plug and WebSock apps. Bandit is written entirely in Elixir and is built atop Thousand Island. It can serve...
New
vic
Expat is a tiny experiment I did for extracting patterns and being able to reuse them (compose and share patterns between elixir librarie...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Sub Categories:

We're in Beta

About us Mission Statement