FluentValidation在输入属性为对象列表时不验证List<string>。

huangapple go评论106阅读模式
英文:

FluentValidation does not validate List<string> when input property is List of objects

问题

I have the following model (simplified for brevity):

  1. {
  2. public List&lt;string&gt; Values { get; set; }
  3. }

Then my ApiController as following:

  1. public AdvancedFilter Test(List&lt;AdvancedFilter&gt; filters)
  2. {
  3. return filters.First();
  4. }

Now my Validator looks as following:

  1. {
  2. public AdvancedFilterValidator()
  3. {
  4. RuleFor(x =&gt; x.Values).NotEmpty().ForEach(x =&gt; x.MinimumLength(20));
  5. //RuleForEach(x =&gt; x.Values).MinimumLength(20); (tried both approaches, none of them worked for me)
  6. }
  7. }

When I try to call my endpoint with invalid payload such as

  1. [
  2. {
  3. &quot;values&quot;:[&quot;test&quot;]
  4. }
  5. ]

It does not throw a Validation exception back at me. Interestingly enough, when I change my controller to accept just a single object like this, the Fluent Validation suddenly kicks in as expected.

  1. [HttpPost(&quot;test&quot;)]
  2. public AdvancedFilter Test(AdvancedFilter filters)
  3. {
  4. return filters;
  5. }

I am using ASP Net Core v 6 with FluentValidation.AspNetCore version "11.3.0" (latest).

Bonus question:
My end goal with this validation is to ensure that all input values are numbers (even though they are strings, for certain cases I need to make sure they are int). The expanded model would shed more light on it (still simplified):

  1. {
  2. public AdvancedFilterColumn Column { get; set; }
  3. public List&lt;string&gt; Values { get; set; }
  4. }
  5. public class AdvancedFilterValidator : AbstractValidator&lt;AdvancedFilter&gt;
  6. {
  7. public AdvancedFilterValidator()
  8. {
  9. When(x =&gt; x.Column == AdvancedFilterColumn.Jurisdiction, () =&gt;
  10. {
  11. RuleForEach(x =&gt; x.Values).Must(x =&gt; int.TryParse(x, out _));
  12. });
  13. }
  14. }

As you can see, for a specific case when Column is Jurisdiction, I need to make sure that passed-in values are all numbers. I believe the validation rule is written correctly, yet I cannot test it (due to the above-mentioned problem).

Any help in respect to this matter would be highly appreciated.

英文:

I have the following model (simplified for brevity)

  1. public class AdvancedFilter
  2. {
  3. public List&lt;string&gt; Values { get; set; }
  4. }

Then my ApiController as following

  1. [HttpPost(&quot;test&quot;)]
  2. public AdvancedFilter Test(List&lt;AdvancedFilter&gt; filters)
  3. {
  4. return filters.First();
  5. }

Now my Validator looks as following

  1. public class AdvancedFilterValidator : AbstractValidator&lt;AdvancedFilter&gt;
  2. {
  3. public AdvancedFilterValidator()
  4. {
  5. RuleFor(x =&gt; x.Values).NotEmpty().ForEach(x =&gt; x.MinimumLength(20));
  6. //RuleForEach(x =&gt; x.Values).MinimumLength(20); (tried both approaches, none of them worked for me)
  7. }
  8. }

When I try to call my endpoint with invalid paylaod such as

  1. [
  2. {
  3. &quot;values&quot;:[&quot;test&quot;]
  4. }
  5. ]

It does not throw Validation exception back at me. Interestingly enough, when I change my controller to accept just a single object like this, the fluent validation suddenly kicks in as expected.

  1. [HttpPost(&quot;test&quot;)]
  2. public AdvancedFilter Test(AdvancedFilter filters)
  3. {
  4. return filters;
  5. }

I am using ASP Net Core v 6 with FluentValidation.AspNetCore version "11.3.0" (latest)

Bonus question:
My end goal with this validation is to have ensure that all input values are numbers (even though they are strings, for certain cases I need to make sure they are int). Expanded model would shine more light to it (still simplified)

  1. public class AdvancedFilter
  2. {
  3. public AdvancedFilterColumn Column { get; set; }
  4. public List&lt;string&gt; Values { get; set; }
  5. }
  6. public class AdvancedFilterValidator : AbstractValidator&lt;AdvancedFilter&gt;
  7. {
  8. public AdvancedFilterValidator()
  9. {
  10. When(x =&gt; x.Column == AdvancedFilterColumn.Jurisdiction, () =&gt;
  11. {
  12. RuleForEach(x =&gt; x.Values).Must(x =&gt; int.TryParse(x, out _));
  13. });
  14. }
  15. }

As you can see for specific case, when Column is Jurisdiction I need to make sure that passed in values are all numbers. I believe the validation rule is written correctly, yet I cannot test it (due to abovementioned problem).

Any help in respect to this matter would be highly appreciated.

答案1

得分: 0

  1. Think that you need to build a validator for validating the `AdvancedFilter` collection.
  2. public class AdvancedFilterCollectionValidator : AbstractValidator<IEnumerable<AdvancedFilter>>
  3. {
  4. public AdvancedFilterCollectionValidator()
  5. {
  6. RuleForEach(x => x).SetValidator(new AdvancedFilterValidator());
  7. }
  8. }

And register the validator service in the ConfigureServices method.

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddMvc();
  4. // Other configurations
  5. services.AddScoped<IValidator<AdvancedFilter>, AdvancedFilterValidator>();
  6. services.AddScoped<IValidator<IEnumerable<AdvancedFilter>>, AdvancedFilterCollectionValidator>();
  7. }

Reference: Validation for a for Collection of a type at Root level?

  1. <details>
  2. <summary>英文:</summary>
  3. Think that you need to build a validator for validating the `AdvancedFilter` collection.
  4. ```csharp
  5. public class AdvancedFilterCollectionValidator : AbstractValidator&lt;IEnumerable&lt;AdvancedFilter&gt;&gt;
  6. {
  7. public AdvancedFilterCollectionValidator()
  8. {
  9. RuleForEach(x =&gt; x).SetValidator(new AdvancedFilterValidator());
  10. }
  11. }

And register the validator service in the ConfigureServices method.

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddMvc();
  4. // Other configurations
  5. services.AddScoped&lt;IValidator&lt;AdvancedFilter&gt;, AdvancedFilterValidator&gt;();
  6. services.AddScoped&lt;IValidator&lt;IEnumerable&lt;AdvancedFilter&gt;&gt;, AdvancedFilterCollectionValidator&gt;();
  7. }

Reference: Validation for a for Collection of a type at Root level?

huangapple
  • 本文由 发表于 2023年3月31日 02:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定