Пример #1
0
        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);
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
        }
Пример #4
0
        /// <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);
        }