Пример #1
0
        public virtual async Task <List <ValidationResult> > GetErrorsAsync(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);
                    await contributor.AddErrorsAsync(context);
                }
            }

            return(context.Errors);
        }
 public void AddErrors(ObjectValidationContext context)
 {
     ValidateObjectRecursively(context.Errors, context.ValidatingObject, currentDepth: 1);
 }
Пример #3
0
 public Task AddErrorsAsync(ObjectValidationContext context)
 {
     ValidateObjectRecursively(context.Errors, context.ValidatingObject, currentDepth: 1);
     return(Task.CompletedTask);
 }