Skip to main content
This plugin validates phone numbers in a specified field against a corresponding country code from another field. It leverages the libphonenumber-js library for robust, country-specific validation. The primary use case is to ensure that phone number data ingested into Flatfile is correctly formatted and valid. It can automatically correct and reformat phone numbers into various standard formats (e.g., NATIONAL, INTERNATIONAL, E164) or simply add an error to the record if the number is invalid.

Installation

Install the plugin using npm:

Configuration & Parameters

The plugin accepts a configuration object with the following parameters:

Usage Examples

Basic Usage

This example validates the ‘phone’ field using the ‘country’ field in all sheets. It will add errors for invalid numbers but will not automatically reformat them.

Configuration Example

This example validates and formats phone numbers in the “contacts” sheet using the ‘phone_number’ and ‘country_code’ fields. Valid numbers are automatically converted to INTERNATIONAL format.

Advanced Usage with Format Options

This example demonstrates using the formatOptions parameter to provide more specific formatting rules from the underlying libphonenumber-js library.

Troubleshooting

If validation is not working as expected:
  1. Check field names: Ensure that the phoneField and countryField values in the configuration exactly match the field keys in your Sheet template.
  2. Verify country codes: Ensure the countryField in your data contains valid two-letter ISO 3166-1 alpha-2 country codes (e.g., “US”, “GB”, “DE”).
  3. Enable debug logging: Set the debug option to true in the configuration to see more detailed logging output in your environment’s console, which can help diagnose issues.

Notes

Default Behavior

  • By default, the plugin applies to all sheets (sheetSlug: '**')
  • Phone numbers are automatically converted to the specified format when autoConvert is true (default)
  • The default format is ‘NATIONAL’
  • Records are processed with a concurrency of 10

Error Handling

The plugin adds errors to specific fields rather than throwing exceptions:
  • Empty phone number: “Phone number is required”
  • Empty country field: “Country is required for phone number formatting”
  • Invalid phone number: “Invalid phone number format for [country]“

Requirements and Limitations

  • This plugin has an external dependency on libphonenumber-js
  • The countryField must contain a valid two-letter ISO 3166-1 alpha-2 country code
  • The plugin operates as a listener on the commit:created event, processing records in batches
  • While the documentation mentions specific support for US, UK, India, Germany, France, and Brazil, the underlying library supports a wider range of countries