Conditional Functions

Validatum provides built-in extension methods that allow for conditional execution of a validation function.


When

Executes a function when the predicate resolves to true.

Method
When<T>(Func<ValidationContext<T>, bool> predicate, ValidatorDelegate<T> func)
Example Usage

Validation function is executed when predicate is true

var validator = new ValidatorBuilder<string>()
    .When(
        ctx => context.Value == "trap",
        ctx => 
        {
            if (context.Value == "trap")
            {
                context.AddBrokenRule("ItsATrap", "Trapped", "You fell into my trap");
            }
        })
    .Build();
    
// this will trigger the validation function to be executed
var result = validator.Validate("trap");

Validation function is not executed when predicate is false

var validator = new ValidatorBuilder<string>()
    .When(
        ctx => context.Value == "trap",
        ctx => 
        {
            if (context.Value == "trap")
            {
                context.AddBrokenRule("ItsATrap", "Trapped", "You fell into my trap");
            }
        })
    .Build();
    
// this will not trigger the validation function to be executed
var result = validator.Validate("sucker");

WhenNot

Executes a function when the predicate resolves to false.

Method
WhenNot<T>(Func<ValidationContext<T>, bool> predicate, ValidatorDelegate<T> func)
Example Usage

Validation function is not executed when predicate is true

var validator = new ValidatorBuilder<string>()
    .WhenNot(
        ctx => context.Value == "trap",
        ctx => 
        {
            if (context.Value == "trap")
            {
                context.AddBrokenRule("ItsATrap", "Trapped", "You fell into my trap");
            }
        })
    .Build();
    
// this will not trigger the validation function to be executed
var result = validator.Validate("trap");

Validation function is executed when predicate is false

var validator = new ValidatorBuilder<string>()
    .WhenNot(
        ctx => context.Value == "trap",
        ctx => 
        {
            if (context.Value == "trap")
            {
                context.AddBrokenRule("ItsATrap", "Trapped", "You fell into my trap");
            }
        })
    .Build();
    
// this will trigger the validation function to be executed
var result = validator.Validate("sucker");

If

Allows for the inclusion of a new ValidatorBuilder<T> when the predicate resolves to true.

Method
If<T>(Func<ValidationContext<T>, bool> predicate, Action<IValidatorBuilder<T>> func)
Example Usage

Validator is built and function is executed when predicate is true

var validator = new ValidatorBuilder<string>()
    .If(ctx => context.Value == "howdy",
        (builder) => 
        {
            builder
                .NotNull()
                .Contains("how"); // no need to call Build()
        })
    .Build();

// the conditional validator will be built and executed
var result = validator.Validate("howdy");

Continue

Allows for the inclusion of a new IValidatorBuilder<T> whenValidationContext<T>.IsValid resolves to true.

Method
Continue<T>(Action<IValidatorBuilder<T>> func)
Example Usage
var validator = new ValidatorBuilder<Employee>()
    .For(e => e.Age, v => v.GreaterThan(18))
    // context.IsValid is true
    .Continue((builder) => 
        {
            builder.For(e => e.DriversLicenceNo, v => v.NotNull());
        })
    // this will make context.IsValid be false
    .For(e => e.DriversLicenceNo, v => v.Null())
    // context.IsValid is now false, continue function won't execute
    .Continue((builder) => 
        {
            builder.NotNull();
        })
    .Build();

var result = validator.Validate(new Employee { Age = 22, DriversLicenceNo = "A2345" });