For Function

In Validatum, the For function is an extension method used to build a validator that targets the type returned from a selector expression.

The For function can be used to create very complex nested validation functions against the properties (and nested properties) of complex types.

The selector expression makes it easy to target a property, retrieve its value at runtime, and build a nested inline validator against the property's type.


Method
For<T, P>(Expression<Func<T, P>> selector, Action<IValidatorBuilder<P>> func)

The first parameter is the selector expression (a type of lambda expression)

The second parameter is a function that provides an IValidatorBuilder<P> that will target the return type P (Note the second type parameter).

Example Usage
var validator = new ValidatorBuilder<Employee>()
    // FirstName is required with min length of 2 and must contain the letter 'p'
    .For(e => e.FirstName, name => 
        {
            name.Required()
                .MinLength(2)
                .Contains("p");
        })
    // Address must be set with AddressLine1, Postcode and Region required and
    // the Country must equal Australia
    .For(e => e.Address, addr => 
        {
            addr.NotNull()
                .Required(a => a.AddressLine1)
                .Required(a => a.PostCode)
                .Required(a => a.Region)
                .Equal(a => a.Country, "Australia");
        })
    .Build();

// this will not validate
var result = validator.Validate(
    new Employee 
    {
        FirstName = "Steve",
        Address = new Address
        {
            AddressLine1 = "742 Evergreen Terrace",
            PostCode = "3500",
            Region = "VIC",
            Country = "Australia"
        }
    });