SaveApis.Framework

Documentation for SaveApis.Framework

View on GitHub

ValidationInstaller — Web

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


Purpose

Scans assemblies for FluentValidation IValidator<T> implementations and registers them in the DI container, making them available via constructor injection throughout the application.


What It Does


Dependencies

No dependencies on other installers.


Configuration

This installer has no configuration options. Assembly selection is controlled via the InstallerContext.


InstallerContext Setup

Validators are discovered by scanning assemblies. Make sure the relevant assemblies are registered:

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

InstallerContext Extension

installer.WithValidation(bool enabled = true);

Example Validator

public record CreateUserRequest(string Name, string Email);

public class CreateUserRequestValidator : AbstractValidator<CreateUserRequest>
{
    public CreateUserRequestValidator()
    {
        RuleFor(x => x.Name).NotEmpty().MaximumLength(100);
        RuleFor(x => x.Email).NotEmpty().EmailAddress();
    }
}

Inject and use in a controller or service:

public class UserController(IValidator<CreateUserRequest> validator) : ControllerBase
{
    [HttpPost]
    public async Task<IActionResult> Create(CreateUserRequest request)
    {
        var result = await validator.ValidateAsync(request);
        if (!result.IsValid)
            return BadRequest(result.Errors);
        // ...
    }
}