Thanks Jeremy. While experimenting yesterday, I was starting to be able to replace the property name placeholders within the strings as follows (excuse the hacked together code):
[Test] //this passes public void CanGetFormattedMessageFromPropertyValidator() { TestClassValidator validator = new TestClassValidator(); var descriptor = validator.CreateDescriptor(); var validatedMembers = descriptor.GetMembersWithValidators(); foreach (var item in validatedMembers) { string propertyName = item.Key; foreach(var propertyValidator in validatedMembers[propertyName]) { var message = propertyValidator.ErrorMessageSource; MessageFormatter messageFormatter = new MessageFormatter(); messageFormatter.AppendPropertyName(propertyName); string formattedMessage = messageFormatter.BuildMessage(message.GetString()); Assert.That(formattedMessage, Is.EqualTo("'Name' should not be empty.")); } } } private class TestClass { public string Name { get; set; } } private class TestClassValidator : AbstractValidator<TestClass> { public TestClassValidator() { RuleFor(x => x.Name).NotEmpty(); } }
I'm sure this will get more complex in other situations (i.e {MinLength} etc.), but it does look like a lot of the plumbing to do this may already be there even if it wasn't intended for this. I'll try to see if I can get formatted messages out of some the other validators. If it works out, I'll post the code somewhere with the ever so slight hope the concept may be considered for the library :-). Thanks again!