コード例 #1
0
        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]);
        }
コード例 #2
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);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: vvalotto/PlataformaNET
 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);
 }
コード例 #4
0
        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]);
        }
コード例 #5
0
        public void ValidationRequestWithNullSuppliedValidationResultsThrows()
        {
            Validator validator = new OrCompositeValidator();
            string target = new string('a', 10); // just any string, but a new one

            validator.Validate(target, null);
        }
コード例 #6
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]);
        }