mongoose-intl-phone-number
This module takes a string of numbers and determines their validity as well as returns data about the phone numbers. This module is based on Google's libphonenumber.
How it works
A phone number is provided on the document, during the pre-save/validate hook (you can specify), it runs the phone number through libphonenumber and stores the data returned onto fields in the document model.
Use Case
Applications that accept international phone numbers should use this plugin to gather and store information about the number such as country code, national format, etc.
Installation
npm install --save mongoose-intl-phone-number
API Reference
mongooseIntlPhoneNumber
Validates a phone number against google's libphonenumber, otherwise returns a validation error.
Example
var mongooseIntlPhoneNumber = require('mongoose-intl-phone-number');
var schema = Schema({...});
schema.plugin(mongooseIntlPhoneNumber, {
hook: 'validate',
phoneNumberField: 'phoneNumber',
nationalFormatField: 'nationalFormat',
internationalFormat: 'internationalFormat',
countryCodeField: 'countryCode',
});
Use it with a model...
var Customer = mongoose.model('Customer');
var customer = new Customer({
firstName: 'test',
lastName: 'customer',
customerType: 'testing',
phoneNumber: '+18888675309',
email: 'test@testing.com'
});
customer.save();
Resulting document...
{
"firstName": "test",
"lastName": "customer",
"customerType": "testing",
"phoneNumber": "+18888675309",
"nationalFormat": "(888) 867-5309",
"internationalFormat": "+1 888-867-5309"
"countryCode": "US"
}
mongooseIntlPhoneNumber~mongooseIntlPhoneNumber(schema, [options])
Attaches the mongoose document hook and parses the phone number that is provided.
Kind: inner method of mongooseIntlPhoneNumber
Param | Type | Default | Description |
---|
schema | object | | Mongoose schema |
[options] | object | | |
[options.hook] | string | "validate" | |
[options.phoneNumberField] | string | "phoneNumber" | |
[options.nationalFormatField] | string | "nationalFormat" | |
[options.internationalFormatField] | string | "internationalFormat" | |
[options.countryCodeField] | string | "countryCode" | |