Using the ValidateOptionsResultBuilder class
ValidateOptionsResultBuilder is a new type in .NET 8. It allows us to dynamically accumulate validation errors and create a ValidateOptionsResult object representing its current state.
The name of the project in the source code is ValidateOptionsResultBuilder.
Its basic usage is straightforward, as we are about to see.
In this project, we are validating the MyOptions object. The type has multiple validation rules, and we want to ensure we are not stopping after the first rule fails validation so a consumer would know all the errors in one go. To achieve this, we decided to use the ValidateOptionsResultBuilder class.
Let’s start with the options class:
namespace ValidateOptionsResultBuilder;
public class MyOptions
{
public string? Prop1 { get; set; }
public string? Prop2 { get; set; }
}
Next, let’s implement a validator class that enforces that both properties can’t be empty...