Have FluentValidation call a function with multiple parameters
//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();
}
Where is promocode coming from? The Must method has overloads accepting Func<TProp,bool>
, Func<T,TProp,bool>
, and Func<T,TProp, PropertyValidatorContext, bool>
If promocode is a property of the object being validated, it would be easy to pass something like
.RuleFor(x => x.UserProfile).Must( (o, userProfile) => { return IsValid(o.promoCode, userProfile); })