Introducing SwiftEmailValidator

Dave Poirier
ITNEXT
Published in
4 min readJan 28, 2022

--

Validating 99% of North-American emails takes less than 5 minutes of efforts, but validating 100% of valid email addresses world-wide can take a significant effort. Introducing SwiftEmailValidator, to take that pain away.

Distributed under MIT license and available for free on GitHub: https://github.com/ekscrypto/SwiftEmailValidator

Photo by Onlineprinters on Unsplash

If you believe you can use a “simple” regular expression to validate emails, you are likely deluding yourself. A quick review of EmailRegex.com will show you that multiple different regex exists for a multitude of programming languages and unfortunately none of them can claim 100% validation. The better ones are extremely hard to read and unmaintainable.

It doesn’t help that several people who claim to have studied RFC standards make (mostly valid) lists of example valid/invalid email addresses, missing edge cases or worse, incorrectly labeling an email as valid when it should be invalid. (i.e.: something@123.123.123.123 is not a valid email address, neither is something@something.web).

Luckily using a bit of logic, and available external resources like the Public Suffix List from https://publicsuffix.org, it becomes possible to build a stronger validator.

Installation

  • From Xcode, open your project settings and “Package Dependencies”
  • Hit the “+” button to add a new dependency
  • In the search bar, enter: https://github.com/ekscrypto/SwiftEmailValidator
  • Review the information displayed and hit the “Add Package” button.
  • When done, the package should be listed as a dependency.
Swift Package Dependencies screen with “Add” button highlighted
Xcode’s Swift Package Manager “Add Package” screen
Xcode Package Dependencies screen with dependency defined

Basic Usage

To validate if an email address is properly formatted:

You can also create a String extension to add a .isValidEmail boolean computed property:

Advanced: Specify RFC compatibility requirements

The validator has three levels of compliance it can validate against, using the “compatibility” parameter of the validator functions:

  • .ascii: RFC 822 — US ASCII emails
  • .asciiWithUnicodeExtension: RFC 2047 — US ASCII emails with encoded Unicode support
  • .unicode: RFC 6531 — SMTPUTF8 support

Advanced: IPv4 / IPv6 Addresses as Domain

If you want to allow email addresses to use IPv4/IPv6 addresses directly, like in myaddress@[127.0.0.1], you can set the allowAddressLiterals to true.

By default IP addresses are not allowed.

Advanced: Custom Public Suffix Rules

The Public Suffix List is a collaborative effort to maintain a list of Top Level Domains and some of the validation rules that applies to each. This helps site owners and browser vendors to block cookies at levels that shouldn’t be available and can also be used to validate if the @domain part of an email address may be valid.

The SwiftEmailValidator library comes built-in with a version of the Public Suffix List but you may want to have your app regularly download newer versions of this list to stay up to date, or maybe you have some internal domains that you would like to support.

The rules are in the format [[String]]. The outside array is a list of public suffix and with the internal array a list of rules for this suffix.

A set of rules with value [[“com”]] would indicate that email@com is invalid but email@website.com is allowed.

A set of rules with value [[“*”,”com”]] would indicate that email@website.com is invalid but email@subdomain.website.com is allowed.

A set of rules with value [[“*”,”com”],[“!myserver”,”com”]] would indicate that email@com, email@website.com are invalid addresses but email@myserver.com is allowed. Rules that start with an exclamation mark are “exception” and will take precedence over other rules.

You can find a Swift script to download the latest copy of the Public Suffix List and generate the appropriate [[String]] rules at https://github.com/ekscrypto/SwiftEmailValidator/blob/main/Utilities/update-suffix.swift

While the library has extensive unit tests, email validation has always been tricky. Handling all edge cases require significant effort. Please make sure you extensively test this library before using it in production.

I hope this library will help some of you, I’m sure it will help me in my current and upcoming projects. Cheers and happy coding!

--

--

Senior iOS Developer | Mobile Security And Reliability Specialist