I am using IValidatableObject validation for entities with e.g. following code:
public class OuterObj : IValidatableObject
{
public int ID { get; set; }
public string Name { get; set; }
public IEnumerable<InnerObj> InnerObjList { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (this.ID <= 0)
{
yield return new ValidationResult("", new[] { nameof(this.ID) });
}
}
}
public class InnerObj : IValidatableObject
{
public int ID { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (this.ID <= 0)
{
yield return new ValidationResult("", new[] { nameof(this.ID) });
}
}
}
In this case when I am validating the outerObj, when there are innerObj present it validates only the innerobj and not the outerobj. It doesn't reach the outerobj validate method in case of presence of innerobj.
I would like to validate both when innerobj present. Please help me with how its done. Why does it not validate the outerobj?