IValidatableObject.Validate doesn't get called

2020-04-10 03:34发布

问题:

I can't find any details on this, but the issue I am having is that Validate function of parent object doesn't get called if Validate call fails on any child properties. Simple scenario below:

public class Child : IValidateObject 
{
    public IEnumerable<ValidationResult> Validate(ValidationContext  validationContext)
    { ... } 
}

public class Parent : IValidatableObject
{ 
    public Child Child { get; set;}
    public IEnumerable<ValidationResult> Validate(ValidationContext  validationContext)
    { ... } 
}

If the validation in the child fails then the Validate function of the parent doesn't get called so you end up having to fix all the child issues first then submit and only then will you see all the validation failures of the parent.

If someone can either help me understand why this is happening or point me to some documents regarding this that would be awesome.

回答1:

According to this code in 2.0.0 branch on GitHub ValidationVisitor will stop validating parent if one of children failed.

In latest dev branch they introduced new property ValidateComplexTypesIfChildValidationFails to control this behavior.

Github issue related to this question. It's not clear to me how to set this new property.

Created new issue on Github to track this issue.