Skip to content

Validation Module

The ValidationModule includes tools and components to configure and run validation tasks in an easy way. The module is currently focused on validations that must run before the applications starts, that way they can be handled correctly to ensure our service starts in the required conditions.

This kind of validations are represented by an IStartupValidation. This interface provides a single Validate method, which encapsulates our validation logic, and returns a ValidationResult.

C#
[TransientDependency(typeof(IStartupValidator))]
internal class MyStartupValidator : IStartupValidator
{
    public async Task<ValidationResult> Validate()
    {
        if(conditionMet)
        {
            return ValidationResult.Success();
        }
        else
        {
            return ValidationResult.Error("Some representative message of the error that caused the validation to fault.");
        }
    }
}

Validations will be run sequentially before the application starts. Validation results will then be handled by a ValidationResultHandler instance according to the configured validation behavior. This can be configured when depending on the ValidationModule. There's currently two possible configurable behaviors for validation results with Error type.

  • Exception will trigger an exception with the provided result message.
  • Warning will log the provided message at information level.
C#
1
2
3
4
5
6
7
8
9
public class MyModule : SuiteModule
{
    public override void SetupModule(IModuleBuilder builder)
    {
        base.SetupModule(builder);
        builder.DependsOn<ValidationModule, ValidationModuleOptions>(
            opts => opts.ConfigureStoreValidationBehavior(ValidationBehavior.Error));
    }
}

Info

ValidationBehavior can only be configured globally, as for now. This meas that all validations run will be handled in the same fashion. This is a known limitation that will be addressed in the future.