public virtual List <ValidationResult> GetErrors(object validatingObject, string name = null, bool allowNull = false) { if (validatingObject == null) { if (allowNull) { return(new List <ValidationResult>()); //TODO: Returning an array would be more performent } else { return(new List <ValidationResult> { name == null ? new ValidationResult("Given object is null!") : new ValidationResult(name + " is null!", new[] { name }) }); } } var context = new ObjectValidationContext(validatingObject); using (var scope = ServiceScopeFactory.CreateScope()) { foreach (var contributorType in Options.ObjectValidationContributors) { var contributor = (IObjectValidationContributor) scope.ServiceProvider.GetRequiredService(contributorType); contributor.AddErrors(context); } } return(context.Errors); }
public void AddErrors(ObjectValidationContext context) { ValidateObjectRecursively(context.Errors, context.ValidatingObject, currentDepth: 1); }