/// <summary> /// Invokes the validator /// </summary> /// <param name="context"></param> /// <param name="validator"></param> /// <param name="propertyName"></param> /// <returns></returns> protected override IEnumerable <Results.ValidationFailure> InvokePropertyValidator(ValidationContext context, Validators.IPropertyValidator validator, string propertyName) { if (string.IsNullOrEmpty(propertyName)) { propertyName = InferPropertyName(Expression); } var propertyContext = new PropertyValidatorContext(context, this, propertyName); var results = new List <ValidationFailure>(); var collectionPropertyValue = propertyContext.PropertyValue as IEnumerable <TProperty>; int count = 0; if (collectionPropertyValue != null) { if (string.IsNullOrEmpty(propertyName)) { throw new InvalidOperationException("Could not automatically determine the property name "); } foreach (var element in collectionPropertyValue) { int index = count++; if (Filter != null && !Filter(element)) { continue; } string indexer = index.ToString(); bool useDefaultIndexFormat = true; if (IndexBuilder != null) { indexer = IndexBuilder(context.InstanceToValidate, collectionPropertyValue, element, index); useDefaultIndexFormat = false; } var newContext = context.CloneForChildCollectionValidator(context.InstanceToValidate, preserveParentContext: true); newContext.PropertyChain.Add(propertyName); newContext.PropertyChain.AddIndexer(indexer, useDefaultIndexFormat); var newPropertyContext = new PropertyValidatorContext(newContext, this, newContext.PropertyChain.ToString(), element); results.AddRange(validator.Validate(newPropertyContext)); } } return(results); }
/// <summary> /// Invokes the validator /// </summary> /// <param name="context"></param> /// <param name="validator"></param> /// <param name="propertyName"></param> /// <returns></returns> 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.ParentContext)) { var collectionPropertyValue = propertyContext.PropertyValue as IEnumerable <TProperty>; int count = 0; if (collectionPropertyValue != null) { foreach (var element in collectionPropertyValue) { var newContext = context.CloneForChildCollectionValidator(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 /// </summary> /// <param name="context"></param> /// <param name="validator"></param> /// <param name="propertyName"></param> /// <returns></returns> protected override IEnumerable <Results.ValidationFailure> InvokePropertyValidator(IValidationContext context, Validators.IPropertyValidator validator, string propertyName) { if (string.IsNullOrEmpty(propertyName)) { propertyName = InferPropertyName(Expression); } PropertyValidatorContext propertyContext; // TODO: For FV10 this will come as a parameter rather than in RootContextData. if (context.RootContextData.TryGetValue("__FV_CurrentAccessor", out var a) && a is Lazy <object> accessor) { propertyContext = new PropertyValidatorContext(context, this, propertyName, accessor); } else { #pragma warning disable 618 propertyContext = new PropertyValidatorContext(context, this, propertyName); #pragma warning restore 618 } if (!validator.Options.InvokeCondition(propertyContext)) { return(Enumerable.Empty <ValidationFailure>()); } // There's no need to check for the AsyncCondition here. If the validator has an async condition, then // the parent PropertyRule will call InvokePropertyValidatorAsync instead. var results = new List <ValidationFailure>(); var collectionPropertyValue = propertyContext.PropertyValue as IEnumerable <TElement>; int count = 0; if (collectionPropertyValue != null) { if (string.IsNullOrEmpty(propertyName)) { throw new InvalidOperationException("Could not automatically determine the property name "); } var actualContext = ValidationContext <T> .GetFromNonGenericContext(context); foreach (var element in collectionPropertyValue) { int index = count++; if (Filter != null && !Filter(element)) { continue; } string indexer = index.ToString(); bool useDefaultIndexFormat = true; if (IndexBuilder != null) { indexer = IndexBuilder(context.InstanceToValidate, collectionPropertyValue, element, index); useDefaultIndexFormat = false; } ValidationContext <T> newContext = actualContext.CloneForChildCollectionValidator(actualContext.InstanceToValidate, preserveParentContext: true); newContext.PropertyChain.Add(propertyName); newContext.PropertyChain.AddIndexer(indexer, useDefaultIndexFormat); object valueToValidate = element; if (Transformer != null) { valueToValidate = Transformer(element); } var newPropertyContext = new PropertyValidatorContext(newContext, this, newContext.PropertyChain.ToString(), valueToValidate); newPropertyContext.MessageFormatter.AppendArgument("CollectionIndex", index); results.AddRange(validator.Validate(newPropertyContext)); } } return(results); }
/// <summary> /// Invokes the validator /// </summary> /// <param name="context"></param> /// <param name="validator"></param> /// <param name="propertyName"></param> /// <returns></returns> protected override IEnumerable <Results.ValidationFailure> InvokePropertyValidator(ValidationContext context, Validators.IPropertyValidator validator, string propertyName) { if (string.IsNullOrEmpty(propertyName)) { propertyName = InferPropertyName(Expression); } 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>; int count = 0; if (collectionPropertyValue != null) { if (string.IsNullOrEmpty(propertyName)) { throw new InvalidOperationException("Could not automatically determine the property name "); } foreach (var element in collectionPropertyValue) { var newContext = context.CloneForChildCollectionValidator(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 /// </summary> /// <param name="context"></param> /// <param name="validator"></param> /// <param name="propertyName"></param> /// <returns></returns> protected override IEnumerable <Results.ValidationFailure> InvokePropertyValidator(IValidationContext context, Validators.IPropertyValidator validator, string propertyName) { if (string.IsNullOrEmpty(propertyName)) { propertyName = InferPropertyName(Expression); } var propertyContext = new PropertyValidatorContext(context, this, propertyName); if (validator.Options.Condition != null && !validator.Options.Condition(propertyContext)) { return(Enumerable.Empty <ValidationFailure>()); } var results = new List <ValidationFailure>(); var collectionPropertyValue = propertyContext.PropertyValue as IEnumerable <TElement>; int count = 0; if (collectionPropertyValue != null) { if (string.IsNullOrEmpty(propertyName)) { throw new InvalidOperationException("Could not automatically determine the property name "); } var actualContext = ValidationContext <T> .GetFromNonGenericContext(context); foreach (var element in collectionPropertyValue) { int index = count++; if (Filter != null && !Filter(element)) { continue; } string indexer = index.ToString(); bool useDefaultIndexFormat = true; if (IndexBuilder != null) { indexer = IndexBuilder(context.InstanceToValidate, collectionPropertyValue, element, index); useDefaultIndexFormat = false; } ValidationContext <T> newContext = actualContext.CloneForChildCollectionValidator(actualContext.InstanceToValidate, preserveParentContext: true); newContext.PropertyChain.Add(propertyName); newContext.PropertyChain.AddIndexer(indexer, useDefaultIndexFormat); object valueToValidate = element; if (Transformer != null) { valueToValidate = Transformer(element); } var newPropertyContext = new PropertyValidatorContext(newContext, this, newContext.PropertyChain.ToString(), valueToValidate); newPropertyContext.MessageFormatter.AppendArgument("CollectionIndex", index); results.AddRange(validator.Validate(newPropertyContext)); } } return(results); }