'Is there a way in Fluent Validation library to remove the index from the collection validation errors?
I'm using Fluent Validation to validate this model:
class MyModel {
public int Id {get; set;}
public List<ChildModel> Children {get; set;}
}
class ChildModel {
public int CId {get; set;}
public string Name {get; set;}
}
If I added a validation like:
public class MyModelValidator : AbstractValidator<MyModel>
{
public MyModelValidator()
{
RuleForEach(x => x.Children)
.SetValidator(new ChildValidator());
}
}
public class ChildValidator : AbstractValidator<ChildModel>
{
public ChildValidator()
{
RuleFor(r => r.Name).NotEmpty();
}
}
This model and validators, in case of validation errors, will give this response:
{
.....
"Children[0].Name": [
"'Name' must not be empty."
],
"Children[1].Name": [
"'Name` must not be empty."
]
.....
}
I would like to know how to replace Children
in the validation response with the CId
value of the same item that the validation failed for it, like this:
{
.....
"12345[0].Name": [
"'Name' must not be empty."
],
"99887[1].Name": [
"'Name` must not be empty."
]
.....
}
12345
and 99887
are the value of the CId
of items in Children
(item in index 0 and item in index 1).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|