protected virtual Validator DoGetValidator()
        {
            Validator validator;

            if (this.valueValidators.Count == 1)
            {
                validator = this.valueValidators[0];
            }
            else if (this.validatedElement.CompositionType == CompositionType.And)
            {
                validator = new AndCompositeValidator(this.valueValidators.ToArray());
            }
            else
            {
                validator = new OrCompositeValidator(this.valueValidators.ToArray())
                {
                    MessageTemplate = this.validatedElement.CompositionMessageTemplate,
                    Tag             = this.validatedElement.CompositionTag
                };
            }
            if (this.validatedElement.IgnoreNulls)
            {
                return(new OrCompositeValidator(new Validator[] { new NotNullValidator(true), validator })
                {
                    MessageTemplate = (this.validatedElement.IgnoreNullsMessageTemplate != null) ? this.validatedElement.IgnoreNullsMessageTemplate : Resources.IgnoreNullsDefaultMessageTemplate, Tag = this.validatedElement.IgnoreNullsTag
                });
            }
            return(validator);
        }
        public void ValidationRequestWithNullSuppliedValidationResultsThrows()
        {
            Validator validator = new OrCompositeValidator();
            string    target    = new string('a', 10); // just any string, but a new one

            validator.Validate(target, null);
        }
        public void ReturnsCompositeFailureAfterQueryingAllValidatorsIfAllValidatorsReturnFailure()
        {
            MockValidator <string> subValidator1 = new MockValidator <string>(true, "validator1");
            MockValidator <string> subValidator2 = new MockValidator <string>(true, "validator2");
            MockValidator <string> subValidator3 = new MockValidator <string>(true, "validator3");
            Validator validator = new OrCompositeValidator(subValidator1, subValidator2, subValidator3);

            validator.MessageTemplate = "composite validator";
            string target = new string('a', 10); // just any string, but a new one

            ValidationResults validationResults = validator.Validate(target);

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.AreEqual("composite validator", resultsList[0].Message);
            Assert.AreEqual(null, resultsList[0].Tag);
            IList <ValidationResult> nestedResultsList = ValidationTestHelper.GetResultsList(resultsList[0].NestedValidationResults);

            Assert.AreEqual(3, nestedResultsList.Count);
            Assert.AreEqual("validator1", nestedResultsList[0].Message);
            Assert.AreEqual("validator2", nestedResultsList[1].Message);
            Assert.AreEqual("validator3", nestedResultsList[2].Message);
            Assert.AreSame(target, subValidator1.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator2.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator3.ValidatedTargets[0]);
        }
        public void CanPerformValidationWithSuppliedValidationResults()
        {
            MockValidator <string> subValidator1 = new MockValidator <string>(true, "validator1");
            MockValidator <string> subValidator2 = new MockValidator <string>(true, "validator2");
            MockValidator <string> subValidator3 = new MockValidator <string>(true, "validator3");
            Validator validator = new OrCompositeValidator(subValidator1, subValidator2, subValidator3);

            validator.Tag = "tag";
            string            target            = new string('a', 10); // just any string, but a new one
            ValidationResults validationResults = new ValidationResults();

            validator.Validate(target, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.IsTrue(TemplateStringTester.IsMatch(Resources.OrCompositeValidatorDefaultMessageTemplate, resultsList[0].Message));
            Assert.AreEqual("tag", resultsList[0].Tag);
            IList <ValidationResult> nestedResultsList = ValidationTestHelper.GetResultsList(resultsList[0].NestedValidationResults);

            Assert.AreEqual(3, nestedResultsList.Count);
            Assert.AreEqual("validator1", nestedResultsList[0].Message);
            Assert.AreEqual("validator2", nestedResultsList[1].Message);
            Assert.AreEqual("validator3", nestedResultsList[2].Message);
            Assert.AreSame(target, subValidator1.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator2.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator3.ValidatedTargets[0]);
        }
Beispiel #5
0
        /// <summary>
        /// Creates the composite validator built by the builder.
        /// </summary>
        protected virtual Validator DoGetValidator()
        {
            // create appropriate composite validator
            Validator compositeValidator;

            if (CompositionType.And == this.validatedElement.CompositionType)
            {
                compositeValidator = new AndCompositeValidator(this.valueValidators.ToArray());
            }
            else
            {
                compositeValidator = new OrCompositeValidator(this.valueValidators.ToArray());
                compositeValidator.MessageTemplate = this.validatedElement.CompositionMessageTemplate;
                compositeValidator.Tag             = this.validatedElement.CompositionTag;
            }
            // add support for ignoring nulls
            Validator valueValidator;

            if (this.validatedElement.IgnoreNulls)
            {
                valueValidator = new OrCompositeValidator(new NotNullValidator(true), compositeValidator);
                valueValidator.MessageTemplate = this.validatedElement.IgnoreNullsMessageTemplate != null
                                        ? this.validatedElement.IgnoreNullsMessageTemplate
                                        : Resources.IgnoreNullsDefaultMessageTemplate;
                valueValidator.Tag = this.validatedElement.IgnoreNullsTag;
            }
            else
            {
                valueValidator = compositeValidator;
            }

            return(valueValidator);
        }
Beispiel #6
0
        static void CreatingAndUsingValidatorsDirectly()
        {
            // Create a Contains Characters Validator and use it to validate a String value.
            Validator charsValidator = new ContainsCharactersValidator("cat", ContainsCharacters.All,
                                                                       "Value must contain {4} of the characters '{3}'.");

            Console.WriteLine("Validating a string value using a Contains Characters Validator...");
            charsValidator.Tag = "Validating the String value 'disconnected'";
            // This overload of the Validate method returns a new ValidationResults
            // instance populated with any/all of the validation errors.
            ValidationResults valResults = charsValidator.Validate("disconnected");
            // Create a Domain Validator and use it to validate an Integer value.
            Validator integerValidator = new DomainValidator <int>("Value must be in the list 1, 3, 7, 11, 13.",
                                                                   new int[] { 1, 3, 7, 11, 13 });

            integerValidator.Tag = "Validating the Integer value '42'";
            Console.WriteLine("Validating an integer value using a Domain Validator...");
            // This overload of the Validate method takes an existing ValidationResults
            // instance and adds any/all of the validation errors to it.
            integerValidator.Validate(42, valResults);
            // Create an Or Composite Validator containing two validators.
            // Note that the NotNullValidator is negated to allow NULL values.
            Validator[] valArray = new Validator[] {
                new NotNullValidator(true, "Value can be NULL."),
                new StringLengthValidator(5, RangeBoundaryType.Inclusive, 5, RangeBoundaryType.Inclusive,
                                          "Value must be between {3} ({4}) and {5} ({6}) chars.")
            };
            Validator orValidator = new OrCompositeValidator("Value can be NULL or a string of 5 characters.", valArray);

            // Validate two strings using the Or Composite Validator.
            Console.WriteLine("Validating a NULL value using an Or Composite Validator...");
            orValidator.Validate(null, valResults);  // this will not cause a validation error
            Console.WriteLine("Validating a string value using an Or Composite Validator...");
            orValidator.Validate("MoreThan5Chars", valResults);
            // Validate a single property of an existing class instance.
            // First create a Product instance with an invalid ID value.
            IProduct productWithID = new Product();

            PopulateInvalidProduct(productWithID);
            // Create a Property Value Validator that will use a RegexValidator
            // to validate the property value.
            Validator propValidator = new PropertyValueValidator <Product>("ID",
                                                                           new RegexValidator("[A-Z]{2}[0-9]{4}", "Product ID must be 2 capital letters and 4 numbers."));

            Console.WriteLine("Validating one property of an object using a Property Value Validator...");
            propValidator.Validate(productWithID, valResults);
            // Now display the results of all the previous validation operations.
            ShowValidationResults(valResults);
        }
        public void ReturnsSuccessWhenValidatingNullAfterQueryingPreviousValidatorsIfManyValidatorsReturnFailure()
        {
            MockValidator <string> subValidator1 = new MockValidator <string>(true, "validator1");
            MockValidator <string> subValidator2 = new MockValidator <string>(true, "validator2");
            MockValidator <string> subValidator3 = new MockValidator <string>(false, "validator3");
            Validator validator = new OrCompositeValidator(subValidator1, subValidator2, subValidator3);
            string    target    = null;

            ValidationResults validationResults = validator.Validate(target);

            Assert.IsTrue(validationResults.IsValid);
            Assert.AreSame(target, subValidator1.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator2.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator3.ValidatedTargets[0]);
        }
        public void ReturnsSuccessAfterQueryingPreviousValidatorsIfOneValidatorReturnFailure()
        {
            MockValidator <string> subValidator1 = new MockValidator <string>(true, "validator1");
            MockValidator <string> subValidator2 = new MockValidator <string>(false, "validator2");
            MockValidator <string> subValidator3 = new MockValidator <string>(false, "validator3");
            Validator validator = new OrCompositeValidator(subValidator1, subValidator2, subValidator3);
            string    target    = new string('a', 10); // just any string, but a new one

            ValidationResults validationResults = validator.Validate(target);

            Assert.IsTrue(validationResults.IsValid);
            Assert.AreSame(target, subValidator1.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator2.ValidatedTargets[0]);
            Assert.AreEqual(0, subValidator3.ValidatedTargets.Count);
        }
        private static bool CompositeHasValidators(Validator validator)
        {
            AndCompositeValidator andValidator = validator as AndCompositeValidator;

            if (andValidator != null)
            {
                return(((Validator[])andValidator.Validators).Length > 0);
            }

            OrCompositeValidator orValidator = validator as OrCompositeValidator;

            if (orValidator != null)
            {
                return(((Validator[])orValidator.Validators).Length > 0);
            }

            return(false);
        }
        public Validator GetValidator()
        {
            Validator valiator = null;

            if (ListValidator.Count == 1)
            {
                valiator = ListValidator[0];
            }
            else
            if (CompositType == CompositaionType.AND)
            {
                valiator = new AndCompositeValidator(ListValidator.ToArray());
            }
            else
            if (CompositType == CompositaionType.OR)
            {
                valiator = new OrCompositeValidator(ListValidator.ToArray());
            }
            return(valiator);
        }
Beispiel #11
0
        public void CanPerformValidationWithSuppliedValidationResults()
        {
            MockValidator <string> subValidator1 = new MockValidator <string>(true, "validator1");
            MockValidator <string> subValidator2 = new MockValidator <string>(true, "validator2");
            MockValidator <string> subValidator3 = new MockValidator <string>(true, "validator3");
            Validator <string>     validator     = new OrCompositeValidator <string>(subValidator1, subValidator2, subValidator3);
            string            target             = new string('a', 10);// just any string, but a new one
            ValidationResults validationResults  = new ValidationResults();

            validator.Validate(target, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(3, resultsList.Count);
            Assert.AreEqual("validator1", resultsList[0].Message);
            Assert.AreEqual("validator2", resultsList[1].Message);
            Assert.AreEqual("validator3", resultsList[2].Message);
            Assert.AreSame(target, subValidator1.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator2.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator3.ValidatedTargets[0]);
        }
Beispiel #12
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);
        }