1
0

task.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {
  2. Length,
  3. IsString,
  4. IsIn,
  5. IsDate,
  6. IsInt,
  7. Validate,
  8. IsOptional,
  9. ValidateIf,
  10. ValidatorConstraint,
  11. ValidatorConstraintInterface,
  12. ValidationArguments,
  13. } from 'class-validator';
  14. import { Expose, Transform } from 'class-transformer';
  15. import * as parser from 'cron-parser';
  16. // cron 表达式验证,bull lib下引用了cron-parser
  17. @ValidatorConstraint({ name: 'isCronExpression', async: false })
  18. export class IsCronExpression implements ValidatorConstraintInterface {
  19. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  20. validate(text: string, args: ValidationArguments) {
  21. try {
  22. const op: any = { iterator: true };
  23. let needOp = false;
  24. if ((args.object as any).startTime) {
  25. needOp = true;
  26. op.startDate = (args.object as any).startTime;
  27. }
  28. if ((args.object as any).endTime) {
  29. needOp = true;
  30. op.endDate = (args.object as any).endTime;
  31. }
  32. if (needOp) {
  33. const tmp = parser.parseExpression(text, op);
  34. if (!tmp.hasNext()) {
  35. throw new Error();
  36. }
  37. } else {
  38. parser.parseExpression(text);
  39. }
  40. return true;
  41. } catch (e) {
  42. return false;
  43. }
  44. }
  45. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  46. defaultMessage(_args: ValidationArguments) {
  47. // here you can provide default error message if validation failed
  48. return 'this cron expression ($value) invalid';
  49. }
  50. }
  51. export class CreateTaskDto {
  52. @Length(2, 50)
  53. @Expose()
  54. name: string;
  55. @IsString()
  56. @Expose()
  57. service: string;
  58. @IsIn([ 0, 1 ])
  59. @Expose()
  60. type: number;
  61. @IsIn([ 0, 1, 2 ])
  62. @Expose()
  63. status: number;
  64. @ValidateIf((_o, v) => { return !(v === '' || v === undefined || v === null); })
  65. @IsDate()
  66. @Expose()
  67. @Transform(value => { if (value) { return new Date(value as any); } return null; }, { toClassOnly: true })
  68. startTime: Date;
  69. @ValidateIf((_o, v) => { return !(v === '' || v === undefined || v === null); })
  70. @IsDate()
  71. @Expose()
  72. @Transform(value => { if (value) { return new Date(value as any); } return null; }, { toClassOnly: true })
  73. endTime: Date;
  74. @IsInt()
  75. @IsOptional()
  76. @Expose()
  77. limit: number;
  78. @ValidateIf(o => { return o.type === 0; })
  79. @Validate(IsCronExpression)
  80. @Expose()
  81. cron: string;
  82. @IsInt()
  83. @ValidateIf(o => { return o.type === 1; })
  84. @Expose()
  85. every: number;
  86. @IsOptional()
  87. @IsString()
  88. @Expose()
  89. data: string;
  90. @IsOptional()
  91. @IsString()
  92. @Expose()
  93. remark: string;
  94. }
  95. export class UpdateTaskDto extends CreateTaskDto {
  96. @IsInt()
  97. @Expose()
  98. id: number;
  99. }
  100. export class CheckIdTaskDto {
  101. @IsInt()
  102. @Expose()
  103. id: number;
  104. }