Placeholders in Fluent Validations
Placeholders in Fluent Validations

ASP .NET Core API – Placeholders in Fluent Validation Messages

In previous article, we have seen how to create validator and apply validation logic to the inputs in ASP .NET Core APIs. We also have seen how to add our own error messages for the validators.

Sometimes in the error messages we want to include some things like property names or property values or other values which are specified in the validation rule. For that purpose, fluent validation library provides various in-built placeholders.

This article will explain various built-in placeholders which can be used in the error messages. For demonstration purpose, we are going to have a look at the same validator that we created in previous post. Then we are going to see how the placeholders can be used.

Why ?

Let’s consider the validator that we created in previous article.

Below are the two error messages which we used for name property.

  • The ‘Name’ should have at least 10 characters.
  • The ‘Name’ should have not more than 250 characters.

As we can see, the property name, (i.e. ‘Name’) is hardcoded here in the message. What if tomorrow we change this property name to FirstName ? We will always need to make sure that the property name is updated in the error message as well.

Another thing, we also have hard coded two numbers, which signify the maximum allowed length and minimum allowed length. In future, if the business decides to change these numbers, developer will have to make sure that the change is made at two places.

Placeholders can help to reduce the duplication and maintain the consistency.

Which are some commonly used placeholders?

The two placeholders, mentioned below, are available with all validators.

  • {PropertyName}, which can be used to include the property name in the error message
  • {PropertyValue}, which can be used to include the property value in the error message

For comparison related validators (e.g. GreaterThan, LessThan, etc.), two other placeholders can be used.

  • {ComparisonValue} – the value with which the property under validation is being compared
  • {ComparisonProperty} – the name of the property which is being used to hold the comparison value (if any)

In length related validations, we can use below mentioned placeholders:

  • {MinLength} – minimum allowed length
  • {MaxLength} – maximum allowed length
  • {TotalLength} – total length of the input

There is a validator, ScaleValidator, which is to validate precision and scale of the incoming number. Below mentioned placeholders can be used with this validator.

  • {ExpectedPrecision} – expected precision specified in the rule
  • {ExpectedScale} – expected scale specified in the rule
  • {Digits} – total number of digits in input
  • {ActualScale} – actual scale from the input

You can refer to the built-in validators page to check the placeholders supported by each validator.

How to use those placeholders ?

Using the placeholders is simple. Instead of hardcoding the property names and values, we can use placeholders in the message. Fluent validation library will take care of replacing those placeholders with their respective values.

For example, the two error messages mentioned for Student’s Name property can be rewritten as:

  • The ‘{PropertyName}’ should have at least {MinLength} characters.
  • The ‘{PropertyName}’ should have not more than {MaxLength} characters.

The modified code of validator is provided in the code snippet given below. In the code snippet, all the error messages have been updated to use placeholders.

Run and Verify

Now if we run the application, it will present us the swagger page. If we try out API and try some invalid inputs, it should show the errors as shown in the snapshot given below.

ASP .NET Core API – Fluent Validation Placeholders Demo

I hope you find this information helpful. Let me know your thoughts.

Leave a ReplyCancel reply