尝试重写我的自定义规则以建议新的7.2 FluentValidation语法,并收到以下错误:
NullReferenceException at ServiceStack.FluentValidation.Internal.PropertyRule,ServiceStack.FluentValidation.Resources.LazyStringSource.GetString(Object context)。
设置显示规则的三种不同尝试。第一个是旧的,有效的,第二个是我尝试重写,但崩溃时出现上述错误。
//works but marked obsolete
Custom(rpt =>
{
return DateLastObservedValidator(rpt)
? null
: new ValidationFailure("DateLastObserved", "invalid date") { ErrorCode = "BadDate" };
});
//fails with nullreference exception
RuleFor(rpt => rpt)
.Custom((rpt, context) =>
{
if (!DateLastObservedValidator(rpt))
{
context.AddFailure(new ValidationFailure("DateLastObserved", error: "invalid date") { ErrorCode = "BadDate" });
}
});
//fails with nullreference exception
RuleFor(rpt => rpt)
.Must(rpt => DateLastObservedValidator(rpt))
.WithMessage("invalid date")
.WithErrorCode("BadDate");
function DateLastObservedValidator是在验证器类中声明的私有bool。