@thiagodelgado111/yup-phone
Version:
Adds a phone number validation check to yup validator using google-libphonenumber.
5 lines (4 loc) • 1.01 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:!0});var gPhoneNumber=require("google-libphonenumber"),phoneUtil=gPhoneNumber.PhoneNumberUtil.getInstance(),CLDR_REGION_CODE_SIZE=2,isValidCountryCode=function(a){return"string"===typeof a&&a.length===CLDR_REGION_CODE_SIZE},YUP_METHOD="phone";
function phoneValidationMethod(a,b){void 0===b&&(b=!1);var d=isValidCountryCode(a)?"${path} must be a valid phone number for region "+a:"${path} must be a valid phone number.";return this.test({name:YUP_METHOD,message:d,test:function(e){if(this.schema._nullable&&null===e)return this.resolve(!0);isValidCountryCode(a)||(a="US",b=!1);try{var c=phoneUtil.parseAndKeepRawInput(e,a);if(!phoneUtil.isPossibleNumber(c))return!1;var d=phoneUtil.getRegionCodeForNumber(c);var f=b?phoneUtil.isValidNumberForRegion(c,
a):phoneUtil.isValidNumberForRegion(c,d)}catch(g){f=!1}return this.resolve(f)}})}exports.YUP_METHOD=YUP_METHOD;exports.default=phoneValidationMethod;
//# sourceMappingURL=yup-phone.cjs.js.map
;