SaveApis.Framework

Documentation for SaveApis.Framework

View on GitHub

ValidationInstaller — Console

Key: Validation
Default Enabled:
Package: SaveApis.Framework.Console


Purpose

Scans assemblies for FluentValidation IValidator<T> implementations and registers them in the DI container for use in hosted services, workers, and other background components.


What It Does


Dependencies

No dependencies on other installers.


Configuration

No configuration options. Assembly selection is controlled via the InstallerContext.


InstallerContext Setup

(app, installer) =>
{
    installer.SetSoftwareAssembly();                  // scans the entry assembly
    installer.AddAssembly(typeof(SomeExternalType));  // also scan this assembly
}

InstallerContext Extension

installer.WithValidation(bool enabled = true);

Example

public record ProcessJobRequest(Guid JobId, int MaxRetries);

public class ProcessJobRequestValidator : AbstractValidator<ProcessJobRequest>
{
    public ProcessJobRequestValidator()
    {
        RuleFor(x => x.JobId).NotEmpty();
        RuleFor(x => x.MaxRetries).InclusiveBetween(1, 10);
    }
}

// In a hosted service:
public class JobWorker(IValidator<ProcessJobRequest> validator) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        var request = new ProcessJobRequest(Guid.NewGuid(), 3);
        var result = await validator.ValidateAsync(request, stoppingToken);
        if (!result.IsValid)
        {
            // handle validation errors
        }
    }
}