简体   繁体   English

FluentValidation - 仅当值不是 null 时,检查值才是表达式

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

I am trying to Validate that a Cron Expression is valid only when the property is not null我正在尝试验证 Cron 表达式仅在属性不是 null 时才有效

My code looks like this,我的代码看起来像这样,

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

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

However this does not work.但是,这不起作用。 What am I missing?我错过了什么?

I would try to separate everything into methods as an alternative like this:我会尝试将所有内容分成方法作为替代方法,如下所示:

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
}

Can you Try that with your code?你能用你的代码试试吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM