hanushi-thana
hanushi-thana

Reputation: 1281

Fluent Validation with conditions in asp.net core

I am working in asp. net core 6.0 web API project (clean architecture (CQRS)).

I am using fluent validation for validate command and queries.

public class CreateSiteDestinationSectionsCommand : IRequest<x>
{
    public int DestinationSectionId { get; set; }
    public int DestinationSectionTitleId { get; set; }
    public int SiteCodeId { get; set; }
    public string Description { get; set; }

    public List<DestinationImageDto> Images { get; set; }

    public List<string> Links { get; set; }
}

This is i did inside CreateSiteDestinationSectionsCommandHandler.

var DestinationSectionTitleId = request.DestinationSectionTitleId;

            if (DestinationSectionTitleId != 10)
            {

                if (DestinationSectionTitleId == 1
                || DestinationSectionTitleId == 2
                || DestinationSectionTitleId == 5
                || DestinationSectionTitleId == 7
                || DestinationSectionTitleId == 8
                || DestinationSectionTitleId == 9
                || DestinationSectionTitleId == 11)
                {
                    var sectionimageCount = request.Images.Count;

                    if (sectionimageCount != 1)
                    {
                        throw new ApiValidationException("Section has not more than one image");
                    }
                   else (DestinationSectionTitleId == 10 && request.Images != null)
                   {

                      var sectionimageCount = request.Images.Count;
                      if (sectionimageCount != 0)
                       {
                         throw new ApiValidationException("Section  doesnot have any image");

                       }

                   }

                }

            }

But instead of handling validation inside commandHandler, I have to handle validation in CreateSiteDestinationSectionsCommandValidator.

I tried this,

  RuleFor(x => x.Images)
         .Must(x => x != null)
                .When(x => x.DestinationSectionId != 10)
                .WithMessage("Site Section image required");

        RuleFor(x => x.DestinationSectionId).NotEmpty();
        RuleFor(x => x.Images)
                .Must(x => x.Count != 1)
                .When(x => x.DestinationSectionId == 1
                && x.DestinationSectionId == 1
                && x.DestinationSectionId == 2
                && x.DestinationSectionId == 5
                && x.DestinationSectionId == 7
                && x.DestinationSectionId == 8
                && x.DestinationSectionId == 9
                && x.DestinationSectionId == 11
                )
                .WithMessage("Site Section has not more than one image");
    }

When I check throug postman request, Even I send with DestinationSectionId = 10 (and not sending any images), I got validation error as

"errors": {
        "Images": [
            "Site Section image required"
        ]
    }

And Even I send more than 1 images for DestinationSectionId = 1, I did not get validation error. BUT I shoud get validation error as Site Section has not more than one image

Why this validations not work correctly? What I missed?

Upvotes: 1

Views: 377

Answers (1)

Nemanja Todorovic
Nemanja Todorovic

Reputation: 2800

For this part:

And Even I send more than 1 images for DestinationSectionId = 1, I did not get validation error. BUT I shoud get validation error as Site Section has not more than one image

you are not getting any error because you wrote:

.When(x => x.DestinationSectionId == 1
                && x.DestinationSectionId == 1
                && x.DestinationSectionId == 2
                && x.DestinationSectionId == 5
                && x.DestinationSectionId == 7
                && x.DestinationSectionId == 8
                && x.DestinationSectionId == 9
                && x.DestinationSectionId == 11
                )

which means that DestinationSectionId must be 1 and 2 and 5 and 7 and 8 and 9 and 11 at the same time, which is not possible.

Instead you should write:

.When(x => x.DestinationSectionId == 1
                    || x.DestinationSectionId == 2
                    || x.DestinationSectionId == 5
                    || x.DestinationSectionId == 7
                    || x.DestinationSectionId == 8
                    || x.DestinationSectionId == 9
                    || x.DestinationSectionId == 11
                    )

For this part:

When I check throug postman request, Even I send with DestinationSectionId = 10 (and not sending any images), I got validation error as...

can you add postman request to the question?

Upvotes: 1

Related Questions