How Abstract API Handles Phone Number Validation in Joi
Abstract API addresses the core weaknesses of traditional Joi validation by adding a live verification layer that queries real-time telecom data sources.
- It confirms if a number is active and dialable, rather than just check its format.
- The API returns the current carrier and a precise line type, such as mobile, landline, or VOIP.
- It uses live telecom data, so validation rules do not depend on static, outdated datasets.
- This allows for complex business rules that simple pattern matches cannot support, like the rejection of VOIP numbers or the restriction of sign-ups to specific countries.
How to Bring Abstract API to Your Dev Environment
Once you possess familiarity with Abstract's capabilities, the addition of its phone number validation API to your project is simple. The process requires just a few steps to get your environment ready.
- Sign up at Abstract API and get the Phone Validation API key.
- Install the necessary packages with the command below.
- In your bootstrap code, import AbstractPhoneValidation and configure it with your key.
- Create a Joi schema and embed an async custom validator that calls AbstractPhoneValidation.verify(phone).
- Reject or transform the input based on the data.valid field and any extra rules you need.
- Ship your code. Abstract keeps the data fresh via the API, so no extra maintenance occurs.
Sample Phone Number Validation Implementation with Abstract API
The code below shows a custom Joi validator. It calls Abstract API to check if a phone number is a valid US mobile number. If the number fails the check, the validator returns an error. Otherwise, it returns the original value.
The API response provides rich data for validation logic. The "valid" field confirms the number is active. The "format" field gives local and international versions. The "country" and "location" fields let you enforce geographic restrictions. The "type" field distinguishes mobile from landline or VOIP, and "carrier" identifies the current network owner. These fields allow Joi to perform real operational checks beyond simple pattern matches. A successful validation returns a JSON object like this:
Final Thoughts
Traditional validation methods only check a number's format against static, often outdated, data. They cannot confirm if a number is active, identify its line type, or name its current carrier. Abstract API overcomes these limits with live data checks for truly reliable validation. Consider an account on Abstract API to get your free API key.
Frequently Asked Questions
What is phone number validation in Joi and why do I need it?
Joi is a schema validation library for Node.js, and phone number validation means adding rules to a Joi schema that check whether a submitted phone number is correctly formed. You need it because phone numbers come in dozens of regional formats, and accepting unvalidated input leads to bad data, failed SMS deliveries, and downstream errors in your application.
What does the joi-phone-number plugin do and how do I use it?
The joi-phone-number plugin extends Joi with phone-specific rules built on Google's libphonenumber library. After installing and registering the extension, you can call .phoneNumber() on a string schema and pass options like defaultCountry and a format such as e164 or international to both validate and normalize the number in one step.
When should I use a regex pattern versus a Joi phone number plugin?
A regex pattern like ^\+?[1-9]\d{1,14}$ is the fastest option with no extra dependencies, but it only checks the E.164 structure and cannot handle the full diversity of global phone formats. A plugin backed by libphonenumber is more accurate across international numbers, though it adds a dependency and slightly more overhead. Use a regex when speed and simplicity matter more than international coverage.
How do I write a custom Joi validation rule using libphonenumber-js?
You can pass a custom validate function directly to Joi.string().custom() and call parsePhoneNumber() or isValidPhoneNumber() from libphonenumber-js inside it. This approach gives you full control over the error message, the conversion logic, and any extra checks such as requiring a specific country code, without being locked into the behavior of a third-party Joi extension.
Why does format-only validation sometimes let invalid numbers through?
Offline validation libraries can only confirm that a number looks structurally correct for its region; they cannot check whether the number actually belongs to an active subscriber. A number can pass a regex or even libphonenumber's parser and still be unallocated or disconnected. This is why syntactically valid numbers sometimes cause failed calls or SMS delivery errors in production.
How does Abstract's Phone Validation API improve on library-based Joi validation?
Abstract's Phone Validation API checks whether a number is active and dialable in real time against live telecom data, rather than just validating its format offline. By calling the API inside a Joi async custom validator and checking the valid field along with line_type and country_code, you get carrier identification and line-type classification (mobile vs. landline) alongside the standard format check, catching numbers that pass structural validation but do not actually work.


