[英]Have FluentValidation call a function with multiple parameters
我正在使用 FluentValidation 进行服务器端验证。 现在我已经让它在必须验证之前调用了一个函数:
RuleFor(x => x.UserProfile).Must(ValidateProfile).WithMessage("We are sorry, you have already logged on " + DateTime.Now + ". Please come again tomorrow.");
现在,这是可行的,因为validateProfile 采用的唯一参数是UserProfile。 一切都很好。
我现在的问题是我试图让一个带有两个参数的函数验证数据。我试图用于验证的函数如下所示:
bool IsValid(string promocode, IUserProfile userProfile)
现在,我不确定如何将 IsValid 绑定到 fluentValidation。 有任何想法吗?
促销代码从何而来? Must 方法具有接受Func<TProp,bool>
、 Func<T,TProp,bool>
和Func<T,TProp, PropertyValidatorContext, bool>
如果 promocode 是被验证对象的属性,则很容易传递类似
.RuleFor(x => x.UserProfile).Must( (o, userProfile) => { return IsValid(o.promoCode, userProfile); })
//with MustAsync
RuleFor(v => v.UserId).MustAsync(
async (model, userId, cancellation) =>
{
return await IsValid(model.PromoCode, userId, cancellation);
}
).WithMessage("{PropertyName} message.");
private async Task<bool> IsUniqueUserNameAsync(string promoCode, string userId, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.