/** * Author: Đào Quốc Huy / huydaoquoc88@gmail.com * Description: Accept string, empty */ import { registerDecorator, ValidationArguments, ValidationOptions, ValidatorConstraint, ValidatorConstraintInterface } from 'class-validator'; /** * @Example `@IsString_I18n()`. * @Return true if not set * @Require i18n translate: "validation.INVALID_STRING" */ export function IsString_I18n(validationOptions?: ValidationOptions) { return (object: any, propertyName: string) => { registerDecorator({ target: object.constructor, propertyName, options: validationOptions, constraints: [], validator: IsString_I18nConstraint, }); }; } @ValidatorConstraint({ name: 'IsString_I18n' }) export class IsString_I18nConstraint implements ValidatorConstraintInterface { validate(value: any, validationArguments?: ValidationArguments) { if((value !== undefined && value !== null ) && typeof value === "string" ){ return true } else { return false } } defaultMessage(validationArguments?: ValidationArguments): string { return "validation.INVALID_STRING" } }