Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
 public void AddErrors(ObjectValidationContext context)
 {
     ValidateObjectRecursively(context.Errors, context.ValidatingObject, currentDepth: 1);
 }