Skip to content

Combine multiple ValidationResults #2125

@leandromoh

Description

@leandromoh

Is your feature request related to a problem? Please describe.

No, just a useful method that I could not find in the lib.
Is there any "native" way to combine/join multiples ValidationResult's?

Describe the solution you'd like

It is very useful to combine/join mutiples ValidationResults into a new ValidationResults, for example..

  ValidationResult fullValidation = new[]
  {
      ValidationA(obj),
      ValidationB(obj),
      ValidationC(obj),
  }
  .Combine();

ValidationResult ValidationA(Person x) { ... }
ValidationResult ValidationB(Person x) { ... }
ValidationResult ValidationC(Person x) { ... }

Describe alternatives you've considered

In my current system I've added the following extension methods.
If there are interest to make them part of the library (in case of no current native solution) I can submit a PR.

public static ValidationResult Combine(this ValidationResult validationResult, params ValidationResult[] failuresResult)
{
    return failuresResult.Append(validationResult).Combine();
}

public static ValidationResult Combine(this IEnumerable<ValidationResult> failuresResult)
{
    var errors = failuresResult.SelectMany(x => x.Errors);

    return new ValidationResult(errors);
}

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions