C# FluentValidation如何验证孙子

C# FluentValidation如何验证孙子,c#,fluentvalidation,C#,Fluentvalidation,下面是我的三级课程(员工>项目>队友)。我能够验证子(项目)的状态,其中仅当项目列表不为空时才进行验证 但是在队友级别,我不知道如何检查TeamAtName属性。基本上,我想对孙子也这样做,如果有队友,请确保检查队友名称是否为空或空 谢谢 public class Employee { public int Id { get; set; } public string Name { get; set; } public List<ProjectsD

下面是我的三级课程(员工>项目>队友)。我能够验证子(项目)的状态,其中仅当项目列表不为空时才进行验证

但是在队友级别,我不知道如何检查TeamAtName属性。基本上,我想对孙子也这样做,如果有队友,请确保检查队友名称是否为空或空

谢谢

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }        

    public List<ProjectsDto> Projects { get; set; }
}

public class ProjectsDto
{
    public string Status { get; set; }
      
    public List<TeammatesDto> Teammates { get; set; }
}

public class TeammatesDto
{
    public string TeammateName { get; set; }
    public string PreviousProject { get; set; }
}

public class CreateEmployeeCommandValidator : AbstractValidator<CreateEmployeeCommand>
{
    private readonly IApplicationDbContext _context;

    public CreateEmployeeCommandValidator(IApplicationDbContext context)
    {
        _context = context;         

        RuleFor(v => v.Name)
            .NotEmpty().WithMessage("Name is required.")
            .MaximumLength(30).WithMessage("Name must not exceed 30 characters.");

        RuleFor(v => v.Projects)
            .ForEach(projectRule => {
                projectRule.Must(item => item.Status == null).WithMessage("Status is required");
            })
       .When(v => !StringUtil.IsNullOrEmptyList(v.Projects));

    }
公共类员工
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表项目{get;set;}
}
公共类项目
{
公共字符串状态{get;set;}
公共列表队友{get;set;}
}
公开课
{
公共字符串名称{get;set;}
公共字符串PreviousProject{get;set;}
}
公共类CreateEmployeeCommandValidator:AbstractValidator
{
私有只读IApplicationDbContext\u上下文;
公共CreateEmployeeCommandValidator(IApplicationDbContext上下文)
{
_上下文=上下文;
规则(v=>v.Name)
.NotEmpty().WithMessage(“需要名称”)
.最大长度(30)。WithMessage(“名称不得超过30个字符”);
规则(v=>v.Projects)
.ForEach(项目规则=>{
projectRule.Must(item=>item.Status==null).WithMessage(“Status is required”);
})
.When(v=>!StringUtil.IsNullOrEmptyList(v.Projects));
}

我认为最好的、更有条理的选项是使用,您可以在foreach规则中使用,也可以仅用于子对象,因此代码如下所示:


        RuleFor(v => v.Name)
            .NotEmpty()
                .WithMessage("Name is required.")
            .MaximumLength(30)
                .WithMessage("Name must not exceed 30 characters.");

        RuleForEach(v => v.Projects)
            .SetValidator(new YourProjectsValidator());
RuleForEach(v => v.Teammates)
            .SetValidator(new YourTeammatesValidator());
在您的项目验证程序中,您可以像下面这样调用队友验证程序:


        RuleFor(v => v.Name)
            .NotEmpty()
                .WithMessage("Name is required.")
            .MaximumLength(30)
                .WithMessage("Name must not exceed 30 characters.");

        RuleForEach(v => v.Projects)
            .SetValidator(new YourProjectsValidator());
RuleForEach(v => v.Teammates)
            .SetValidator(new YourTeammatesValidator());

我认为最好且更有条理的选项是使用,您可以在foreach规则中使用,也可以仅用于子对象,因此代码如下所示:


        RuleFor(v => v.Name)
            .NotEmpty()
                .WithMessage("Name is required.")
            .MaximumLength(30)
                .WithMessage("Name must not exceed 30 characters.");

        RuleForEach(v => v.Projects)
            .SetValidator(new YourProjectsValidator());
RuleForEach(v => v.Teammates)
            .SetValidator(new YourTeammatesValidator());
在您的项目验证程序中,您可以像下面这样调用队友验证程序:


        RuleFor(v => v.Name)
            .NotEmpty()
                .WithMessage("Name is required.")
            .MaximumLength(30)
                .WithMessage("Name must not exceed 30 characters.");

        RuleForEach(v => v.Projects)
            .SetValidator(new YourProjectsValidator());
RuleForEach(v => v.Teammates)
            .SetValidator(new YourTeammatesValidator());