FluentValidation: Output some kind of identity for failing objects
We have the following validation of an input:
public class UserRegisterValidator : AbstractValidator<UserRegister>
{
public UserRegisterValidator()
{
RuleFor(s => s.Users).SetCollectionValidator(new UserValidator());
}
}
public class UserValidator : AbstractValidator<User>
{
public UserValidator ()
{
RuleFor(s => s.Email).NotEmpty()
}
}
This will present the user with messages like
‘Email’ should not be empty.
But who’s email should not be empty?
I’ve seen others getting messages like
'Users[0].Email should not be empty
How can I enable such messages?
But I would like to go one step further. I would like to specify some kind of id for the object, recognizable for the user, like
‘User[John Smith].Email’ should not be empty.
How is that possible?
I guess the answer lies in the PropertyNameResolver
, but I’m not sure how…
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (8 by maintainers)
Another idea I had, was to create a new
WithMessage( )
that get the default message as input:Allowing for ‘light’ customization of the defaults.