Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 验证集合,FluentValidation为属性返回一个失败的规则错误_C#_Fluentvalidation - Fatal编程技术网

C# 验证集合,FluentValidation为属性返回一个失败的规则错误

C# 验证集合,FluentValidation为属性返回一个失败的规则错误,c#,fluentvalidation,C#,Fluentvalidation,我刚刚开始使用FluentValidation v9.x,我想知道如何在集合中验证规则 基本上如果我有一个物质的集合 public class Substance { public int? SubstanceId { get; set; } public string SubstanceName { get; set; } public decimal? SubstanceAmount { get; set; } public int? SubstanceUnitId { ge

我刚刚开始使用FluentValidation v9.x,我想知道如何在集合中验证规则

基本上如果我有一个物质的集合

public class Substance
{
  public int? SubstanceId { get; set; }
  public string SubstanceName { get; set; }
  public decimal? SubstanceAmount { get; set; }
  public int? SubstanceUnitId { get; set; }
  public int? SubstanceRouteId { get; set; }
  public DateTimeOffset? SubstanceTime { get; set; }
}
我想验证一下:

  • 如果SubstanceAmount和SubstanceUnitId具有值,以及
  • 如果SubstanceRouteId有一个值,并且
  • 如果SubstanceTime有一个值 在任何物质项目上
我希望在每个规则失败时返回一条错误消息,而不是针对每种物质,这就是现在发生的以下情况:

RuleForEach(x => x.SubstanceList).SetValidator(new SubstanceValidator(RuleSetsToApply));

public class SubstanceValidator : AbstractValidator<Substance>
{
  public SubstanceValidator(List<ValidationRule> RuleSetsToApply)
  {
    string ruleSetName = "SubstanceAmountUnit";
    RuleSet(ruleSetName, () => {
      RuleFor(x => x.SubstanceAmount).NotNull().NotEmpty();
      RuleFor(x => x.SubstanceUnitId).NotNull().NotEmpty().GreaterThan(0);
    });

    ruleSetName = "SubstanceIngestion";
    RuleSet(ruleSetName, () => {
      RuleFor(x => x.SubstanceTime).NotNull().NotEmpty();
    });

    ruleSetName = "SubstanceRoute";
    RuleSet(ruleSetName, () => {
      RuleFor(x => x.SubstanceRouteId).NotNull().NotEmpty().GreaterThan(0);
    });
  }
}
RuleForEach(x=>x.SubstanceList).SetValidator(新的SubstanceValidator(RuleSetsToApply));
公共类实体验证器:AbstractValidator
{
公共实体验证器(列表规则集适用)
{
string ruleSetName=“SubstanceAmountUnit”;
规则集(规则集名称,()=>{
RuleFor(x=>x.SubstanceAmount).NotNull().NotEmpty();
(x=>x.SubstanceUnitId).NotNull().NotEmpty().greaterth(0)的规则;
});
ruleSetName=“SubstanceIngestion”;
规则集(规则集名称,()=>{
RuleFor(x=>x.SubstanceTime).NotNull().NotEmpty();
});
ruleSetName=“Subsubceroute”;
规则集(规则集名称,()=>{
RuleFor(x=>x.SubstanceRouteId).NotNull().NotEmpty().greaterth(0);
});
}
}
所以如果我有五种物质和

  • 第一种物质不符合规则2,
  • 第三条不符合规则#1和#2和
  • 第四个不符合规则#3,
    我希望每个规则返回一个错误,即使规则#2失败了两次

如何实现这一点?

如果我正确理解了问题,在这种情况下,我将在父
实体列表
上定义验证规则,而不是在
实体验证器上定义验证规则

为了简洁起见,我没有考虑您的附加规则集逻辑,因为我对它了解不够,或者它是否确实是必需的。但是,以下将生成您拥有的三个验证案例:

  • 如果SubstanceAmount和SubstanceUnitId具有值,以及
  • 如果SubstanceRouteId有一个值,并且
  • 如果SubstanceTime在任何物质项上都有值

谢谢你,我会尝试一下,很快就成功了,谢谢你!
RuleFor(x => x.SubstanceList)
    .Must(x => x != null ? x.All(y => y.SubstanceAmount.HasValue && y.SubstanceUnitId.HasValue && y.SubstanceUnitId.Value <= 0) : true)
    .WithMessage(x => "One or more substance amounts or unit ids has not been provided, and/or one or more unit ids is less than or equal to 0.");

RuleFor(x => x.SubstanceList)
    .Must(x => x != null ? x.All(y => y.SubstanceTime.HasValue) : true)
    .WithMessage(x => "One or more substance times has not been provided.");

RuleFor(x => x.SubstanceList)
    .Must(x => x != null ? x.All(y => y.SubstanceRouteId.HasValue && y.SubstanceRouteId.HasValue && y.SubstanceRouteId.Value <= 0) : true)
    .WithMessage(x => "One or more substance route ids has not been provided or is less than or equal to 0.");
void Main()
{
    var fixture = new Fixture();
    var substances = new List<Substance>();
    substances.Add(fixture.Build<Substance>().Without(x => x.SubstanceTime).Create());
    substances.Add(fixture.Create<Substance>());
    substances.Add(fixture.Build<Substance>().Without(x => x.SubstanceAmount).Without(x => x.SubstanceTime).Create());
    substances.Add(fixture.Build<Substance>().With(x => x.SubstanceRouteId, -1).Create());
    substances.Add(fixture.Create<Substance>());
    Console.WriteLine(substances);

    var foo = new Foo() { SubstanceList = substances };
    var validator = new FooValidator();
    var validationResult = validator.Validate(foo);
    Console.WriteLine(validationResult.Errors.Select(x => x.ErrorMessage));
}

public class Substance
{
    public int? SubstanceId { get; set; }
    public string SubstanceName { get; set; }
    public decimal? SubstanceAmount { get; set; }
    public int? SubstanceUnitId { get; set; }
    public int? SubstanceRouteId { get; set; }
    public DateTimeOffset? SubstanceTime { get; set; }
}

public class Foo
{
    public List<Substance> SubstanceList { get; set; }
}

public class FooValidator : AbstractValidator<Foo>
{
    public FooValidator()
    {
        RuleFor(x => x.SubstanceList)
            .Must(x => x != null ? x.All(y => y.SubstanceAmount.HasValue && y.SubstanceUnitId.HasValue && y.SubstanceUnitId.Value <= 0) : true)
            .WithMessage(x => "One or more substance amounts or unit ids has not been provided, and/or one or more unit ids is less than or equal to 0.");

        RuleFor(x => x.SubstanceList)
            .Must(x => x != null ? x.All(y => y.SubstanceTime.HasValue) : true)
            .WithMessage(x => "One or more substance times has not been provided.");

        RuleFor(x => x.SubstanceList)
            .Must(x => x != null ? x.All(y => y.SubstanceRouteId.HasValue && y.SubstanceRouteId.HasValue && y.SubstanceRouteId.Value <= 0) : true)
            .WithMessage(x => "One or more substance route ids has not been provided or is less than or equal to 0.");
    }
}