Exemple #1
0
        public void ValidateMultipleRegexFields()
        {
            var obj = new ModelWithMultipleValidationAttributes()
            {
                RegexField = 4567,
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsTrue(validation.HasError);

            obj = new ModelWithMultipleValidationAttributes()
            {
                RegexField = 456,
            };

            validation = ModelValidator.ValidateEntity(obj);
            Assert.IsFalse(validation.HasError);

            obj = new ModelWithMultipleValidationAttributes()
            {
                RegexField = -23,
            };

            validation = ModelValidator.ValidateEntity(obj);
            Assert.IsTrue(validation.HasError);
        }
Exemple #2
0
        public void ValidateRegexWithAllowNullSetToFalse()
        {
            var obj = new ModelWithOneNullableRegexIntField()
            {
                RegexField = null,
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsTrue(validation.HasError);

            obj = new ModelWithOneNullableRegexIntField()
            {
                RegexField = 2,
            };

            validation = ModelValidator.ValidateEntity(obj);
            Assert.IsFalse(validation.HasError);

            obj = new ModelWithOneNullableRegexIntField()
            {
                RegexField = -2
            };

            validation = ModelValidator.ValidateEntity(obj);
            Assert.IsTrue(validation.HasError);
        }
Exemple #3
0
        public void ValidateEmptyCollectionAllowedField_ShouldPassIfEmptyCollectIsProvided()
        {
            var obj = new ModelWithRequiredCollection();

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsFalse(validation.HasError);
        }
Exemple #4
0
        public void ValidateEmptyCollectionAllowedField_ShouldNotPassIfNullCollectIsProvided()
        {
            var obj = new ModelWithRequiredCollection();

            obj.List = null;
            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsTrue(validation.HasError);
        }
Exemple #5
0
        public void ShouldThrowBecauseOfPropertyValueNotBeingSameTypeAsValue()
        {
            var conditional = new ModelWithIncorrectEnumDependentPropertyType()
            {
                TestEnum   = TestEnum.One,
                RegexField = 2
            };

            Assert.ThrowsException <ValidationException>(() => ModelValidator.ValidateEntity(conditional));
        }
Exemple #6
0
        public void ShouldThrowBecauseOfPropertyValueNotBeingABool()
        {
            var invalid = new ModelWithIncorrectDependentPropertyType
            {
                Enabled    = 2,
                RegexField = 20
            };

            Assert.ThrowsException <ValidationException>(() => ModelValidator.ValidateEntity(invalid));
        }
Exemple #7
0
        public void ShouldThrowBecauseOfNonExistingPropertyValue()
        {
            var invalid = new ModelWithIncorrectPropertyValue
            {
                Enabled    = true,
                RegexField = 20
            };

            Assert.ThrowsException <ValidationException>(() => ModelValidator.ValidateEntity(invalid));
        }
Exemple #8
0
        public void ValidatePositiveIntegerWithNegativeValue()
        {
            var obj = new ModelWithOneRegexIntField
            {
                RegexField = -20
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsTrue(validation.HasError);
        }
Exemple #9
0
        public void ValidatePositiveIntegerWithNullValue()
        {
            var obj = new ModelWithOneNullableRegexIntField()
            {
                RegexField = null
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsTrue(validation.HasError);
        }
Exemple #10
0
        public void ValidatePositiveNumberWithNineValues()
        {
            var obj = new ModelWithPositiveNumberWithNoMoreThanNineValues()
            {
                RegexField = 123456789
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsFalse(validation.HasError);
        }
Exemple #11
0
        public void ValidatePositiveNumberWithMoreThanNineValues()
        {
            var obj = new ModelWithPositiveNumberWithNoMoreThanNineValues()
            {
                RegexField = int.MaxValue
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsTrue(validation.HasError);
        }
Exemple #12
0
        public void ValidateDefaultValueNotRequiredField_ShouldPassIfDefaultValueIsProvided()
        {
            var obj = new ModelWithRequiredDefaultValueAllowed()
            {
                Value = 0
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsFalse(validation.HasError);
        }
Exemple #13
0
        public void ValidateNullRequiredField()
        {
            var obj = new ModelWithOneRequiredStringField
            {
                RequiredField = null
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsTrue(validation.HasError);
        }
Exemple #14
0
        public void ValidatePopulatedRequiredField()
        {
            var obj = new ModelWithOneRequiredStringField
            {
                RequiredField = "required"
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsFalse(validation.HasError);
        }
Exemple #15
0
        public void ShouldPassValidationIfConditionalEnumConditionsIsMetAndValueIsCorrect()
        {
            var conditional = new ModelWithConditionalEnumValue
            {
                TestEnum   = TestEnum.One,
                RegexField = 2
            };

            var result = ModelValidator.ValidateEntity(conditional);

            Assert.IsFalse(result.HasError);
        }
        public void ShouldPassValidationIfValueIsNotEmptyString()
        {
            var obj = new ModelWithConditionalRequiredNotEmptyString
            {
                Enabled = true,
                Value   = "   d    ",
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsFalse(validation.HasError);
        }
Exemple #17
0
        public void HandlesInheritedAttributes()
        {
            var obj        = new ModelWithInheritedAttributes();
            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsTrue(validation.HasError);

            obj.Guid   = Guid.NewGuid();
            validation = ModelValidator.ValidateEntity(obj);

            Assert.IsFalse(validation.HasError);
        }
        public void ShouldPassValidationIfConditionIsMetAndValueIsNotNull()
        {
            var obj = new ModelWithConditionalRequiredGuid()
            {
                Enabled       = true,
                RequiredField = Guid.Empty,
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsFalse(validation.HasError);
        }
        public void ShouldPassValidationIfConditionIsMetAndValueIsNotDefault()
        {
            var obj = new ModelWithConditionalRequiredNotDefaultGuid()
            {
                Enabled = true,
                Value   = Guid.NewGuid()
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsFalse(validation.HasError);
        }
        public void ShouldNotValidateIfConditionNoMet()
        {
            var obj = new ModelWithConditionalRequiredGuid()
            {
                Enabled       = false,
                RequiredField = null,
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsFalse(validation.HasError);
        }
Exemple #21
0
        public void ValidateEmptyCollectionNotAllowedField_ShouldNotPassIfEmptyCollectIsProvided()
        {
            var obj = new ModelWithRequiredNonEmptyCollection();

            obj.Array       = Enumerable.Empty <string>().ToArray();
            obj.List        = obj.Array.ToList();
            obj.Collection  = obj.Array.ToHashSet();
            obj.IEnumerable = obj.Array.ToDictionary(x => x);

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsTrue(validation.HasError);
            Assert.AreEqual(4, validation.Errors.Count);
        }
Exemple #22
0
        public void ValidateRegexWithAllowNullSetToTrue()
        {
            var obj = new ModelWithRegExAndAllowNull()
            {
                RegexField = null,
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsFalse(validation.HasError);

            obj = new ModelWithRegExAndAllowNull()
            {
                RegexField = int.MaxValue,
            };

            validation = ModelValidator.ValidateEntity(obj);
            Assert.IsTrue(validation.HasError);
        }
Exemple #23
0
        public void ValidateOnlyNumbersOrNothing()
        {
            var obj = new ModelWithOnlyDigits()
            {
                RegexField = 23,
            };

            var validation = ModelValidator.ValidateEntity(obj);

            Assert.IsFalse(validation.HasError);

            obj = new ModelWithOnlyDigits()
            {
                RegexField = null,
            };

            validation = ModelValidator.ValidateEntity(obj);
            Assert.IsTrue(validation.HasError);
        }
Exemple #24
0
        public void ValidateInvalidRegexWithDependentStringProperty()
        {
            var notEnabled = new ModelWithPositiveNumberAndDependentStringProperty()
            {
                RegexField = -40,
                Enabled    = "false"
            };

            var enabled = new ModelWithPositiveNumberAndDependentStringProperty()
            {
                RegexField = -40,
                Enabled    = "true"
            };

            var validationNotEnabled = ModelValidator.ValidateEntity(notEnabled);
            var validationEnabled    = ModelValidator.ValidateEntity(enabled);

            Assert.IsFalse(validationNotEnabled.HasError);
            Assert.IsTrue(validationEnabled.HasError);
        }
Exemple #25
0
        public void ValidateValidRegexWithDependentBoolProperty()
        {
            var notEnabled = new ModelWithPositiveNumberAndDependentBoolProperty()
            {
                RegexField = 40,
                Enabled    = false
            };

            var enabled = new ModelWithPositiveNumberAndDependentBoolProperty()
            {
                RegexField = 40,
                Enabled    = true
            };

            var validationNotEnabled = ModelValidator.ValidateEntity(notEnabled);
            var validationEnabled    = ModelValidator.ValidateEntity(enabled);

            Assert.IsFalse(validationNotEnabled.HasError);
            Assert.IsFalse(validationEnabled.HasError);
        }
Exemple #26
0
        public void ShouldThrowBecauseOfNonExistingPropertyName()
        {
            var invalid = new ModelWithInvalidDependentProperty();

            Assert.ThrowsException <ValidationException>(() => ModelValidator.ValidateEntity(invalid));
        }