Exemple #1
0
        public void DefaultErrorMessage_ExactLengthValidator()
        {
            var validator = new ExactLengthValidator(10);

            Assert.That(validator.ErrorMessageSource, Is.TypeOf(typeof(LocalizedStringSource)));
            Assert.That(validator.ErrorMessageSource.GetString(), Is.EqualTo(Messages.exact_length_error));
        }
 public void When_the_text_is_an_exact_length_the_validator_should_pass()
 {
     string text = "test";
     var validator = new ExactLengthValidator(4);
     var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => text));
     result.IsValid().ShouldBeTrue();
 }
 public void When_the_validator_fails_the_error_message_should_be_set()
 {
     var validator = new ExactLengthValidator(2);
     var result =
         validator.Validate(new PropertyValidatorContext("Forename", null, x => "Gire and gimble in the wabe"));
     result.Single().ErrorMessage.ShouldEqual("'Forename' must be 2 characters in length. You entered 27 characters.");
 }
 public void When_the_text_length_is_smaller_the_validator_should_fail()
 {
     string text = "test";
     var validator = new ExactLengthValidator(10);
     var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => text));
     result.IsValid().ShouldBeFalse();
 }
        public void Min_and_max_properties_should_be_set()
        {
            var validator = new ExactLengthValidator(5);

            validator.Min.ShouldEqual(5);
            validator.Max.ShouldEqual(5);
        }
        public void TestValidateNegative()
        {
            // arrange
            const string testString      = "MyString";
            var          lengthValidator = new ExactLengthValidator(testString.Length + 1);

            Assert.ThrowsException <ExactLengthException>(() => StringValidator.TestValidateNegative(lengthValidator, testString));
        }
        public void TestValidatePositive()
        {
            // arrange
            const string testString      = "MyString";
            var          lengthValidator = new ExactLengthValidator(testString.Length);

            StringValidator.TestValidatePositive(lengthValidator, testString);
        }
        public void Given_CorrectValidator_When_Verifying_Then_ValidationPass()
        {
            // Arrange
            var exactValidatorVerifier = new ExactLengthValidator(10);
            var verifier = new ExactLengthValidatorVerifier(10);

            // Act & Assert
            AssertExtension.NotThrows(() => verifier.Verify(exactValidatorVerifier));
        }
        public void Given_CorrectValidatorWithDifferentValue_When_Verifying_Then_ValidationFail()
        {
            // Arrange
            var exactValidatorVerifier = new ExactLengthValidator(10);
            var verifier = new ExactLengthValidatorVerifier(1);

            // Act & Assert
            AssertExtension.Throws <XunitException>(() => verifier.Verify(exactValidatorVerifier),
                                                    "(Min property)");
        }
        public void Should_create_exactlengthadapter_for_exactlengthvalidator()
        {
            // Given
            var validator = new ExactLengthValidator(10);

            // When
            var result = factory.Create(this.rule, validator);

            // Then
            result.ShouldBeOfType<ExactLengthAdapater>();
        }
        private ValidationMessage ExactLength(
            string value,
            int length,
            ValidationMessageType validationMessageType = ValidationMessageType.Error)
        {
            var betweenLengthValidator = new ExactLengthValidator <TestValidatableObject>(_ => length, validationMessageType);
            var context           = new ValidationContext <TestValidatableObject, string>(null, nameof(TestValidatableObject.Number), null, value);
            var validationMessage = betweenLengthValidator.ValidateProperty(context).FirstOrDefault();

            return(validationMessage);
        }
        public void TestContructor()
        {
            // arrange
            const int expected = 5;

            // act
            var lengthValidator = new ExactLengthValidator(expected);

            // assert
            Assert.AreEqual(lengthValidator.Length, expected);
        }
        public void Should_create_exactlengthadapter_for_exactlengthvalidator()
        {
            // Given
            var validator = new ExactLengthValidator(10);

            // When
            var result = factory.Create(this.rule, validator);

            // Then
            result.ShouldBeOfType <ExactLengthAdapater>();
        }
        public void Format_ILengthValidators()
        {
            var validator1 = new LengthValidator(5, 10);
            var validator2 = new MaximumLengthValidator(12);
            var validator3 = new MinimumLengthValidator(2);
            var validator4 = new ExactLengthValidator(4);

            Assert.That(_formatter.Format(validator1, _typeNameFormatter), Is.EqualTo("LengthValidator { MinLength = '5', MaxLength = '10' }"));
            Assert.That(_formatter.Format(validator2, _typeNameFormatter), Is.EqualTo("MaximumLengthValidator { MinLength = '0', MaxLength = '12' }"));
            Assert.That(_formatter.Format(validator3, _typeNameFormatter), Is.EqualTo("MinimumLengthValidator { MinLength = '2', MaxLength = '-1' }"));
            Assert.That(_formatter.Format(validator4, _typeNameFormatter), Is.EqualTo("ExactLengthValidator { MinLength = '4', MaxLength = '4' }"));
        }
Exemple #15
0
        public void Given_Composer_When_ExactLengthValidatorVerifierGeneric_Then_CorrectRuleSet()
        {
            // Arrange
            var composer             = BaseVerifiersSetComposer.Build();
            var exactLengthValidator = new ExactLengthValidator(10);

            // Act
            var rules = composer.AddExactLengthValidatorVerifier(10).Create();

            // Assert
            Assert.Equal(new[] { typeof(ExactLengthValidatorVerifier) }, rules.Select(x => x.GetType()).ToArray());
            AssertExtension.NotThrows(() => rules[0].Verify(exactLengthValidator));
        }
Exemple #16
0
        private void AddAttributeValidation()
        {
            var type = typeof(TModel);

            var ignoreAttrsAttr = type.GetTypeInfo().GetCustomAttributes(typeof(IgnoreValidationAttributesAttribute), true)
                                  .FirstOrDefault() as IgnoreValidationAttributesAttribute;
            var ignoreProps = ignoreAttrsAttr != null
                ? new HashSet <string>(ignoreAttrsAttr.Properties ?? new string[0])
                : new HashSet <string>();

            foreach (var prop in type.GetProperties().Where(o => !ignoreProps.Contains(o.Name)))
            {
                var attrs = prop.GetCustomAttributes(true);
                //Add validation from attributes
                foreach (var attr in attrs.OfType <ValidationAttribute>())
                {
                    IPropertyValidator propValidator = null;
                    #region NotNullAttribute

                    if (attr is NotNullAttribute)
                    {
                        propValidator = new NotNullValidator();
                    }

                    #endregion
                    #region EmailAttribute

                    if (attr is EmailAttribute)
                    {
                        propValidator = new EmailValidator();
                    }

                    #endregion
                    #region EnumAttribute

                    var enumAttr = attr as EnumAttribute;
                    if (enumAttr != null)
                    {
                        propValidator = new EnumValidator(enumAttr.Type);
                    }

                    #endregion
                    #region NotEmptyAttribute

                    var notEmptyAttr = attr as NotEmptyAttribute;
                    if (notEmptyAttr != null)
                    {
                        propValidator = new NotEmptyValidator(notEmptyAttr.DefaultValue ?? prop.PropertyType.GetDefaultValue());
                    }

                    #endregion
                    #region NotNullOrEmptyAttribute

                    var notNullOrEmptyAttr = attr as NotNullOrEmptyAttribute;
                    if (notNullOrEmptyAttr != null)
                    {
                        propValidator = new NotEmptyValidator(prop.PropertyType.GetDefaultValue());
                        AddAttributePropertyValidator(new NotNullValidator(), prop, attr.IncludePropertyName);
                    }

                    #endregion
                    #region EqualAttribute

                    AddComparisonValidator(attr as EqualAttribute, type, prop,
                                           o => new EqualValidator(o),
                                           (func, info) => new EqualValidator(func, info));

                    #endregion
                    #region LengthAttribute

                    var lengthAttr = attr as LengthAttribute;
                    if (lengthAttr != null)
                    {
                        propValidator = new LengthValidator(lengthAttr.Min, lengthAttr.Max);
                    }

                    #endregion
                    #region NotEqualAttribute

                    AddComparisonValidator(attr as NotEqualAttribute, type, prop,
                                           o => new NotEqualValidator(o),
                                           (func, info) => new NotEqualValidator(func, info));

                    #endregion
                    #region RegularExpressionAttribute

                    var regexAttr = attr as RegularExpressionAttribute;
                    if (regexAttr != null)
                    {
                        propValidator = new RegularExpressionValidator(regexAttr.Expression, regexAttr.RegexOptions);
                    }

                    #endregion
                    #region CreditCardAttribute

                    if (attr is CreditCardAttribute)
                    {
                        propValidator = new CreditCardValidator();
                    }

                    #endregion
                    #region ScalePrecisionAttribute

                    var scalePrecisionAttr = attr as ScalePrecisionAttribute;
                    if (scalePrecisionAttr != null)
                    {
                        propValidator = new ScalePrecisionValidator(scalePrecisionAttr.Scale, scalePrecisionAttr.Precision);
                    }

                    #endregion
                    #region ExactLengthAttribute

                    var exctLenAttr = attr as ExactLengthAttribute;
                    if (exctLenAttr != null)
                    {
                        propValidator = new ExactLengthValidator(exctLenAttr.Length);
                    }

                    #endregion
                    #region ExclusiveBetweenAttribute

                    var exclusiveBetweenAttribute = attr as ExclusiveBetweenAttribute;
                    if (exclusiveBetweenAttribute != null)
                    {
                        if (exclusiveBetweenAttribute.From != null && exclusiveBetweenAttribute.To != null &&
                            exclusiveBetweenAttribute.From.GetType() != exclusiveBetweenAttribute.To.GetType())
                        {
                            var fromConverted = Convert.ChangeType(exclusiveBetweenAttribute.From, exclusiveBetweenAttribute.To.GetType());

                            propValidator = new ExclusiveBetweenValidator(fromConverted as IComparable, exclusiveBetweenAttribute.To as IComparable);
                        }
                        else
                        {
                            propValidator = new ExclusiveBetweenValidator(exclusiveBetweenAttribute.From as IComparable, exclusiveBetweenAttribute.To as IComparable);
                        }
                    }

                    #endregion
                    #region ExclusiveBetweenAttribute

                    var inclusiveBetweenAttribute = attr as InclusiveBetweenAttribute;
                    if (inclusiveBetweenAttribute != null)
                    {
                        if (inclusiveBetweenAttribute.From != null && inclusiveBetweenAttribute.To != null &&
                            inclusiveBetweenAttribute.From.GetType() != inclusiveBetweenAttribute.To.GetType())
                        {
                            var fromConverted = Convert.ChangeType(inclusiveBetweenAttribute.From, inclusiveBetweenAttribute.To.GetType());

                            propValidator = new InclusiveBetweenValidator(fromConverted as IComparable, inclusiveBetweenAttribute.To as IComparable);
                        }
                        else
                        {
                            propValidator = new InclusiveBetweenValidator(inclusiveBetweenAttribute.From as IComparable, inclusiveBetweenAttribute.To as IComparable);
                        }
                    }

                    #endregion
                    #region GreaterThanAttribute

                    AddComparisonValidator(attr as GreaterThanAttribute, type, prop,
                                           o => new GreaterThanValidator(o as IComparable),
                                           (func, info) => new GreaterThanValidator(func, info));

                    #endregion
                    #region GreaterThanOrEqualAttribute

                    AddComparisonValidator(attr as GreaterThanOrEqualAttribute, type, prop,
                                           o => new GreaterThanOrEqualValidator(o as IComparable),
                                           (func, info) => new GreaterThanOrEqualValidator(func, info));

                    #endregion
                    #region LessThanOrEqualAttribute

                    AddComparisonValidator(attr as LessThanOrEqualAttribute, type, prop,
                                           o => new LessThanOrEqualValidator(o as IComparable),
                                           (func, info) => new LessThanOrEqualValidator(func, info));

                    #endregion
                    #region LessThanAttribute

                    AddComparisonValidator(attr as LessThanAttribute, type, prop,
                                           o => new LessThanValidator(o as IComparable),
                                           (func, info) => new LessThanValidator(func, info));

                    #endregion

                    if (propValidator == null)
                    {
                        continue;
                    }

                    AddAttributePropertyValidator(propValidator, prop, attr.IncludePropertyName);
                }
            }
        }
		public void Min_and_max_properties_should_be_set() {
			var validator = new ExactLengthValidator(5);
			validator.Min.ShouldEqual(5);
			validator.Max.ShouldEqual(5);
		}