sax

sax

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.

Showing Posts 1 to 3

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.

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.

sax

sax OP

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.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

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
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
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New
pferriby
Introductory paragraph I’ll be looking for a keen junior or someone that has a couple of years experience in the real world (so you’ve be...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews