//**///////////////////////////////////////////////////////////////////////////////////////// // Author: Đào Quốc Huy / huydaoquoc88@gmail.com // Description: Validation for Base64 string. Valid if input is [BASE64, "", null, undefined]// ///////////////////////////////////////////////////////////////////////////////////////////// import { isBase64, registerDecorator, ValidationArguments, ValidationOptions, ValidatorConstraint, ValidatorConstraintInterface } from 'class-validator'; export function IsBase64Custom(validationOptions?: ValidationOptions) { return (object: any, propertyName: string) => { registerDecorator({ target: object.constructor, propertyName, options: validationOptions, constraints: [], validator: IsBase64CustomConstraint, }); }; } @ValidatorConstraint({ name: 'IsBase64Custom', }) export class IsBase64CustomConstraint implements ValidatorConstraintInterface { validate(value: any, validationArguments?: ValidationArguments) { if ( value === null || value === undefined || isBase64(value) ) { //isBase64() true if string is "" or base64 return true } else { return false } } defaultMessage(validationArguments?: ValidationArguments): string { return 'validation.INVALID_BASE64' } }