コード例 #1
0
        public void For_IsNotNull_When_PropertyValueIsNull_Then_ValidationErrorIsReturned()
        {
            // Arrange:
            var validator = new InlineValidator <Person>();

            validator.Setup(x => x.Name)
            .IsNotNull();
            validator.Setup(x => x.Age)
            .IsNotNull();

            var toValidate = new Person
            {
                Name = null,
                Age  = default
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            validationErrors[0].ErrorMessage.Should().Be("Value cannot be null");
            validationErrors[0].ErrorCode.Should().Be("NOT_NULL");
            validationErrors[0].PropertyPath.IsEmpty.Should().BeFalse();
            validationErrors[0].PropertyPath.ToString().Should().Be("Name");
        }
コード例 #2
0
        public void For_IsNull_When_PropertyValuesAreNotNull_Then_ValidationErrorsAreReturned()
        {
            // Arrange:
            var validator = new InlineValidator <Person>();

            validator.Setup(x => x.Name)
            .IsNull();
            validator.Setup(x => x.Age)
            .IsNull();

            var toValidate = new Person
            {
                Name = "Some Name",
                Age  = default
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(2);

            validationErrors[0].ErrorMessage.Should().Be("Value must be null");
            validationErrors[0].ErrorCode.Should().Be("NULL");
            validationErrors[0].PropertyPath.IsEmpty.Should().BeFalse();
            validationErrors[0].PropertyPath.ToString().Should().Be("Name");
            validationErrors[0].ProvidedValue.Should().Be("Some Name");

            validationErrors[1].ErrorMessage.Should().Be("Value must be null");
            validationErrors[1].ErrorCode.Should().Be("NULL");
            validationErrors[1].PropertyPath.IsEmpty.Should().BeFalse();
            validationErrors[1].PropertyPath.ToString().Should().Be("Age");
            validationErrors[1].ProvidedValue.Should().Be(0);
        }
コード例 #3
0
        public void For_IsGreaterThanOrEqualTo_WhenValueIsLessThanMinValue_Then_ValidationErrorIsReturned()
        {
            // Arrange:
            var numberValidator = new InlineValidator <NumberClass>();

            numberValidator.Setup(x => x.IntValue)
            .IsGreaterThanOrEqualTo(0);
            numberValidator.Setup(x => x.FloatValue)
            .IsGreaterThanOrEqualTo(0);
            numberValidator.Setup(x => x.DoubleValue)
            .IsGreaterThanOrEqualTo(0);
            numberValidator.Setup(x => x.DecimalValue)
            .IsGreaterThanOrEqualTo(0);

            var toValidate = new NumberClass
            {
                IntValue     = -1,
                FloatValue   = -1,
                DoubleValue  = -1,
                DecimalValue = -1m
            };

            // Act:
            var validationErrors = numberValidator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(4);
            foreach (var validationError in validationErrors)
            {
                validationError.ErrorMessage.Should().Be("Value must be greater than or equal to 0");
                validationError.ErrorCode.Should().Be("NUM_LESS_THAN");
                validationError.PropertyPath.IsEmpty.Should().BeFalse();
                validationError.ProvidedValue.Should().Be(-1);
            }
        }
コード例 #4
0
        public void For_IsGreaterThan_WhenValueGreaterThanMinValue_Then_NoValidationErrorIsReturned()
        {
            // Arrange:
            var numberValidator = new InlineValidator <NumberClass>();

            numberValidator.Setup(x => x.IntValue)
            .IsGreaterThan(0);
            numberValidator.Setup(x => x.FloatValue)
            .IsGreaterThan(0);
            numberValidator.Setup(x => x.DoubleValue)
            .IsGreaterThan(0);
            numberValidator.Setup(x => x.DecimalValue)
            .IsGreaterThan(0);

            var toValidate = new NumberClass
            {
                IntValue     = 1,
                FloatValue   = 1,
                DoubleValue  = 1,
                DecimalValue = 1m
            };

            // Act:
            var validationErrors = numberValidator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(0);
        }
コード例 #5
0
        public void SimpleNestedInlineValidatorsLvl2()
        {
            // Arrange:
            var line1Validator = new InlineValidator <Line1>();

            line1Validator.Setup(x => x.Street)
            .SetPropertyDisplayName("StreetName")
            .AddRule(x => x != null, "cannot be null", "NOT_NULL")
            .WithMessage("not null")
            .WithCode("MUST_NOT_BE_NULL")
            .StopValidationAfterFailure()
            .AddRule(x => char.IsUpper(x[0]), "must start with uppercase", "STARTS_WITH_UPPER");

            var addressValidator = new InlineValidator <Address>();

            addressValidator.Setup(x => x.Line1)
            .SetPropertyDisplayName("CustomLine1")
            .SetValidator(line1Validator);

            var personValidator = new InlineValidator <Person>();

            personValidator.Setup(x => x.Address)
            .SetValidator(addressValidator)
            .SetPropertyDisplayName("CustomAddress");
            personValidator.Setup(x => x.Age)
            .AddRule(x => x > 0, "must be > 0", "GREATER_THAN_ZERO");

            var toValidate = new Person
            {
                Address = new Address
                {
                    Line1 = new Line1
                    {
                        Street = null
                    }
                },
                Age = -1
            };

            // Act:
            var validationErrors       = personValidator.Validate(toValidate);
            var validationResult       = validationErrors.ToValidationResult();
            var validationResultAsJson = JsonConvert.SerializeObject(validationResult);


            // Assert:
            validationErrors.Count.Should().Be(2);

            validationErrors[0].ErrorMessage.Should().Be("not null");
            validationErrors[0].ErrorCode.Should().Be("MUST_NOT_BE_NULL");
            validationErrors[0].PropertyPath.ToString().Should().Be("CustomAddress.CustomLine1.StreetName");
            validationErrors[0].ProvidedValue.Should().Be(null);

            validationErrors[1].ErrorMessage.Should().Be("must be > 0");
            validationErrors[1].ErrorCode.Should().Be("GREATER_THAN_ZERO");
            validationErrors[1].PropertyPath.ToString().Should().Be("Age");
            validationErrors[1].ProvidedValue.Should().Be(-1);
        }
コード例 #6
0
        public void SimpleInlineValidatorStopsAfterValidationFailureAndOverridesPropertyNameAndApplyErrorTransformations()
        {
            // Arrange:
            var line1Validator = new InlineValidator <Line1>();

            line1Validator.Setup(x => x.Street)
            .SetPropertyDisplayName("StreetName")
            .AddRule(x => x != null, "cannot be null", "NOT_NULL")
            .WithMessage("not null")
            .WithCode("MUST_NOT_BE_NULL")
            .StopValidationAfterFailure()
            .AddRule(x => char.IsUpper(x[0]), "must start with uppercase", "STARTS_WITH_UPPER");
            var toValidate = new Line1
            {
                Street = null
            };

            // Act:
            var validationErrors = line1Validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            validationErrors[0].ErrorMessage.Should().Be("not null");
            validationErrors[0].ErrorCode.Should().Be("MUST_NOT_BE_NULL");
            validationErrors[0].PropertyPath.ToString().Should().Be("StreetName");
            validationErrors[0].ProvidedValue.Should().Be(null);
        }
コード例 #7
0
        public void For_HasLengthBetween_When_MinLengthIsSetToBeGreaterThanMaxLength_Then_ExceptionIsThrown()
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();

            // Act & Assert:
            var exception = Assert.Throws <ArgumentException>(() =>
                                                              validator.Setup(x => x.SomeProperty).HasLengthBetween(10, 5));

            exception.Message.Should().Be("'minLength' cannot be greater than 'maxLength'");
        }
コード例 #8
0
        public void For_HasLength_When_RequiredLengthIsSetToNegativeInteger_Then_ExceptionIsThrown()
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();

            // Act & Assert:
            var exception = Assert.Throws <ArgumentException>(() =>
                                                              validator.Setup(x => x.SomeProperty).HasLength(-1));

            exception.Message.Should().Be("'length' must be >= 0");
        }
コード例 #9
0
        public void For_HasLengthBetween_When_MinOrMaxLengthIsSetToBeNegativeInteger_Then_ExceptionIsThrown(int minLength, int maxLength)
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();

            // Act & Assert:
            var exception = Assert.Throws <ArgumentException>(() =>
                                                              validator.Setup(x => x.SomeProperty).HasLengthBetween(minLength, maxLength));

            exception.Message.Should().Be("'minLength' and 'maxLength' must be >= 0");
        }
コード例 #10
0
        public void For_MatchesRegex_When_InvalidRegexIsProvided_Then_ExceptionIsThrown()
        {
            // Arrange:
            var validator    = new InlineValidator <TestClass>();
            var invalidRegex = "[sde;:\t";

            // Act & Assert:
            var exception = Assert.Throws <ArgumentException>(() =>
                                                              validator.Setup(x => x.SomeProperty).MatchesRegex(invalidRegex));

            exception.Message.Should().Be($"'regex' represents invalid regex");
        }
コード例 #11
0
        public void For_IsNotNull_When_PropertyIsNotNull_Then_ValidationErrorIsNotReturned()
        {
            // Arrange:
            var validator = new InlineValidator <Person>();

            validator.Setup(x => x.Name)
            .IsNotNull();
            validator.Setup(x => x.Age)
            .IsNotNull();

            var toValidate = new Person
            {
                Name = "Some Name",
                Age  = default
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(0);
        }
コード例 #12
0
        public void SimpleNestedInlineValidatorsLvl2WithExceptionIfUnexpectedNullHappensInNestedValidator()
        {
            // Arrange:
            var line1Validator = new InlineValidator <Line1>();

            line1Validator.Setup(x => x.Street)
            .SetPropertyDisplayName("StreetName")
            .AddRule(x => x != null, "cannot be null", "NOT_NULL")
            .WithMessage("not null")
            .WithCode("MUST_NOT_BE_NULL")
            .StopValidationAfterFailure()
            .AddRule(x => char.IsUpper(x[0]), "must start with uppercase", "STARTS_WITH_UPPER");

            var addressValidator = new InlineValidator <Address>();

            addressValidator.Setup(x => x.Line1)
            .SetPropertyDisplayName("CustomLine1")
            .SetValidator(line1Validator);

            var personValidator = new InlineValidator <Person>();

            personValidator.Setup(x => x.Address)
            .SetValidator(addressValidator)
            .SetPropertyDisplayName("CustomAddress");
            personValidator.Setup(x => x.Age)
            .AddRule(x => x > 0, "must be > 0", "GREATER_THAN_ZERO");

            var toValidate = new Person
            {
                Address = null,
                Age     = -1
            };

            // Act:
            Assert.Throws <ArgumentNullException>(() => personValidator.Validate(toValidate));
        }
コード例 #13
0
        public void SimpleInlineValidatorThrowsExceptionBecauseOfInvalidValidationRule()
        {
            // Arrange:
            var line1Validator = new InlineValidator <Line1>();

            line1Validator.Setup(x => x.Street)
            .AddRule(x => x != null, "cannot be null", "NOT_NULL")
            .AddRule(x => char.IsUpper(x[0]), "must start with uppercase", "STARTS_WITH_UPPER");
            var toValidate = new Line1
            {
                Street = null
            };

            // Act & Assert:
            Assert.Throws <NullReferenceException>(() => line1Validator.Validate(toValidate));
        }
コード例 #14
0
        public void For_IsEmailAddress_When_ValidatedStrHasValidEmailFormat_Then_NoValidationErrorIsReturned(string email)
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();

            validator.Setup(x => x.SomeProperty).IsEmailAddress();
            var toValidate = new TestClass
            {
                SomeProperty = email
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(0);
        }
コード例 #15
0
        public void For_MatchesRegex_When_ValidatedStrMatchesRegex_Then_NoValidationErrorIsReturned()
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();
            var regex     = "^S+";

            validator.Setup(x => x.SomeProperty).MatchesRegex(regex);
            var toValidate = new TestClass
            {
                SomeProperty = "SBlah blah"
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(0);
        }
コード例 #16
0
        public void For_HasMaxLength_When_StrHasLengthLessThanOrEqualToMaxLength_Then_NoValidationErrorIsReturned(string value)
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();

            validator.Setup(x => x.SomeProperty)
            .HasMaxLength(5);
            var toValidate = new TestClass
            {
                SomeProperty = value
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(0);
        }
コード例 #17
0
        public void For_IsNotNullOrEmpty_When_ValidatedValueIsNotNullOrEmpty_Then_NoValidationErrorIsReturned()
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();

            validator.Setup(x => x.SomeProperty)
            .IsNotNullOrEmpty();
            var toValidate = new TestClass
            {
                SomeProperty = "  "
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(0);
        }
コード例 #18
0
        public void For_HasLengthBetween_When_ValidatedValueIsInRange_Then_NoValidationErrorIsReturned(string value)
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();

            validator.Setup(x => x.SomeProperty)
            .HasLengthBetween(5, 7);
            var toValidate = new TestClass
            {
                SomeProperty = value
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(0);
        }
コード例 #19
0
        public void For_HasLength_When_ValidatedStrHasLengthDifferentThanRequired_Then_ValidationErrorIsReturned(string value)
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();

            validator.Setup(x => x.SomeProperty).HasLength(5);
            var toValidate = new TestClass
            {
                SomeProperty = value
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            validationErrors[0].ErrorMessage.Should().Be("Must have length equal to 5");
            validationErrors[0].ErrorCode.Should().Be("STR_DIFFERENT_THAN_REQUIRED_LENGTH");
            validationErrors[0].PropertyPath.IsEmpty.Should().Be(false);
            validationErrors[0].PropertyPath.ToString().Should().Be("SomeProperty");
            validationErrors[0].ProvidedValue.Should().Be(value);
        }
コード例 #20
0
        public void For_IsEmailAddress_When_ValidatedStrHasInvalidEmailFormat_Then_ValidationErrorIsReturned(string email)
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();

            validator.Setup(x => x.SomeProperty).IsEmailAddress();
            var toValidate = new TestClass
            {
                SomeProperty = email
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            validationErrors[0].ErrorMessage.Should().Be("Has invalid email format");
            validationErrors[0].ErrorCode.Should().Be("STR_INVALID_EMAIL");
            validationErrors[0].PropertyPath.IsEmpty.Should().Be(false);
            validationErrors[0].PropertyPath.ToString().Should().Be("SomeProperty");
            validationErrors[0].ProvidedValue.Should().Be(email);
        }
コード例 #21
0
        public void For_HasMaxLength_When_StrHasLengthGreaterThanMaxLength_Then_ValidationErrorIsReturned(string value)
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();

            validator.Setup(x => x.SomeProperty)
            .HasMaxLength(5);
            var toValidate = new TestClass
            {
                SomeProperty = value
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            validationErrors[0].ErrorMessage.Should().Be("Cannot have value greater than 5");
            validationErrors[0].ErrorCode.Should().Be("STR_GREATER_THAN_MAX_LENGTH");
            validationErrors[0].PropertyPath.IsEmpty.Should().Be(false);
            validationErrors[0].PropertyPath.ToString().Should().Be("SomeProperty");
            validationErrors[0].ProvidedValue.Should().Be(value);
        }
コード例 #22
0
        public void SimpleInlineValidatorReturnsValidationError()
        {
            // Arrange:
            var line1Validator = new InlineValidator <Line1>();

            line1Validator.Setup(x => x.Street)
            .AddRule(x => x != null, "cannot be null", "NOT_NULL")
            .AddRule(x => char.IsUpper(x[0]), "must start with uppercase", "STARTS_WITH_UPPER");
            var toValidate = new Line1
            {
                Street = "street"
            };

            // Act:
            var validationErrors = line1Validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            validationErrors[0].ErrorMessage.Should().Be("must start with uppercase");
            validationErrors[0].ErrorCode.Should().Be("STARTS_WITH_UPPER");
            validationErrors[0].PropertyPath.ToString().Should().Be("Street");
            validationErrors[0].ProvidedValue.Should().Be("street");
        }
コード例 #23
0
        public void For_IsNotEqual_When_ValidatedValueIsEqualToRequiredValue_Then_PropertyErrorIsReturned()
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();

            validator.Setup(x => x.SomeProperty)
            .IsNotEqual("x");
            var toValidate = new TestClass
            {
                SomeProperty = "x"
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            validationErrors[0].ErrorMessage.Should().Be("Value should not be equal to x");
            validationErrors[0].ErrorCode.Should().Be("STR_IS_EQUAL");
            validationErrors[0].PropertyPath.IsEmpty.Should().Be(false);
            validationErrors[0].PropertyPath.ToString().Should().Be("SomeProperty");
            validationErrors[0].ProvidedValue.Should().Be("x");
        }
コード例 #24
0
        public void For_MatchesRegex_When_ValidatedStrDoesNotMatchRegex_Then_ValidationErrorIsReturned()
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();
            var regex     = "^S+";

            validator.Setup(x => x.SomeProperty).MatchesRegex(regex);
            var toValidate = new TestClass
            {
                SomeProperty = "Blah blah"
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            validationErrors[0].ErrorMessage.Should().Be($"Does not match regex '{regex}'");
            validationErrors[0].ErrorCode.Should().Be("STR_NOT_MATCHING_TO_REGEX");
            validationErrors[0].PropertyPath.IsEmpty.Should().Be(false);
            validationErrors[0].PropertyPath.ToString().Should().Be("SomeProperty");
            validationErrors[0].ProvidedValue.Should().Be("Blah blah");
        }
コード例 #25
0
        public void For_IsNotNullOrEmpty_When_ValidatedValueIsNullOrEmpty_Then_ValidationErrorIsReturned(string value)
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();

            validator.Setup(x => x.SomeProperty)
            .IsNotNullOrEmpty();
            var toValidate = new TestClass
            {
                SomeProperty = value
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            validationErrors[0].ErrorMessage.Should().Be("Value is null or empty but should not be");
            validationErrors[0].ErrorCode.Should().Be("STR_IS_NULL_OR_EMPTY");
            validationErrors[0].PropertyPath.IsEmpty.Should().Be(false);
            validationErrors[0].PropertyPath.ToString().Should().Be("SomeProperty");
            validationErrors[0].ProvidedValue.Should().Be(value);
        }
コード例 #26
0
        public void For_HasLengthBetween_When_ValidatedValueIsOutOfRange_Then_ValidationErrorIsReturned(string value)
        {
            // Arrange:
            var validator = new InlineValidator <TestClass>();

            validator.Setup(x => x.SomeProperty)
            .HasLengthBetween(5, 7);
            var toValidate = new TestClass
            {
                SomeProperty = value
            };

            // Act:
            var validationErrors = validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            validationErrors[0].ErrorMessage.Should().Be("Must have value in range [5, 7](inclusive)");
            validationErrors[0].ErrorCode.Should().Be("STR_LENGTH_OUT_RANGE");
            validationErrors[0].PropertyPath.IsEmpty.Should().Be(false);
            validationErrors[0].PropertyPath.ToString().Should().Be("SomeProperty");
            validationErrors[0].ProvidedValue.Should().Be(value);
        }
コード例 #27
0
        public void SimpleNestedInlineValidatorsLvl1()
        {
            // Arrange:
            var line1Validator = new InlineValidator <Line1>();

            line1Validator.Setup(x => x.Street)
            .SetPropertyDisplayName("StreetName")
            .AddRule(x => x != null, "cannot be null", "NOT_NULL")
            .WithMessage("not null")
            .WithCode("MUST_NOT_BE_NULL")
            .StopValidationAfterFailure()
            .AddRule(x => char.IsUpper(x[0]), "must start with uppercase", "STARTS_WITH_UPPER");

            var addressValidator = new InlineValidator <Address>();

            addressValidator.Setup(x => x.Line1)
            .SetPropertyDisplayName("CustomLine1")
            .SetValidator(line1Validator);

            var toValidate = new Address
            {
                Line1 = new Line1
                {
                    Street = null
                }
            };

            // Act:
            var validationErrors = addressValidator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            validationErrors[0].ErrorMessage.Should().Be("not null");
            validationErrors[0].ErrorCode.Should().Be("MUST_NOT_BE_NULL");
            validationErrors[0].PropertyPath.ToString().Should().Be("CustomLine1.StreetName");
            validationErrors[0].ProvidedValue.Should().Be(null);
        }