Los*_*ost 8 .net c# validation fluentvalidation
我正在使用FluentValidation进行服务器端验证.现在我已经让它在使用Must验证之前调用了一个函数:
RuleFor(x => x.UserProfile).Must(ValidateProfile).WithMessage("We are sorry, you have already logged on " + DateTime.Now + ". Please come again tomorrow.");
Run Code Online (Sandbox Code Playgroud)
现在,这是有效的,因为validateProfile采用的唯一参数是UserProfile.一切都很好.
我现在的问题是我正在尝试使用两个参数验证数据的函数.我尝试用于验证的函数如下所示:
bool IsValid(string promocode, IUserProfile userProfile)
Run Code Online (Sandbox Code Playgroud)
现在,我不确定如何将IsValid绑定到fluentValidation.有任何想法吗?
Jap*_*ple 15
promocode来自哪里?葡萄汁方法重载接受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); })
Run Code Online (Sandbox Code Playgroud)
小智 6
//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();
}
Run Code Online (Sandbox Code Playgroud)