Example #1
0
        public void ValidatorShouldUsePropertyMappings()
        {
            ValidatorBase validator  = GetValidator();
            var           dictionary = new Dictionary <string, ICollection <string> >
            {
                {
                    PropertyToValidate,
                    new List <string> {
                        PropertyToMap
                    }
                }
            };

            validator.Initialize(new ValidatorContext(new object(), dictionary, null, null, GetServiceProvider()));
            validator.IsValid.ShouldBeTrue();
            validator.UpdateErrors(PropertyToValidate, ValidatorErrors, false);
            validator.IsValid.ShouldBeFalse();
            validator.GetErrors(PropertyToValidate).ShouldBeEmpty();

            string[] errors = validator.GetErrors(PropertyToMap).OfType <string>().ToArray();
            errors.Length.ShouldEqual(1);
            errors.Contains(ValidatorError).ShouldBeTrue();

            validator.UpdateErrors(PropertyToMap, null, false);
            validator.GetErrors(PropertyToMap).ShouldBeEmpty();
            validator.IsValid.ShouldBeTrue();
        }
Example #2
0
        public void ValidatorShouldUpdateErrors()
        {
            ValidatorBase validator = GetValidator();

            validator.Initialize(new ValidatorContext(new object(), GetServiceProvider()));
            validator.UpdateErrors(PropertyToValidate, ValidatorErrors, false);
            validator.UpdateErrors(PropertyToMap, ValidatorErrors, false);

            validator.GetErrors(PropertyToValidate).OfType <object>().Single().ShouldEqual(ValidatorError);
            validator.GetErrors(PropertyToMap).OfType <object>().Single().ShouldEqual(ValidatorError);

            validator.ClearErrors();
            validator.GetErrors(PropertyToValidate).OfType <object>().ShouldBeEmpty();
            validator.GetErrors(PropertyToMap).OfType <object>().ShouldBeEmpty();
            validator.IsValid.ShouldBeTrue();
        }
Example #3
0
        public void GetErrorsShouldReturnValidatorErrors()
        {
            ValidatorBase validator = GetValidator();

            validator.Initialize(new ValidatorContext(new object(), GetServiceProvider()));
            validator.GetErrors(PropertyToValidate).ShouldBeEmpty();

            validator.UpdateErrors(PropertyToValidate, ValidatorErrors, false);
            validator.GetErrors(PropertyToValidate).OfType <object>().Single().ShouldEqual(ValidatorError);

            validator.UpdateErrors(PropertyToValidate, new object[] { ValidatorError, ValidatorError }, false);
            var errors = validator.GetErrors(PropertyToValidate).OfType <object>().ToArray();

            errors.Length.ShouldEqual(2);
            errors.All(o => Equals(o, ValidatorError)).ShouldBeTrue();
        }
Example #4
0
        public void ValidatorShouldUseIgnorePropertiesCollection()
        {
            ValidatorBase validator = GetValidator();

            validator.Initialize(new ValidatorContext(new object(), null, new List <string> {
                PropertyToValidate
            }, null,
                                                      GetServiceProvider()));
            validator.IsValid.ShouldBeTrue();
            validator.UpdateErrors(PropertyToValidate, ValidatorErrors, false);
            validator.IsValid.ShouldBeTrue();
            validator.GetErrors(PropertyToValidate).ShouldBeEmpty();
        }
Example #5
0
        public void GetAllErrorsShouldReturnAllValidatorErrors()
        {
            ValidatorBase validator = GetValidator();

            validator.Initialize(new ValidatorContext(new object(), GetServiceProvider()));

            validator.UpdateErrors(PropertyToValidate, ValidatorErrors, false);
            validator.UpdateErrors(PropertyToMap, ValidatorErrors, false);
            var allErrors = validator.GetErrors();

            allErrors.Count.ShouldEqual(2);
            allErrors[PropertyToValidate].Single().ShouldEqual(ValidatorError);
            allErrors[PropertyToMap].Single().ShouldEqual(ValidatorError);
        }
Example #6
0
        public virtual void ValidatorShouldClearOldErrorsValidateAll()
        {
            ValidatorBase validator = GetValidator();

            validator.Initialize(new ValidatorContext(new object(), GetServiceProvider()));
            validator.UpdateErrors(PropertyToValidate, ValidatorErrors, false);

            validator.ValidateAsync().Wait();
            if (validator is ManualValidator)
            {
                var allErrors = validator.GetErrors();
                allErrors.Count.ShouldEqual(1);
                allErrors[PropertyToValidate].Single().ShouldEqual(ValidatorErrors[0]);
            }
            else
            {
                validator.IsValid.ShouldBeTrue();
            }
        }
Example #7
0
        public void NotInitializedValidatorShouldThrowErrorTest()
        {
            ValidatorBase validator = GetValidator();

            ShouldThrow <InvalidOperationException>(() => validator.GetErrors(PropertyToValidate));
        }