protected override IEnumerable <Results.ValidationFailure> InvokePropertyValidator(ValidationContext context, Validators.IPropertyValidator validator, string propertyName) { var propertyContext = new PropertyValidatorContext(context, this, propertyName); var results = new List <ValidationFailure>(); var delegatingValidator = validator as IDelegatingValidator; if (delegatingValidator == null || delegatingValidator.CheckCondition(propertyContext.Instance)) { var collectionPropertyValue = propertyContext.PropertyValue as IEnumerable <TProperty>; int count = 0; if (collectionPropertyValue != null) { foreach (var element in collectionPropertyValue) { var newContext = context.CloneForChildValidator(context.InstanceToValidate); newContext.PropertyChain.Add(propertyName); newContext.PropertyChain.AddIndexer(count++); var newPropertyContext = new PropertyValidatorContext(newContext, this, newContext.PropertyChain.ToString(), element); results.AddRange(validator.Validate(newPropertyContext)); } } } return(results); }
/// <summary> /// Invokes the validator asynchronously /// </summary> /// <param name="context"></param> /// <param name="validator"></param> /// <param name="propertyName"></param> /// <param name="cancellation"></param> /// <returns></returns> protected override Task <IEnumerable <ValidationFailure> > InvokePropertyValidatorAsync(ValidationContext context, IPropertyValidator validator, string propertyName, CancellationToken cancellation) { var propertyContext = new PropertyValidatorContext(context, this, propertyName); var results = new List <ValidationFailure>(); var delegatingValidator = validator as IDelegatingValidator; if (delegatingValidator == null || delegatingValidator.CheckCondition(propertyContext.ParentContext)) { var collectionPropertyValue = propertyContext.PropertyValue as IEnumerable <TProperty>; if (collectionPropertyValue != null) { var validators = collectionPropertyValue.Select((v, count) => { var newContext = context.CloneForChildValidator(context.InstanceToValidate); newContext.PropertyChain.Add(propertyName); newContext.PropertyChain.AddIndexer(count); var newPropertyContext = new PropertyValidatorContext(newContext, this, newContext.PropertyChain.ToString(), v); return(validator.ValidateAsync(newPropertyContext, cancellation) .Then(fs => results.AddRange(fs))); }); return (TaskHelpers.Iterate( validators, cancellationToken: cancellation ).Then(() => results.AsEnumerable(), runSynchronously: true)); } } return(TaskHelpers.FromResult(Enumerable.Empty <ValidationFailure>())); }
protected virtual IValidationContext CreateNewValidationContextForChildValidator(ValidationContext <T> context, TProperty value) { var selector = GetSelector(context, value); var newContext = context.CloneForChildValidator(value, PassThroughParentContext, selector); if (!context.IsChildCollectionContext) { newContext.PropertyChain.Add(context.RawPropertyName); } return(newContext); }