0

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

2 Answers 2

1

So I got it right, somehow.

There was a mapping issue. Earlier in API endpoint, It was getting a list and was creating an instance of Command. I changed JSON to the way Command class is, to allow it automatically map to the class, after which, Validation started working.

So I changed API endpoint from:

[HttpPost]
public async Task<ActionResult<Unit>> Create(List<objectsCreateDto> objectsCreateDtos)
{
    return await Mediator.Send(new Create.Command{objects = objectsCreateDtos});
}

to:

public async Task<ActionResult<Unit>> Create(Create.Command command)
{
    return await Mediator.Send(command);
}

Changed JSON to:

{
    "objects":  [
            {
                "uuId": "00000000-0000-0000-0000-000000000000",
                "identifier": "testing order identifier",
                "name": "something"
            },
            {
                "uuId": "00000000-0000-0000-0000-000000000000",
                "identifier": "testing order identifier",
                "name": "something"
            },
            {
                "uuId": "00000000-0000-0000-0000-000000000000",
                "identifier": "testing order identifier",
                "name": "something"
            }
        ]
}
Sign up to request clarification or add additional context in comments.

Comments

0

Does the Command-object you're trying to validate contain any objectsCreateDto-items in its list? If not, nothing gets validated. I've tried your code and if I add some objects to the list, everything works fine.

5 Comments

I've added the objectsCreateDto class as well. I hope seeing it may help u understand better
Thanks for the updated sources. I tried it again but still works on my machine. Can you add your call of the validator and an example Command your trying to validate?
Anyway I can debug into the validator method? I tried putting breakpoint in the validator class but it didnt go there
I just noticed, my breakpoint doesnt hit in the CommandValidator class's constructor in this case. But in the other parts of code where validation is working, it does stops at the CommandValidator class's constructor
sorry I think I initialy missunderstood your question, it's not that the validator doesn't work, but that the validator is not triggered at all, correct? Would it be possible if you share a code-snipped from a call where the validation works?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.