public void WrapperSucceedsIfNotNullValueSucceedsForWrappedValidator()
        {
            var validator = new TestValidator();
            var wrapperValidator = new NullIgnoringValidatorWrapper(validator);
            object value = "test value";

            var rawResults = validator.Validate(value);
            var wrapperResults = wrapperValidator.Validate(value);

            Assert.IsTrue(rawResults.IsValid);
            Assert.IsTrue(wrapperResults.IsValid);
        }
        public void WrapperIgnoresNullValues()
        {
            var validator = new TestValidator();
            var wrapperValidator = new NullIgnoringValidatorWrapper(validator);
            object value = null;

            var rawResults = validator.Validate(value);
            var wrapperResults = wrapperValidator.Validate(value);

            Assert.IsFalse(rawResults.IsValid);
            Assert.AreEqual("Invalid value", rawResults.ElementAt(0).Message);
            Assert.IsTrue(wrapperResults.IsValid);
        }
        public void WrapperFailsIfNotNullValueFailsForWrappedValidator()
        {
            var validator = new TestValidator();
            var wrapperValidator = new NullIgnoringValidatorWrapper(validator);
            object value = "some value";

            var rawResults = validator.Validate(value);
            var wrapperResults = wrapperValidator.Validate(value);

            Assert.IsFalse(rawResults.IsValid);
            Assert.AreEqual("Invalid value", rawResults.ElementAt(0).Message);
            Assert.IsFalse(wrapperResults.IsValid);
            Assert.AreEqual("Invalid value", wrapperResults.ElementAt(0).Message);
        }
        /// <summary>
        /// Creates the composite validator built by the builder.
        /// </summary>
        protected virtual Validator DoGetValidator()
        {
            // create the appropriate validator
            Validator validator;

            if (this.valueValidators.Count == 1)
            {
                validator = this.valueValidators[0];
            }
            else
            {

                if (CompositionType.And == this.validatedElement.CompositionType)
                {
                    validator = new AndCompositeValidator(this.valueValidators.ToArray());
                }
                else
                {
                    validator = new OrCompositeValidator(this.valueValidators.ToArray());
                    validator.MessageTemplate = this.validatedElement.CompositionMessageTemplate;
                    validator.Tag = this.validatedElement.CompositionTag;
                }
            }

            // add support for ignoring nulls
            Validator valueValidator;
            if (this.validatedElement.IgnoreNulls)
            {
                valueValidator = new NullIgnoringValidatorWrapper(validator);
            }
            else
            {
                valueValidator = validator;
            }

            return valueValidator;
        }