In two previous articles, we have seen how to setup fluent validations in ASP .NET Core API project and we also have seen how to use placeholders in the error messages.
In this article, we are going to have a close look at how the messages can be customized to match the application needs.
Overriding the error message
We already have used this method in previous posts. We have passed the error message to this method as shown on the below line.
RuleFor(x => x.Id).GreaterThan(0).WithMessage(“Id is invalid.”);
This method also has another overload. This overload takes a
Func<T, string> as input. Which means any method, which takes type T as input, which is getting validated and returns the string error message can be invoked via
WithMessage to yield the error message.
This overload provides more flexibility on overriding the error message for failed validation.
Overriding the property name in message
We have seen that there are two ways to specify the property names in the error messages – one way is to hard code the name and other way is to use placeholders. Sometimes property names may be longer or some other times we may want to use more meaningful text than just the name of property which is being validated.
For example, let’s say the student model has property name. But we want to be more specific and mention the text “Full Name” in the error message. In that case
As we can directly hardcode the message and property name in the
WithMessage method, this may not appear very useful for simple applications. But in the applications where the common error messages have been decided already with placeholders, this
WithName method may prove handy to correctly set the property name in already defined messages.
WithName method will replace the property name only in the error messages. After the validation fails, you will still find the actual property name in the field “associated property” of the error entry in Errors collection.
If you want to change the property name in error message as well as in the associated properties in error collection, then prefer
OverridePropertyName method instead of
WithName. The syntax is for calling these two method is same.
Custom Display Name Resolver
We can also plug custom logic to resolve the names. For example if the property name is too big which contain multiple words, then we can add a custom logic to add spaces between the words so as to make it readable.
Let’s say, Student type has a property “GivenName” instead of “Name”. And we want to display it in the humanized form, i.e. Given Name. What options do we have ?
- Option 1, is to override the error message using
WithMessageand use hardcoded name in the message
- Option 2, is to use
OverridePropertyNamemethods to override the property names in error messages
- Option 3, is to write a custom member name resolver. This option should be selected only if there is some common logic which needs to be applied at multiple places.
The member name resolution logic can be customized by assigning appropriate
Func value to the property
For the demonstration purpose, I have added a new NuGet package Humanizer, which helps in converting strings to various forms. We want to convert the pascal case word, GivenName to its title case humanized form, Given Name. So, in the web API project, let’s go to Program.cs file and add the code given below:
Also, change the property name from Name to GivenName in the Student class. Now, when we run the API, the startup code would setup the custom resolver. It means if I try to send invalid input for GivenName property, I would see the humanized form of the property name as shown in the snapshot given below.
The code examples given here are just for demonstration purpose. These examples should give us idea about various customization options available with Fluent Validation. I hope you find this information helpful. Let me know your thoughts.