Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Is There an Easier Way to Verify The Reason For Validation Failure?

$
0
0
I have a question regarding testing a specific rule for a property triggered a failure in the validation. Has there ever been any thought to adding "expected message" to the test extensions? Example...

Say you have the following validator
publicclass UserValidator : AbstractValidator<User>
    {
        publicconststring DuplicateEmailMessage = "Email address already exists.";
        publicconststring EmailLengthMessage = "Email address must be between {MinLength} and {MaxLength} characters.";
        publicconststring InvalidEmailMessage = "Email address is not valid.";

        public UserValidator(IEnumerable<User> users)
        {
            RuleFor(x => x.FirstName).NotEmpty().Length(1, 25);
            RuleFor(x => x.LastName).NotEmpty().Length(1, 25);
            RuleFor(x => (string)x.EmailAddress)
                .NotEmpty().WithMessage(EmailLengthMessage)
                .Length(5, 254).WithMessage(EmailLengthMessage)
                .Must(x => x.IsValidEmailAddress()).WithMessage(InvalidEmailMessage)
                .Must((user, emailAddress) =>
                    !users.Any(x =>
                        x.EmailAddress.Address == emailAddress &&
                        x.Id != user.Id))
                        .WithMessage(DuplicateEmailMessage);
        }
    }
To test that duplicate emails are being caught the object would have to be setup to pass all validation prior to email address which makes for an ugly test.
var result = userValidator.Validate(new User
            {
                Id = Guid.NewGuid(),
                FirstName = "Neil",
                LastName = "Peart",
                EmailAddress = new EmailAddress(duplicateEmailAddress)
            });

            result.IsValid.ShouldBeFalse();
            result.Errors.First().ErrorMessage.ShouldEqual(UserValidator.DuplicateEmailMessage);
The extensions provided with FluentValidation can be used to eliminate the need to setup all the properties correctly like so
validator.ShouldHaveValidationErrorFor(x => x.EmailAddress, "test@example.com")
But if the email address is typed incorrectly in the test, the test could still pass if one of the other rules for email address was triggered. So the test would pass but not for the reason expected. It would be nice to check that the message and/or state being returned is the message/state expected. So something like this...
validator.ShouldHaveValidationErrorFor(x => x.EmailAddress, "test@example.com", UserValidator.DuplicateEmailMessage)
Does the question make sense? Is there a different/better way I should handle this situation Would it be possible to add that functionality? I could probably create my own extension method to handle this situation but it seems like it might be a good addition to the built in test extensions. I initially wrote this with the message in mind but if the message includes any placeholders it might not work which is why I also mentioned using expected state instead of expected message. Thoughts?

Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>