title: Fluent Validation with conditions in asp.net core
body: <p>I am working in asp. net core 6.0 web API project (clean architecture (CQRS)).</p>
  <p>I am using fluent validation for validate command and queries.</p>
  <pre><code>public class CreateSiteDestinationSectionsCommand : IRequest&lt;x&gt;
  {
      public int DestinationSectionId { get; set; }
      public int DestinationSectionTitleId { get; set; }
      public int SiteCodeId { get; set; }
      public string Description { get; set; }
  
      public List&lt;DestinationImageDto&gt; Images { get; set; }
  
      public List&lt;string&gt; Links { get; set; }
  }
  </code></pre>
  <p>This is i did inside CreateSiteDestinationSectionsCommandHandler.</p>
  <pre><code>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(&quot;Section has not more than one image&quot;);
                      }
                     else (DestinationSectionTitleId == 10 &amp;&amp; request.Images != null)
                     {
  
                        var sectionimageCount = request.Images.Count;
                        if (sectionimageCount != 0)
                         {
                           throw new ApiValidationException(&quot;Section  doesnot have any image&quot;);
  
                         }
  
                     }
  
                  }
  
              }
  
  </code></pre>
  <p>But instead of handling validation inside commandHandler,  I have to handle validation in CreateSiteDestinationSectionsCommandValidator.</p>
  <p>I tried this,</p>
  <pre><code>  RuleFor(x =&gt; x.Images)
           .Must(x =&gt; x != null)
                  .When(x =&gt; x.DestinationSectionId != 10)
                  .WithMessage(&quot;Site Section image required&quot;);
  
          RuleFor(x =&gt; x.DestinationSectionId).NotEmpty();
          RuleFor(x =&gt; x.Images)
                  .Must(x =&gt; x.Count != 1)
                  .When(x =&gt; x.DestinationSectionId == 1
                  &amp;&amp; x.DestinationSectionId == 1
                  &amp;&amp; x.DestinationSectionId == 2
                  &amp;&amp; x.DestinationSectionId == 5
                  &amp;&amp; x.DestinationSectionId == 7
                  &amp;&amp; x.DestinationSectionId == 8
                  &amp;&amp; x.DestinationSectionId == 9
                  &amp;&amp; x.DestinationSectionId == 11
                  )
                  .WithMessage(&quot;Site Section has not more than one image&quot;);
      }
  </code></pre>
  <p>When I check throug postman request,
  Even I send with DestinationSectionId = 10 (and not sending any images), I got validation error as</p>
  <pre><code>&quot;errors&quot;: {
          &quot;Images&quot;: [
              &quot;Site Section image required&quot;
          ]
      }
  </code></pre>
  <p>And Even I send more than 1 images for  DestinationSectionId = 1, I did not get validation error. BUT I shoud get validation error as <code>Site Section has not more than one image</code></p>
  <p>Why this validations not work correctly? What I missed?</p>