I tried multiple solutions on stackoverflow but the validation is not being triggered when i'm validing a list of objects. Data comes in the form of and I validate using fluentvalidation like:
public class Command : IRequest
{
public List<objectsCreateDto> objects { get; set; }
}
public class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
RuleForEach(x => x.objects).SetValidator(new objectsCreateDtoValidator());
}
}
My objectsCreateDtoValidator is:
public class objectsCreateDtoValidator : AbstractValidator<objectsCreateDto>
{
public objectsCreateDtoValidator()
{
RuleFor(x => x.Name).NotEmpty();
RuleFor(X => X.Identifier).NotEmpty();
RuleFor(x => x.UuId).NotEmpty();
}
}
Here is my objectsCreateDto class:
public class objectsCreateDto
{
public Guid UuId { get; set; }
public string Name { get; set; }
public string Identifier { get; set; }
}
This is the JSON i send to API endpoint:
[
{
"uuId": "00000000-0000-0000-0000-000000000000",
"name": "uuid test",
"identifier": "testing order identifier"
}
]
API endpoint is:
[HttpPost]
public async Task<ActionResult<Unit>> Create(List<objectsCreateDto> objectsCreateDtos)
{
return await Mediator.Send(new Create.Command{objects = objectsCreateDtos});
}
Neither of validation is being triggered. Tried all the solutions but didnt work. If I missed something, be kind.
PS: FluentValidation is validating at other places whereas its NOT validating a list of objects in this case