Custom validation for phone-number using class-validator package
The solution for “Custom validation for phone-number using class-validator package” can be found here. The following code will assist you in solving the problem.
import { ClassConstructor } from ‘class-transformer’;
import {
ValidationOptions,
registerDecorator,
ValidationArguments,
ValidatorConstraintInterface,
ValidatorConstraint,
} from ‘class-validator’;
import { PhoneNumberUtil } from ‘google-libphonenumber’;
import * as iso from ‘i18n-iso-countries’;
// phone-number validator
export const IsValidNumberOf = (
type: ClassConstructor,
property: (o: T) => any,
validationOptions?: ValidationOptions,
) => {
return (object: any, propertyName: string) => {
registerDecorator({
target: object.constructor,
propertyName,
options: validationOptions,
constraints: [property],
validator: IsValidNumberOfConstraint,
});
};
};
@ValidatorConstraint({ name: ‘IsValidNumberOf’, async: true })
export class IsValidNumberOfConstraint implements ValidatorConstraintInterface {
validate(value: any, args: ValidationArguments) {
const util = PhoneNumberUtil.getInstance();
const [fn] = args.constraints;
// get the value of the country Field
const countryCode = fn(args.object);
// check if the country is valid even though it is checked at class level
const isValidISOCode = iso.isValid(countryCode);
if (!isValidISOCode) {
return false;
}
// Checks if the value (number) belongs in the extracted countryCode
const formattedPhoneNumber = util.parse(value, countryCode);
const isValidPhoneNumber = util.isValidNumberForRegion(formattedPhoneNumber, countryCode);
if (!isValidPhoneNumber) {
return false;
}
return true;
}
defaultMessage(args: ValidationArguments) {
// const [constraintProperty]: (() => any)[] = args.constraints;
return `${args.property} must be a valid phone-number in the specified country`;
}
}
// user
import { IsISO31661Alpha2, IsNotEmpty } from ‘class-validator’;
import { IsValidNumberOf } from ‘../../validators/is-valid-number-of.validator’;
import { IPhoneNumber } from ‘../../interfaces/shared/phone-number.interface’;
export class PhoneNumberDTO implements IPhoneNumber {
@IsValidNumberOf(PhoneNumberDTO, (o: { country: any; }) => o.country)
number: string;
@IsNotEmpty()
@IsISO31661Alpha2()
country: string;
}
More questions on [categories-list]
- tss from gene granges
- ixl ansers ixl ansers
- get coin prices node-binance
- how to setup netflix workflow worker
- spritesheets in pyqt spritesheets in pyqt
- cahokia mounds pictures cahokia mounds pictures cahokia mounds pictures
- python 2 decimal places how to get decimal part of a double in python set number of decimals python
- how to find nuber of tweets per day using python how to find nuber of tweets per day using python how to find nuber of tweets per day using python how to find nuber of tweets per day using python how to find nuber of tweets per day using python
- haskell get specific elements of a String
- vb net code snippets for storing password
- error TS2307: Cannot find module ‘@ngx-meta/core’.
- inline scripts encapsulated in tags