【发布时间】:2020-02-25 22:19:48
【问题描述】:
我想知道是否有人可以帮助我,因为我只是在学习。我正在尝试使用 FluentValidation 来验证日期时间列表,以提高我的技能,但我似乎无法解决问题,我似乎可以找到我正在尝试做的事情的示例。基本上,我想做的是:
- 检查开始是否在结束之前(好的)
- 检查是否开始时间和时间不相等(好)
- 检查没有重叠的开始和结束在同一天。 (做不到)
如果有人能帮助我,我将不胜感激。代码如下
public class Room
{
public DateTime RoomBooked { get; set; }
public List<RoomSchedule> Schedule { get; set; }
}
public class RoomSchedule
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
public class RoomValidator : AbstractValidator<Room>
{
public RoomValidator()
{
RuleFor(o => o.RoomBooked)
.NotEmpty().WithMessage("Booking can not be empty");
RuleForEach(x => x.Schedule)
.SetValidator(new RoomScheduleValidator());
}
}
public class RoomScheduleValidator : AbstractValidator<RoomSchedule>
{
public RoomScheduleValidator()
{
RuleFor(o => o.Start)
.NotEmpty().WithMessage("Start time required.")
.Equal(m => m.End).WithMessage("Start time can not be the same as the end time.");
RuleFor(m => m.End)
.NotEmpty().WithMessage("End time required.")
.GreaterThan(m => m.Start)
.WithMessage("End time can not be before start time.");
}
}
【问题讨论】:
标签: c# asp.net-mvc .net-core fluentvalidation