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 Announcing Top

OvermindDL1
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM. ...
New
josevalim
EDIT: since Ecto 3.0 final version is out, this post was amended to use the final versions in the instructions below. Hi everyone, We a...
New
woutdp
Hi! I wanted to introduce my latest project LiveSvelte. It allows you to render Svelte inside LiveView with end-to-end reactivity. It’s ...
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
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
nikokozak
Hello all, I’ve been working on Svonix - a library for quickly integrating Svelte components into Phoenix views. It’s a much-needed succ...
New
Azolo
Hey everyone, I just released WebSockex which is a Elixir WebSocket client. WebSockex strives to work as a OTP special process, be RFC6...
New
Hal9000
Here is my first stab at this. README pasted below. https://github.com/Hal9000/elixir_random Comments and critiques are welcome. Thank...
New
markmark206
simple_feature_flags is a tiny package that lets you turn features on or off based on which environment (e.g. localhost, staging, product...
New
trisolaran
Hi! :waving_hand: I would like to present LiveSelect, a little library that I wrote to easily add a dynamic selection input to your LV f...
198 10858 107
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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

We're in Beta

About us Mission Statement