public void ValidationRequestWithNullSuppliedValidationResultsThrows()
        {
            Validator validator = new AndCompositeValidator();
            string    target    = new string('a', 10); // just any string, but a new one

            validator.Validate(target, null);
        }
Пример #2
0
        public void AndCompositeValidator()
        {
            var v = new AndCompositeValidator(PropertyName, new List <Validator> {
                new RequiredValidator(), new LengthValidator(5)
            });

            Assert.AreEqual(v.PropertyName, PropertyName);

            var o = new SimpleObject {
                SimpleProperty = "12345"
            };

            Assert.IsTrue(v.Validate(o));

            o.SimpleProperty = null;
            Assert.IsFalse(v.Validate(o));
            o.SimpleProperty = "123";
            Assert.IsFalse(v.Validate(o));
            o.SimpleProperty = "123456";
            Assert.IsFalse(v.Validate(o));
        }
        public void ReturnsSuccessIfAllValidatorsReturnSuccess()
        {
            MockValidator <string> subValidator1 = new MockValidator <string>(false, "validator1");
            MockValidator <string> subValidator2 = new MockValidator <string>(false, "validator2");
            MockValidator <string> subValidator3 = new MockValidator <string>(false, "validator3");
            Validator validator = new AndCompositeValidator(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.AreSame(target, subValidator3.ValidatedTargets[0]);
        }
Пример #4
0
        public void TestMethod1()
        {
            Customer myCustomer = new Customer()
            {
                firstName = " A Name tat is too long for the validation"
            };

            Validator shortStringValidator = new AndCompositeValidator(new NotNullValidator(), new StringLengthValidator(1, 50));

            ValidationResults r = shortStringValidator.Validate(myCustomer.firstName);

            //ValidationResults r = Validation.Validate<Customer>(myCustomer);
            if (!r.IsValid)
            {
                throw new InvalidOperationException("Validation error found.");
            }
        }
Пример #5
0
        public IValidationResult Validate()
        {
            if (!IsOverriden)
            {
                //MS Validation
                Validator NotNullStringLengthValidator = new AndCompositeValidator(this.msNotNullValidator, this.msStringLengthValidator);

                ValidationResults valResults = NotNullStringLengthValidator.Validate(Customer.customerNumber);

                if (!valResults.IsValid) //if not valid
                {
                    RuleValidationResult = new MyValidationResult();
                    RuleValidationResult.ValidationMessageList.Add(new NullValidationMessage());
                }


                if (Customer.EmailList != null)
                {
                    //Some other Random Test Validation. RegexValidator in this case
                    Validator <string> emailAddresssValidator = new RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                    valResults = emailAddresssValidator.Validate(Customer.EmailList.First());
                }
                else
                {
                    valResults = this.msNotNullValidator.Validate(Customer.EmailList);
                }

                //Holidays Own Validation

                //RuleValidationResult = valResults;

                if (valResults != null)
                {
                    RuleValidationResult = new MyValidationResult();

                    foreach (var varRes in valResults)
                    {
                        IValidationMessage val = new MSWrapperMessage(MessageTypes.Error, varRes.Message);
                        RuleValidationResult.ValidationMessageList.Add(val);
                    }
                }
            }

            return(RuleValidationResult);
        }
        public void ReturnsFailureAfterQueryingAllValidatorsIfOneValidatorReturnFailure()
        {
            MockValidator <string> subValidator1 = new MockValidator <string>(false, "validator1");
            MockValidator <string> subValidator2 = new MockValidator <string>(true, "validator2");
            MockValidator <string> subValidator3 = new MockValidator <string>(false, "validator3");
            Validator validator = new AndCompositeValidator(subValidator1, subValidator2, subValidator3);
            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("validator2", resultsList[0].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>(false, "validator1");
            MockValidator <string> subValidator2 = new MockValidator <string>(true, "validator2");
            MockValidator <string> subValidator3 = new MockValidator <string>(false, "validator3");
            Validator         validator          = new AndCompositeValidator(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(1, resultsList.Count);
            Assert.AreEqual("validator2", resultsList[0].Message);
            Assert.AreSame(target, subValidator1.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator2.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator3.ValidatedTargets[0]);
        }
        public void ReturnsCompositeFailureWhenValidatingNullAfterQueryingAllValidatorsIfManyValidatorsReturnFailure()
        {
            MockValidator <string> subValidator1 = new MockValidator <string>(false, "validator1");
            MockValidator <string> subValidator2 = new MockValidator <string>(true, "validator2");
            MockValidator <string> subValidator3 = new MockValidator <string>(true, "validator3");
            Validator validator = new AndCompositeValidator(subValidator1, subValidator2, subValidator3);
            string    target    = null;

            ValidationResults validationResults = validator.Validate(target);

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

            Assert.AreEqual(2, resultsList.Count);
            Assert.AreEqual("validator2", resultsList[0].Message);
            Assert.AreEqual("validator3", resultsList[1].Message);
            Assert.AreSame(target, subValidator1.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator2.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator3.ValidatedTargets[0]);
        }