Skip to content

Validator Configuration Setup

Jon edited this page Apr 27, 2019 · 1 revision

How to setup AutoValidator?

Assuming Schema / class validation wants to be used the user will want to configure autoValidator before first use.

var validator = new AutoValidation(cfg =>
{
    cfg.AddProfile<Profile1>();
});

AutoValidation takes an expression ValidatorConfigurationExpression which allows the user to define what profiles should be loaded into the factory.

What is ValidatorConfigurationExpression?

ValidatorConfigurationExpression is what you use to setup your model mappings. This is only required if you want to use schema validation. It allows you to define how your models should be checked to see if they are valid or not.

How do I set it up?

The setup only needs to happen once in your application, (assuming you keep a reference to the configuration or factory to create validators later).

You can create a ValidatorConfigurationExpression object and setup its values, then pass that to the AutoValidation or you can use the inline expression to define the expression in the AutoValidation constructor:

ValidatorConfigurationExpression options

void AddProfile<TProfile>() where TProfile : IClassValidationProfile, new();
void AddProfile(Type profileType);
void AddProfiles(Assembly assemblyToScan);
void AddProfiles(IEnumerable<Assembly> assembliesToScan);

How to use AutoValidation

You have two options with the main AutoValidation entity.

  1. Create Factory
  2. Create function that points to the creation of a Factory.

At this point you can create a factory and use that to create your validators as required.