zod-phone-number
v1.0.2
Published
Zod Phone Number is validation schema for Zod
Downloads
171
Maintainers
Readme
Zod Phone Number
Zod Phone Number is validation schema for Zod.
Features
- Seamlessly integrates with Zod's validation framework.
- Supports multiple phone number formats, including National, International, RFC3966, and E.164.
- Provides detailed phone number metadata, such as country code and formatted numbers.
- Easy-to-use API for handling phone number validation in your applications.
Installation
$ npm i zod-phone-number
Usage
const { ZodPhoneNumber, RETURNING_FORMAT } = require('zod-phone-number');
const { data } = ZodPhoneNumber.phoneNumber({
returningFormat: RETURNING_FORMAT.NATIONAL,
}).safeParse('+2010 605 944 77'); // 010 60594488
const { data } = ZodPhoneNumber.phoneNumber({
returningFormat: RETURNING_FORMAT.INTERNATIONAL,
}).safeParse('+2010 605 944 88'); // +20 10 60594488
const { data } = ZodPhoneNumber.phoneNumber({
returningFormat: RETURNING_FORMAT.ORIGINAL,
}).safeParse('+2010 605 944 88'); // +2010 605 944 88
const { data } = ZodPhoneNumber.phoneNumber({
returningFormat: RETURNING_FORMAT.FORMATTED_VALUE,
}).safeParse('+2010 605 944 88'); // +201060594488
const { data } = ZodPhoneNumber.phoneNumber({
returningFormat: RETURNING_FORMAT.RFC3966,
}).safeParse('+2010 605 944 88'); // tel:+201060594488
const { data } = ZodPhoneNumber.phoneNumber({
returningFormat: RETURNING_FORMAT['E.164'],
}).safeParse('+2010 605 944 88'); // +201060594488
const { data } = ZodPhoneNumber.phoneNumber({
returningFormat: RETURNING_FORMAT.VALUE_WITH_EXTRA_INFO,
}).safeParse('+2010 605 944 88');
/*
output: {
countryCode: 'EG',
formattedNumber: '+201060594488',
nationalNumber: '1060594488',
originalValue: '+2010 605 944 88',
countryCallingCode: '20',
}
*/
Tests
To run the test suite, first install the dependencies then run npm test
:
$ npm install
$ npm test
Support
Feel free to open issues on github.