npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tepez/joi-phone-number-extensions

v1.2.1

Published

A joi extension for validating and formatting phone numbers using google-libphonenumber

Downloads

507

Readme

joi-phone-number-extensions

A joi extension for validating and formatting phone numbers using google-libphonenumber

npm version Build Status

Install

npm install --save @tepez/joi-phone-number-extensions

Usage

const BaseJoi = require('joi');
const JoiPhoneNumberExtensions = require('joi-phone-number-extensions');
const Joi = BaseJoi.extend(JoiPhoneNumberExtensions);

const schema = Joi.phoneNumber().defaultRegion('US').type('MOBILE').format('E164');
schema.validate('+1 541-754-3010', (err, value) => {
    console.log(value) // +15417543010
});

API

phoneNumber - inherits from string

Generates a schema object that matches phone numbers.

phoneNumber.defaultRegion(regionCode)

Use this region code as default if given a non-international number.

regionCode is one of the code supported by libphonenumber, e.g. UG, US, UY or UZ.

const withDefaultRegion = Joi.phoneNumber().defaultRegion('US');
const withoutDefaultRegion = Joi.phoneNumber();

// valid
withDefaultRegion.validate('(541) 754-3010');

withDefaultRegion.validate('+1 541-754-3010');
withoutDefaultRegion.validate('+1 541-754-3010');

// invalid
withoutDefaultRegion.validate('(541) 754-3010');

phoneNumber.region(regionCode)

Require the number to be a valid number in given region.

regionCode is one of the code supported by libphonenumber, e.g. UG, US, UY or UZ.

const schema = Joi.phoneNumber().region('FR');

// valid
schema.validate('+33752961860');

// invalid
schema.validate('+1 541-754-3010');

regionCode can also be a ref for another property:

const schema = Joi.object().keys({
    phone: Joi.phoneNumber().region(Joi.ref('region')),
    region: Joi.string()
});
// valid
schema.validate({
    phone: '+33752961860',
    region: 'FR'
});

phoneNumber.type(type)

Require the number to a valid number of given type.

type must be one of the types supported by libphonenumber, e.g. FIXED_LINE, MOBILE, FIXED_LINE_OR_MOBILE, TOLL_FREE, PREMIUM_RATE, SHARED_COST, VOIP, PERSONAL_NUMBER, PAGER, UAN, VOICEMAIL or UNKNOWN.

const schema = Joi.phoneNumber().type('FIXED_LINE');

// valid
schema.validate('+1 541-754-3010');

// invalid
schema.validate('+972525555555');

phoneNumber.format(format)

Requires the number to be formatted in given format. If the validation convert option is on (enabled by default), will return the number as formatted in given format.

format must be one of the types supported by libphonenumber, e.g. E164, INTERNATIONAL, NATIONAL or RFC3966.

Joi.phoneNumber().format('NATIONAL').validate('+1-541-754-3010')
// (541) 754-3010'

Joi.phoneNumber().format('E164').validate('+1-541-754-3010')
// +15417543010

See tests for more examples

TypeScript

import * as Joi from 'joi'
import Extension, { JoiWithPhoneNumberExtension } from '@tepez/joi-phone-number-extensions'

const ExtendedJoi: JoiWithPhoneNumberExtension = Joi.extend(Extension);
// Type information for the phoneNumber() method is now available to ExtendedJoi