结合 FluentValidation 和 IValidatableObject 错误

Combining FluentValidation and IValidatableObject Errors

是否可以在同一个 ViewModel 中 运行 同时使用 FluentValidation IValidatableObject?例如,如果我有以下 ViewModel:

public class TestViewModel
{
    public Foo Foo { get; set; }
    public Bar Bar { get; set; }
}

public class Foo : IValidatableObject
{
    public string FooName { get; set; }

    public IEnumerable<ValidationResult> Validate(System.ComponentModel.DataAnnotations.ValidationContext validationContext)
    {
        if (string.IsNullOrEmpty(FooName))
        {
            yield return new ValidationResult("Required from IValidatableObject", new[] { "FooName" });
        }
    }
}

[Validator(typeof(BarValidator))]
public class Bar
{
    public string BarName { get; set; }
}

public class BarValidator : AbstractValidator<Bar>
{
    public BarValidator() {
        RuleFor(x => x.BarName).NotNull().WithMessage("Required from FluentValidation");
    }
}

当我的控制器调用 ModelState.IsValid 时,FooBar 验证是否可以 运行 和 return 结果?

答案是否定的,目前没有。在此处查看回复:https://github.com/JeremySkinner/FluentValidation/issues/545#issuecomment-322757974