Considering this simplified code:
public class Request
{
public List<Selection> Selections { get; set; }
}
public class Selection
{
public decimal Price { get; set; }
}
public class RequestValidator(): AbstractValidator<Request>
{
public RequestValidator()
{
RuleForEach(x => x.Selections).SetValidator(new SelectionValidator());
}
}
public class SelectionValidator : AbstractValidator<Selection>
{
public SelectionValidator()
{
RuleFor(x => x.Price).NotEqual(0);
}
}
If I have a Selection
item with Price
equal to 0 I do get error message:
'Price' must not be equal to '0'.
What I am missing is reference to which element in collection has this error.
When debugging i can clearly see that Error.PropertyName
is set to Selections[0].Price
, but formatted name is missing that reference to the item.
Is there a way to correctly format the full property name? Maybe by using .WithMessage()
but it does not seems to work.
To make it clear, my goal is to have error on Nested Items look like this:
<CollectionPropertyName>[<index>].<PropertyName> <error text>
. ex. Selectons[0].Price must not be equal to 0