使用FluentValidation检查一个或多个属性是否已填充。

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

With FluentValidation check that one are more property are populated

问题

使用FluentValidation,我需要验证五个属性中至少有一个已填充。假设我有以下类:

  1. public class Something
  2. {
  3. public string Required1 { get; set; }
  4. public string Required2 { get; set; }
  5. public string Optional1 { get; set; }
  6. public string Optional2 { get; set; }
  7. public string Optional3 { get; set; }
  8. public string Optional4 { get; set; }
  9. public string Optional5 { get; set; }
  10. }

然后我有一个验证器,具有以下构造函数:

  1. public SomethingValidator()
  2. {
  3. RuleFor(e => e.Required1)
  4. .NotEmpty();
  5. RuleFor(e => e.Required2)
  6. .NotEmpty();
  7. RuleFor(e => e.Required1)
  8. .Must((e, r1) =>
  9. !string.IsNullOrWhiteSpace(e.Optional1)
  10. || !string.IsNullOrWhiteSpace(e.Optional2)
  11. || !string.IsNullOrWhiteSpace(e.Optional3)
  12. || !string.IsNullOrWhiteSpace(e.Optional4)
  13. || !string.IsNullOrWhiteSpace(e.Optional5)
  14. );
  15. }

它可以工作,但似乎有点奇怪,我向属性添加规则,而实际上我想要的是一个针对整个对象的规则。第三个RuleFor()中表达的属性可以是任何属性。它在Must()方法中不是必需的。

是否有更好的内置规则类型?

英文:

With FluentValidation I need to validate that at lest one of five properties is populated. Lets say I have this class:

  1. public class Something
  2. {
  3. public string Required1 { get; set; }
  4. public string Required2 { get; set; }
  5. public string Optional1 { get; set; }
  6. public string Optional2 { get; set; }
  7. public string Optional3 { get; set; }
  8. public string Optional4 { get; set; }
  9. public string Optional5 { get; set; }
  10. }

Then I have a validator with this constructor:

  1. public SomethingValidator()
  2. {
  3. RuleFor(e => e.Required1)
  4. .NotEmpty();
  5. RuleFor(e => e.Required2)
  6. .NotEmpty();
  7. RuleFor(e => e.Required1)
  8. .Must((e, r1) =>
  9. !string.IsNullOrWhiteSpace(e.Optional1)
  10. || !string.IsNullOrWhiteSpace(e.Optional2)
  11. || !string.IsNullOrWhiteSpace(e.Optional3)
  12. || !string.IsNullOrWhiteSpace(e.Optional4)
  13. || !string.IsNullOrWhiteSpace(e.Optional5)
  14. );
  15. }

It works but it seem kinda funky that I'm add a rule to a property when really I want a rule just for the object. The property express in the third RuleFor() could be anything. It's not required in the Must() method.

Is there a better built in rule type?

答案1

得分: 0

你可以从RuleFor(e => e)开始时省略该属性,只需传递目标对象。

  1. RuleFor(e => e)
  2. .Must(e =>
  3. !string.IsNullOrWhiteSpace(e.Optional1)
  4. || !string.IsNullOrWhiteSpace(e.Optional2)
  5. || !string.IsNullOrWhiteSpace(e.Optional3)
  6. || !string.IsNullOrWhiteSpace(e.Optional4)
  7. || !string.IsNullOrWhiteSpace(e.Optional5)
  8. );
英文:

You can ommit that property when you start from RuleFor(e => e) just passing the target object.

  1. RuleFor(e => e)
  2. .Must(e =>
  3. !string.IsNullOrWhiteSpace(e.Optional1)
  4. || !string.IsNullOrWhiteSpace(e.Optional2)
  5. || !string.IsNullOrWhiteSpace(e.Optional3)
  6. || !string.IsNullOrWhiteSpace(e.Optional4)
  7. || !string.IsNullOrWhiteSpace(e.Optional5)
  8. );

huangapple
  • 本文由 发表于 2023年5月21日 01:36:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296546.html
匿名

发表评论

匿名网友

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

确定