joi-phone-number-extensions
A joi extension for validating and formatting phone numbers using google-libphonenumber
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;const withoutDefaultRegion = Joi; // validwithDefaultRegion; withDefaultRegion;withoutDefaultRegion; // invalidwithoutDefaultRegion;
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; // validschema; // invalidschema;
regionCode
can also be a ref
for another property:
const schema = Joiobject;// validschema;
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 = Joitype'FIXED_LINE'; // validschema; // invalidschema;
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// (541) 754-3010' Joi// +15417543010
See tests for more examples