ASP.NET 复杂属性中的 FluentValidation
FluentValidation in ASP.NET complex properties
我有以下情况。我有一个class,例如
public class Person
{
public SchoolYear FirstYear {get; set;}
public SchoolYear SecondYear {get; set;}
}
public class SchoolYear
{
public string YearStartDate {get; set;}
public string YearEndDate {get; set;}
}
我想确保第二学年的开始是在第一学年结束后的一天,我想用 FluentValidation 库来完成。我该怎么做?
请您检查一下。
public class PersonValidator : AbstractValidator<Person>
{
public PersonValidator()
{
RuleFor(x => x.SecondYear).Must((p, s)=> {
var firstYear = p.FirstYear;
var secondYear = p.SecondYear;
return Convert.ToDateTime(secondYear.YearStartDate) == Convert.ToDateTime(firstYear.YearEndDate).AddDays(1);
})
.WithMessage("second school year should be ONE DAY AFTER firstYear YearEndDate ");
}
}
有关自定义验证器的更多详细信息follow。
我有以下情况。我有一个class,例如
public class Person
{
public SchoolYear FirstYear {get; set;}
public SchoolYear SecondYear {get; set;}
}
public class SchoolYear
{
public string YearStartDate {get; set;}
public string YearEndDate {get; set;}
}
我想确保第二学年的开始是在第一学年结束后的一天,我想用 FluentValidation 库来完成。我该怎么做?
请您检查一下。
public class PersonValidator : AbstractValidator<Person>
{
public PersonValidator()
{
RuleFor(x => x.SecondYear).Must((p, s)=> {
var firstYear = p.FirstYear;
var secondYear = p.SecondYear;
return Convert.ToDateTime(secondYear.YearStartDate) == Convert.ToDateTime(firstYear.YearEndDate).AddDays(1);
})
.WithMessage("second school year should be ONE DAY AFTER firstYear YearEndDate ");
}
}
有关自定义验证器的更多详细信息follow。