[英]Creating custom validator in nest js
This is the shape of data:这是数据的形状:
{
"saturday": [
{
"start": "10:00 AM",
"end": "12:00 AM"
}
],
"sunday": [],
...
}
this is my time-slot.interface :这是我的time-slot.interface :
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[]>;
and this is my time-slot.dot :这是我的time-slot.dot :
import { IsNotEmpty, ValidateNested } from 'class-validator';
import { TimeSlots } from '../interfaces/time-slots.interface';
export class DoctorTimeSlotsDto {
@IsNotEmpty()
@ValidateNested()
timeSlots: TimeSlots;
}
how can i make this custom validation?我怎样才能进行这个自定义验证? timeSlots is a nested object of objects & arrays.
timeSlots 是对象和数组的嵌套对象。
I solved it:我解决了:
@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[];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.