UNPKG

create-chuntianxiaozhu

Version:

春天小猪模板工具

41 lines (37 loc) 1.07 kB
import { ValidationArguments, ValidationOptions, ValidatorConstraint, ValidatorConstraintInterface, registerDecorator, } from 'class-validator'; export function IsCommaSplit(validationOptions?: ValidationOptions) { return function (object: Object, propertyName: string) { registerDecorator({ name: 'isCommaSplit', target: object.constructor, propertyName: propertyName, options: validationOptions, validator: { validate(value: any) { return typeof value === 'string' && value.split(',').length > 0; // you can return a Promise<boolean> here as well, if you want to make async validation }, }, }); }; } /** * 自定义类校验器 */ @ValidatorConstraint({ name: 'customIsEnum', async: false }) export class CustomIsEnum implements ValidatorConstraintInterface { validate(value: any, args?: ValidationArguments): boolean { if (!value) { return false; } return args.constraints.indexOf(value) > -1; } defaultMessage(): string { return '值不对'; } }