/// <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);
        }
Example #2
0
        /// <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 async Task <IEnumerable <ValidationFailure> > InvokePropertyValidatorAsync(ValidationContext context, IPropertyValidator validator, string propertyName, CancellationToken cancellation)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                propertyName = InferPropertyName(Expression);
            }

            var propertyContext     = new PropertyValidatorContext(context, this, propertyName);
            var delegatingValidator = validator as IDelegatingValidator;

            if (delegatingValidator == null || delegatingValidator.CheckCondition(propertyContext))
            {
                var collectionPropertyValue = propertyContext.PropertyValue as IEnumerable <TProperty>;

                if (collectionPropertyValue != null)
                {
                    if (string.IsNullOrEmpty(propertyName))
                    {
                        throw new InvalidOperationException("Could not automatically determine the property name ");
                    }

                    var validatorTasks = collectionPropertyValue.Select(async(v, count) => {
                        if (Filter != null && !Filter(v))
                        {
                            return(Enumerable.Empty <ValidationFailure>());
                        }

                        string indexer             = count.ToString();
                        bool useDefaultIndexFormat = true;

                        if (IndexBuilder != null)
                        {
                            indexer = IndexBuilder(context.InstanceToValidate, collectionPropertyValue, v, count);
                            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(), v);

                        return(await validator.ValidateAsync(newPropertyContext, cancellation));
                    });

                    var results = new List <ValidationFailure>();

                    foreach (var task in validatorTasks)
                    {
                        var failures = await task;
                        results.AddRange(failures);
                    }

                    return(results);
                }
            }

            return(Enumerable.Empty <ValidationFailure>());
        }
        /// <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);

            if (validator.Options.Condition != null && !validator.Options.Condition(propertyContext))
            {
                return(Enumerable.Empty <ValidationFailure>());
            }

            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);
                    newPropertyContext.MessageFormatter.AppendArgument("CollectionIndex", index);
                    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)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                propertyName = InferPropertyName(Expression);
            }

            var propertyContext     = new PropertyValidatorContext(context, this, propertyName);
            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 ");
                    }

                    var results         = new List <ValidationFailure>();
                    var validationTasks = new List <Task>();

                    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);

                        validationTasks.Add(
                            validator.ValidateAsync(newPropertyContext, cancellation)
                            .Then(f => results.AddRange(f), cancellation)
                            );
                    }

                    return(TaskHelpers.Iterate(validationTasks, cancellation)
                           .Then(() => results.AsEnumerable(), runSynchronously: true, cancellationToken: cancellation));
                }
            }

            return(TaskHelpers.FromResult(Enumerable.Empty <ValidationFailure>()));
        }
        /// <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))
            {
                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;
                        }

                        var newContext = context.CloneForChildCollectionValidator(context.InstanceToValidate, preserveParentContext: true);
                        newContext.PropertyChain.Add(propertyName);
                        newContext.PropertyChain.AddIndexer(index);

                        var newPropertyContext = new PropertyValidatorContext(newContext, this, newContext.PropertyChain.ToString(), element);

                        results.AddRange(validator.Validate(newPropertyContext));
                    }
                }
            }

            return(results);
        }
        protected override IEnumerable <ValidationFailure> InvokePropertyValidator(ValidationContext context, 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))
            {
                var collectionPropertyValue = propertyContext.PropertyValue as IEnumerable <TProperty>;

                var otherCollection = GetOtherCollection(context, propertyContext);

                var preValidationResult = PreValidateCollections(propertyName, collectionPropertyValue, otherCollection);

                if (preValidationResult != null)
                {
                    return(preValidationResult);
                }

                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++;

                        string indexer = index.ToString();

                        var matchingValue = GetMatchingValue(propertyName, element, otherCollection, out ICollection <ValidationFailure> errors);

                        if (errors != null)
                        {
                            return(errors);
                        }

                        var newContext = context.CloneForChildCollectionValidator(context.InstanceToValidate, preserveParentContext: true);
                        newContext.PropertyChain.Add(propertyName);
                        newContext.PropertyChain.AddIndexer(indexer, true);

                        newContext.RootContextData["_FDE_ComparisonSource"] = matchingValue;

                        var newPropertyContext = new PropertyValidatorContext(newContext, this, newContext.PropertyChain.ToString(), element);

                        results.AddRange(validator.Validate(newPropertyContext));
                    }
                }
            }

            return(results);
        }
        protected override async Task <IEnumerable <ValidationFailure> > InvokePropertyValidatorAsync(ValidationContext context, IPropertyValidator validator, string propertyName, CancellationToken cancellation)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                propertyName = InferPropertyName(Expression);
            }

            var propertyContext     = new PropertyValidatorContext(context, this, propertyName);
            var delegatingValidator = validator as IDelegatingValidator;

            if (delegatingValidator == null || delegatingValidator.CheckCondition(propertyContext))
            {
                var collectionPropertyValue = propertyContext.PropertyValue as IEnumerable <TProperty>;

                var otherCollection = GetOtherCollection(context, propertyContext);

                var preValidationResult = PreValidateCollections(propertyName, collectionPropertyValue, otherCollection);

                if (preValidationResult != null)
                {
                    return(preValidationResult);
                }

                if (collectionPropertyValue != null)
                {
                    if (string.IsNullOrEmpty(propertyName))
                    {
                        throw new InvalidOperationException("Could not automatically determine the property name ");
                    }

                    var validatorTasks = collectionPropertyValue.Select(async(v, count) =>
                    {
                        string indexer = count.ToString();

                        var matchingValue = GetMatchingValue(propertyName, v, otherCollection, out ICollection <ValidationFailure> errors);

                        if (errors != null)
                        {
                            return(errors);
                        }

                        var newContext = context.CloneForChildCollectionValidator(context.InstanceToValidate, preserveParentContext: true);
                        newContext.PropertyChain.Add(propertyName);
                        newContext.PropertyChain.AddIndexer(indexer, true);

                        newContext.RootContextData["_FDE_ComparisonSource"] = matchingValue;

                        var newPropertyContext = new PropertyValidatorContext(newContext, this, newContext.PropertyChain.ToString(), v);

                        return(await validator.ValidateAsync(newPropertyContext, cancellation));
                    });

                    var results = new List <ValidationFailure>();

                    foreach (var task in validatorTasks)
                    {
                        var failures = await task;
                        results.AddRange(failures);
                    }

                    return(results);
                }
            }

            return(Enumerable.Empty <ValidationFailure>());
        }