Guides
Last updated
July 25, 2025

5 Ways to Implement Email Validation with Joi

Nicolas Rios
Nicolas Rios
Table of Contents:
ON THIS PAGE
Get your free
email validation
 API key now
stars rating
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

Validating emails in Joi is a common step for maintaining data quality. We'll walk through five different implementation methods, complete with working code. Along the way, we'll look at the pitfalls of these traditional approaches and see how Abstract API helps overcome them.

How to Implement Email Validation in Joi

Joi offers several ways to validate email addresses. Here are four common methods, from basic syntax checks to custom rule creation, each with a corresponding code example.

Standard Joi Email Validation

The most direct method uses the “Joi.string().email()” chain. This approach employs the RFC-compliant parser from “@hapi/address” for syntax validation. It also checks the email's top-level domain against the IANA TLD list.

If a bundler removes this list, you might encounter a “Built-in TLD list disabled” error message. The Joi documentation provides more details on this function.

const schema = Joi.string().email();

Explicit TLD Configuration

You can specify which top-level domains to accept. The “tlds” option lets you define an “allow” list, such as “com” or “net”. Alternatively, you can set “tlds” to “false” to bypass this check entirely.

This is a documented workaround for bundler issues. The “minDomainSegments” option ensures an address has a minimum number of domain parts, like in “foo@bar.com”. The official API documentation has further examples.

const schema = Joi.string().email({  
  tlds: { allow: ['com', 'net', 'io'] },  
  minDomainSegments: 2
});

Refined Rules for Length, Unicode, and FQDNs

Joi allows for tighter control over validation rules. You can set “allowUnicode” to “false” to block non-ASCII characters, a common need for legacy systems. To enforce the 254-character limit on email addresses, set “ignoreLength” to “false”.

The “allowFullyQualified” option, when “true”, permits a trailing dot in the domain name. This can be useful for certain internal DNS workflows. More information is available on the Joi API page.

const schema = Joi.string().email({
  allowUnicode: false,
  ignoreLength: false,
  allowFullyQualified: true
});

Custom Rules with @hapi/address

For more advanced cases, you can build a custom rule. The “custom()” method lets you use “@hapi/address” directly. The “Address.analyzeEmail” function exposes the same validator Joi uses internally.

This setup lets you chain extra logic without a need to fork Joi. The Address module API explains this in greater detail.

const Address = require('@hapi/address');
const emailSchema = Joi.string().custom((value, helpers) => {
  const error = Address.analyzeEmail(value, { tlds: { allow: true } });
  if (error) return helpers.error('string.email', { value });
  return value;
});

Challenges of Joi Email Validation

While Joi provides several validation methods, they come with inherent limitations. These challenges range from incomplete standards support to the inability to confirm if an email address is actually functional.

  • Joi only implements a subset of RFC 5322 and rejects legal UTF-8 characters in an address's local part. The standard Joi.string().email() method lacks an option to support newer international standards.
  • International domain names require Punycode translation. Joi validates the raw string, so native-script domains fail unless pre-encoded. This forces extra logic around methods like Joi.string().email() to handle these cases correctly.
  • The default Joi.string().email() rule does not require a top-level domain, so it accepts invalid addresses. To fix this, you must explicitly configure TLDs, which creates maintenance overhead for your validation schema.
  • Joi only asserts syntax, not deliverability. An address that passes validation with any method may still be undeliverable. The library cannot check for provider policies or actual mailbox existence.

Validate Emails with Abstract API.
Ensure data integrity in your Joi projects by implementing robust and accurate email validation.
Get started for free

How Abstract API Handles Email Validation in Joi

Abstract API addresses the core weaknesses of traditional Joi validation through real-time network checks and advanced filters.

  • It confirms mail deliverability through real-time MX record lookups and SMTP handshakes.
  • The system detects and flags disposable, role-based, and catch-all email addresses.
  • It suggests corrections for common typographical errors in email addresses.
  • The API integrates directly into Joi's asynchronous validation flow with an external hook.
  • It returns a clear, final verdict on deliverability, which simplifies validation logic.

How to Set Up Abstract API in Your Project

Once you're familiar with Abstract’s capabilities, the addition of its email validation API to your project is simple. To prepare your development environment, you need to complete a few steps:

  • Sign up at Abstract and obtain your email validation API key from the dashboard.
  • Install the necessary packages, Joi for schema validation and Axios for HTTP requests.
  • Store your API key in a ".env" file to keep it out of source control.
npm install joi axios dotenv

Sample Email Validation Implementation with Abstract API

The code below defines a Joi schema that uses an external, asynchronous function to validate an email. It sends the email address to the Abstract API endpoint. If the API response indicates the email is not "DELIVERABLE" or is a disposable address, the validation fails and throws an error. Otherwise, the original email value passes.

import Joi from 'joi';
import axios from 'axios';

const emailSchema = Joi.string().email().external(async (value, helpers) => {
  const { data } = await axios.get('https://emailvalidation.abstractapi.com/v1/', {
    params: { api_key: process.env.ABSTRACT_EMAIL_KEY, email: value }
  });
  if (data.deliverability !== 'DELIVERABLE' || data.is_disposable_email.value) {
    throw helpers.error('any.invalid', { message: 'Undeliverable or disposable email' });
  }
  return value;
});
export default emailSchema;

Upon a successful validation for a deliverable email, the API returns a detailed JSON object. This payload provides a clear "DELIVERABLE" status, a quality score, and boolean flags for disposable, role-based, and free email providers. It also confirms the mail server exists and accepts connections, which offers a definitive check that simple pattern matching cannot perform.

{
  "email": "johnsmith@gmail.com",
  "autocorrect": "",
  "deliverability": "DELIVERABLE",
  "quality_score": 0.90,
  "is_valid_format": { "value": true,  "text": "TRUE" },
  "is_free_email":  { "value": true,  "text": "TRUE" },
  "is_disposable_email": { "value": false, "text": "FALSE" },
  "is_role_email":  { "value": false, "text": "FALSE" },
  "is_mx_found":    { "value": true,  "text": "TRUE" },
  "is_smtp_valid":  { "value": true,  "text": "TRUE" }
}

Final Thoughts

Traditional validation methods approve syntactically correct but undeliverable emails. They fail to detect typos, disposable domains, or non-existent mail servers. Abstract API closes these gaps with real-time network checks for a definitive verdict on deliverability. To reliably validate user emails, consider an account on Abstract API to get your free API key.

Validate Emails with Abstract API.
Protect your application and improve data quality by adding email validation to your Joi schemas.
Get started for free

Related Articles

Phone Validation
key now
Get your free
stars rating
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required