-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (30 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const Joi = require('@hapi/joi');
const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();
module.exports = Joi.extend(joi => ({
base: joi.string(),
name: 'string',
language: {
mobileNumber: 'did not seem to be a mobile number.'
},
rules: [
{
name: 'mobileNumber',
params: {
countryCode: joi.string().required()
},
validate(params, value, state, options) {
try {
const number = phoneUtil.parseAndKeepRawInput(value, params.countryCode);
// getNumberType === 1 means it is a Mobile Number
// https://github.com/ruimarinho/google-libphonenumber/blob/master/src/phonenumberutil.js#L916
if (!phoneUtil.isValidNumber(number) || phoneUtil.getNumberType(number) !== 1) {
return this.createError('string.mobileNumber', { v: value }, state, options);
}
return value;
} catch (e) {
return this.createError('string.mobileNumber', { v: value }, state, options);
}
}
}
]
}));