簡體   English   中英

FluentValidation - 僅當值不是 null 時,檢查值才是表達式

[英]FluentValidation - Check value is an expression only if the value is not null

我正在嘗試驗證 Cron 表達式僅在屬性不是 null 時才有效

我的代碼看起來像這樣,

 RuleFor(team => team.PlayTimeSlot)
                .Must(IsValidSchedule)
                .When(team => !string.IsNullOrEmpty(team.PlayTimeSlot));

 public bool IsValidSchedule(string schedule)
        {
        // Some Schedule validation logic
        }

但是,這不起作用。 我錯過了什么?

我會嘗試將所有內容分成方法作為替代方法,如下所示:

RuleFor(team => team.PlayTimeSlot)
        .When(playTimeValid)
        .Must(IsValidSchedule);

public bool isValidSchedule()
{
    return true
}
public bool playTimeValid(TeamModel team)
{
    if !string.isnullorempty(team.playtimeslot)
    return true
}

你能用你的代碼試試嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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