FlatValidator.DependencyInjection 2.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package FlatValidator.DependencyInjection --version 2.4.0                
NuGet\Install-Package FlatValidator.DependencyInjection -Version 2.4.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FlatValidator.DependencyInjection" Version="2.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FlatValidator.DependencyInjection --version 2.4.0                
#r "nuget: FlatValidator.DependencyInjection, 2.4.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FlatValidator.DependencyInjection as a Cake Addin
#addin nuget:?package=FlatValidator.DependencyInjection&version=2.4.0

// Install FlatValidator.DependencyInjection as a Cake Tool
#tool nuget:?package=FlatValidator.DependencyInjection&version=2.4.0                

The FlatValidator is a validation library for .NET that delivers an high-performance and memory prudence by using lambda-based and strongly-typed rules.

The FlatValidator.DependencyInjection package extends the FlatValidator package to register all custom inherited validators in the IServiceCollection (Microsoft.Extensions.Dependencyinjection.Abstractions) automatically.

public static IServiceCollection AddCustomValidators(this IServiceCollection services)
{
    services.AddFlatValidatorsFromAssembly(Assembly.GetExecutingAssembly());

    return services;
}

Quick examples

1. Inheritance of the FlatValidator class

public record UserModel(string Phone, string ShipmentAddress, string PostalCode);

public class UserValidator: FlatValidator<UserModel> 
{
    public UserValidator(IPostalService postalService) 
    {
        ErrorIf(m => m.Phone.IsPhoneNumber(), "Invalid phone number.", m => m.Phone);
        
        // define one or more groups for preconditions
        If(m => m.ShipmentAddress.NotEmpty(), @then: m =>
        {
            ValidIf(m => postalService.AddressExistsAsync(m.ShipmentAddress, m.PostalCode), 
                    "Invalid postal address and/or postal code.", 
                    m => m.ShipmentAddress, m => m.PostalCode);
        });
    }
}

// .... we want a synchronous version to validate here!
var result = new UserValidator().Validate(new UserModel(...)); 

// possibility to inspect occured validation failures
bool success = result.IsValid;
var errors = result.Errors;
var warnings = result.Warnings;

2. Using FlatValidator in inline mode:

var model = new Model(Email: "email", BirthDate: DateTime.Now, Rate: -100);

// .... now use an asynchronous version!
var result = await FlatValidator.ValidateAsync(model, v => 
{
    // IsEmail() is one of funcs for typical data formats like Phone, Url, CreditCard, etc.
    v.ValidIf(m => m.Email.IsEmail(), "Invalid email", m => m.Email);

    v.ErrorIf(async m => await userService.IsUserExistAsync(m.Email),
              m => $"Email {m.Email} already registered", m => m.Email);
});

if (!result) 
{ 
    // ToDictionary() => Dictionary<PropertyName, ErrorMessage[]>
    var dict = result.ToDictionary(); 
}

Note - You don't need to install the FlatValidator.DependencyInjection package for inline mode usage.

Release Notes and Change Log

Release notes can be found on GitHub.

Supporting the project

The FlatValidator is developed and supported by @belset for free in spare time, so that financial help keeps the projects to be going successfully.

You can sponsor the project via Buy me a coffee.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.5.0 1,294 11/19/2024
2.4.0 5,070 9/20/2024
2.3.0 10,614 5/7/2024
2.2.0 2,387 3/17/2024
2.1.1 373 3/16/2024
2.1.0 268 2/27/2024
2.0.0 1,439 2/1/2024
1.0.0 118 1/27/2024
1.0.0-rc 103 1/25/2024