UNPKG

@geekbears/gb-class-validators

Version:

Geekbears custom validators using class-validator package.

39 lines (38 loc) 1.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IsGBPhoneNumberV2 = IsGBPhoneNumberV2; /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unused-vars */ const class_validator_1 = require("class-validator"); const google_libphonenumber_1 = require("google-libphonenumber"); function IsGBPhoneNumberV2(validationOptions) { return function (object, propertyName) { (0, class_validator_1.registerDecorator)({ target: object.constructor, propertyName, options: validationOptions, constraints: [], validator: { validate(value, args) { let isValid = false; if (typeof value !== 'string') return isValid; const phoneUtil = google_libphonenumber_1.PhoneNumberUtil.getInstance(); try { const parsedNumber = phoneUtil.parseAndKeepRawInput(value, ''); isValid = phoneUtil.isValidNumber(parsedNumber); } catch (error) { return isValid; } return isValid; }, defaultMessage(args) { return 'The phone number ($value) is not valid!'; }, }, }); }; }