I'm having a play around with http://fluentvalidation.codeplex.com/ to validate some domain models.
I have a typical scenario where I want to validate a string, for example...
RuleFor(x => x.MyString).NotNull().NotEmpty().Length(2, 20).WithMessage("Please provide a string with a minium of 2 characters.");
...that all works just fine and dandy until I create a unit test that specifies that the MyString property must have a length of 2-20 characters not including whitespace.
So myObject.myString = "A" + new String(' ', 10);
should fail validation.
I can get all this working with a .Must(IsValidString)
and write all the logic myself in a...
private bool IsValidString(string myString)
{
if(String.IsNullOrEmpty(myString))
return false;
// Then work on myString.Trim()'ed value.
}
...but then I loose all the lovely fluentvalidationness!!
Obviously I can make my unit test pass using this method, and all will be happy in my little the world, but am I missing a trick?
Many thanks.
.NotNull()
call as the.NotEmpty()
call after handles null as well. – Bravery