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 Task AddErrorsAsync(ObjectValidationContext context) { if (context.ValidatingObject is not IDynamicQueryInput dynamicQueryInput) { return(Task.CompletedTask); } dynamicQueryInput.FilterGroup?.Travel((_, condition) => { if (!_regFieldName.IsMatch(condition.FieldName)) { context.Errors.Add(new ValidationResult($"InvalidFieldName: {condition.FieldName}", new[] { nameof(DynamicQueryCondition.FieldName) })); } }); return(Task.CompletedTask); }
public void AddErrors(ObjectValidationContext context) { //Get the validating object var obj = context.ValidatingObject; if (obj is CreateBookDto) { var bookDto = obj as CreateBookDto; if (bookDto.Price == 8) { var result = new ValidationResult("Price is not equal to 8", new string[] { nameof(bookDto.Price) }); //Add the validation errors if available context.Errors.Add(result); } } }
public void AddErrors(ObjectValidationContext context) { if (!(context.ValidatingObject is IDynamicQueryInput dynamicQueryInput)) { return; } if (dynamicQueryInput.FilterGroup != null) { dynamicQueryInput.FilterGroup.Travel((_, condition) => { if (!_regFieldName.IsMatch(condition.FieldName)) { context.Errors.Add(new ValidationResult($"InvalidFieldName: {condition.FieldName}", new[] { nameof(DynamicQueryCondition.FieldName) })); } }); } }
public void AddErrors(ObjectValidationContext context) { var serviceType = typeof(IValidator <>).MakeGenericType(context.ValidatingObject.GetType()); if (!(_serviceProvider.GetService(serviceType) is IValidator validator)) { return; } var result = validator.Validate((IValidationContext)context.ValidatingObject); if (!result.IsValid) { context.Errors.AddRange( result.Errors.Select( error => new ValidationResult(error.ErrorMessage, new[] { error.PropertyName }) ) ); } }
public void AddErrors(ObjectValidationContext context) { var serviceType = typeof(IValidator <>).MakeGenericType(context.ValidatingObject.GetType()); var validator = _serviceProvider.GetService(serviceType) as IValidator; if (validator == null) { return; } var result = validator.Validate(context.ValidatingObject); if (!result.IsValid) { context.Errors.AddRange( result.Errors.Select( error => new ValidationResult(error.ErrorMessage) ) ); } }
public virtual async Task AddErrorsAsync(ObjectValidationContext context) { var serviceType = typeof(IValidator <>).MakeGenericType(context.ValidatingObject.GetType()); var validator = _serviceProvider.GetService(serviceType) as IValidator; if (validator == null) { return; } var result = await validator.ValidateAsync((IValidationContext)Activator.CreateInstance( typeof(ValidationContext <>).MakeGenericType(context.ValidatingObject.GetType()), context.ValidatingObject)); if (!result.IsValid) { context.Errors.AddRange( result.Errors.Select( error => new ValidationResult(error.ErrorMessage, new[] { error.PropertyName }) ) ); } }
public Task AddErrorsAsync(ObjectValidationContext context) { ValidateObjectRecursively(context.Errors, context.ValidatingObject, currentDepth: 1); return(Task.CompletedTask); }