簡體   English   中英

讓 FluentValidation 調用具有多個參數的函數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM