在nest-js中创建自定义验证器



这是数据的形状:

{
"saturday": [
{
"start": "10:00 AM",
"end": "12:00 AM"
}
],
"sunday": [],
...
}

这是我的时隙。接口

export enum DaysEnum {
SATURDAY = 'saturday',
SUNDAY = 'sunday',
MONDAY = 'monday',
TUESDAY = 'tuesday',
WEDNESDAY = 'wednesday',
THURSDAY = 'thursday',
FRIDAY = 'friday',
}
export interface TimeSlot {
start: Date;
end: Date;
}
export type TimeSlots = Record<DaysEnum, TimeSlot[]>;

这是我的时隙。dot

import { IsNotEmpty, ValidateNested } from 'class-validator';
import { TimeSlots } from '../interfaces/time-slots.interface';
export class DoctorTimeSlotsDto {
@IsNotEmpty()
@ValidateNested()
timeSlots: TimeSlots;
}

如何进行自定义验证?timeSlots是对象的嵌套对象&阵列。

我解决了它:

@IsNotEmpty()
@IsDate()
@Type(() => Date)
start: Date;
@IsNotEmpty()
@IsDate()
@Type(() => Date)
@IsDateGreaterThan('start', { message: 'end must be greater than start' })
end: Date;
}
export class DoctorTimeSlotsDto {
@IsNotEmpty()
@ValidateNested()
@Type(() => TimeSlot)
[DaysEnum.SATURDAY]: TimeSlot[];
@IsNotEmpty()
@ValidateNested()
@Type(() => TimeSlot)
[DaysEnum.SUNDAY]: TimeSlot[];
@IsNotEmpty()
@ValidateNested()
@Type(() => TimeSlot)
[DaysEnum.MONDAY]: TimeSlot[];
@IsNotEmpty()
@ValidateNested()
@Type(() => TimeSlot)
[DaysEnum.TUESDAY]: TimeSlot[];
@IsNotEmpty()
@ValidateNested()
@Type(() => TimeSlot)
[DaysEnum.WEDNESDAY]: TimeSlot[];
@IsNotEmpty()
@ValidateNested()
@Type(() => TimeSlot)
[DaysEnum.THURSDAY]: TimeSlot[];
@IsNotEmpty()
@ValidateNested()
@Type(() => TimeSlot)
[DaysEnum.FRIDAY]: TimeSlot[];
}

相关内容

  • 没有找到相关文章

最新更新