In this entity:
public class Foo
{
public Guid Id { get; set; }
public string Type { get; set; }
public string Name { get; set; }
}
How can I customize the validation message in runtime using another property of entity or any other string obtained from the database?
The validation message for RuleFor(foo => foo.Name)
would be:
var msg = "The foo with type '" + foo.Type + "' already exists in the database with name '" + nameInDataBase + "'!"
As I had a complex scenario, the solution that solved my problem was found here: Custom Validators.
Here's the validator code:
With this solution when validation is ok just return
null
. But when there is a validation error return a instance ofValidationFailure
and pass in her constructor the name of the validated property and the validation message.