How to Validate an E-Mail-Address in PureScript

by|inArticles||3 min read
Validating an E-Mail-Address in PureScript
Validating an E-Mail-Address in PureScript

Validating an E-Mail-Address in PureScript can be done in several ways. We will discover two of them in this article. In detail we will look at regular expressions and using a helper library.

Validating an E-Mail-Address using Regular Expressions

As in the most other programming languages we can use regular expressions in PureScript. The package purescript-strings provides us with this functionality. The module Data.String.Regex provides all functions to validate an email address by regex.

Luckily we don't need to invent the regular expression ourselves. The regular expression for the RFC5322 address specification can already easily be found on the net and looks as follows:

([-!#-'*+/-9=?A-Z^-~]+(\.[-!#-'*+/-9=?A-Z^-~]+)*|"([]!#-[^-~ \t]|(\\[\t -~]))+")@([-!#-'*+/-9=?A-Z^-~]+(\.[-!#-'*+/-9=?A-Z^-~]+)*|\[[\t -Z^-~]*])

To use this regular expression we can create one by calling the regex function:

regex :: String -> RegexFlags -> Either String Regex

All we need is a string (our regex that we saw earlier) and some RegexFlags. To create RegexFlags we cann use predefined functions, for instance ignoreCase, since we usually don't care too much about this in an email address.

The regex function will provide us an Either with a valid Regex if the regex is correct and a String if the given regex-string is not valid. We will omit the error case (and use fromRight from Either) and trust that the regex is correct:

let ourRegex = fromRight $ regex `([-!#-'*+/-9=?A-Z^-~]+(\.[-!#-'*+/-9=?A-Z^-~]+)*|"([]!#-[^-~ \t]|(\\[\t -~]))+")@([-!#-'*+/-9=?A-Z^-~]+(\.[-!#-'*+/-9=?A-Z^-~]+)*|\[[\t -Z^-~]*])`

Alright, we have created a regex. Let's use it to validate an email address! This can be done with the test function which will give as a Boolean. True if the email string contains the regex expression.

isEmailValid :: String -> Boolean
isEmailValid email =
  test regex email
  where
    ourRegex = ...

Validating an E-Mail-Address using email-validate

Of course we can always write code ourselves, as we have seen with the regex example. It gives us the flexibility to add our own rules or adapt the regular expression. But in the most cases we can use packages which provide the same functionality. A big plus of this approach is that those external packages are often properly tested and battle tested by other users as well.

One of the packages is purescript-email-validate. Let's examine how our email validation would look like. The function of interest is called emailAddress.

The email-validate package is making use of an own type called EmailAddress. To create an EmailAddress we need to pass a String and get an Maybe back. In the error case we will receive Nothing. In the success case we will get a Just with the valid EmailAddress. Neat!

isEmailValid :: String -> Boolean
isEmailValid email = case (emailAddress email) of
  Nothing -> false
  Just _ -> true

In this sample we are not interested in using the extra type but I strongly advise you to do so since it brings many advantages for your code stability and correctness.

As well, we can take the shortcut of the package with the two functions isValid and validate:

isValid :: String -> Boolean

-- If you want to find out why a particular string is not
validate :: String -> Either String EmailAddress

Under the hoos email-validate is using a string-parsers to validate email strings. This is different to what we saw in the regex example.

Thank you for reading this far! Let’s connect. You can @ me on X (@debilofant) with comments, or feel free to follow. Please like/share this article so that it reaches others as well.

Related Articles

© Copyright 2024 - ersocon.net - All rights reservedVer. 415