The ValidatorBuilder<T> Class

In Validatum all validation begins with the ValidatorBuilder<T> class. You must create an instance of this class in order to build a Validator<T> to validate values. Any type can be used to create a ValidatorBuilder.


The ValidatorBuilder<T> class provides a default implementation of the IValidatorBuilder<T> interface.

This class provides two important functions:

  1. Adding validation functions using the With() method.
  2. Create a Validator<T> instance using the Build() method.


Methods

With<T>(ValidatorDelegate<T> func) (extension)

Returns
IValidatorBuilder<T>

This method is used to add validation functions to the builder.

Example Usage
var builder = new ValidatorBuilder<int>()
    .With(ctx => 
    {
        // perform validation
        if (context.Value == 13)
        {
            context.AddBrokenRule("NotThirteen", "Unlucky", "Value cannot equal 13");
        }
    });

Build(string label = null)

Returns
Validator<T>

This method is used to create a Validator<T> instance.

Example Usage
var validator = new ValidatorBuilder<string>()
    .Required()
    .Build(); // builds a Validator<string> instance
    
// use the validator
var result = validator.Validate("hello");

See also