Code Monkey home page Code Monkey logo

fluentvalidation.aspnetcore.http's Introduction

FluentValidation.AspNetCore.Http

Build, Test, and Deploy NuGet.org feedz.io

A simple ASP.NET Core 7+ Minimal API integration for FluentValidation 10+.

The package leverages endpoint filters to trigger FluentValidation IValidator<T>. In case of a validation error, the filter returns a TypedResults.ValidationProblem(errors);, where the errors argument represents the collection of failures. You can customize this behavior by implementing the IFluentValidationEndpointFilterResultsFactory interface.

How to install

dotnet add package ForEvolve.FluentValidation.AspNetCore.Http

Pre-release feedz.io URL:

https://f.feedz.io/forevolve/fluentvalidation/nuget/index.json

How to use

In your Program.cs file, you must register the library:

builder.AddFluentValidationEndpointFilter();

You can then add the validation filter to an endpoint:

using FluentValidation.AspNetCore.Http;
//...
app.MapGet("/some-path", (SomeParamToValidate model) => {
    // ...
}).AddFluentValidationFilter();

You can also add the validation filter to a group instead, which applies the filter to all of the group's endpoints:

using FluentValidation.AspNetCore.Http;
//...
var root = app
    .MapGroup("/")
    .AddFluentValidationFilter()
;
// Then you can register endpoints that will get validated, like:
root.MapGet("/some-path", (SomeParamToValidate model) => {
    //...
});

You must register your validators normally. Here is an example that has nothing to do with this library and only uses FluentValidation:

// Using the assembly scanning feature
builder.Services.AddValidatorsFromAssemblyContaining<Program>();

// Manually registering a validator
builder.Services.AddScoped<IValidator<MyEntity>, MyEntityValidator>();

Settings

You can configure the FluentValidationEndpointFilterSettings class during the registration as follow:

builder.AddFluentValidationEndpointFilter(settings => {
    settings.ScanningStrategy = ScanningStrategy.ScanUntilNoValidatorFound;
});

You can also leverage ASP.NET Core Configure and PostConfigure methods as usual, like this:

builder.Services.Configure<FluentValidationEndpointFilterSettings>(options =>
{
    options.ScanningStrategy = ScanningStrategy.ScanUntilNoValidatorFound;
});

Finally, you can configure the filter in the appsettings.json file under the FluentValidationEndpointFilter key, like this:

{
  "FluentValidationEndpointFilter": {
    "ScanningStrategy": "ScanUntilNoValidatorFound"
  }
}

ScanningStrategy

The only configuration at this time is the scanning strategy that contains the following options:

  1. ScanAllParams (default behavior): The filter scans all parameters. For each parameter, it tries to get an IValidator<T> instance from the ASP.NET Core container. When it finds one, the filter validates the parameter.
  2. ScanUntilNoValidatorFound: The filter scans parameters until it does not find a validator for a parameter. When that happens, the validation stops.

    When using this strategy, you must first add the objects to validate, then add the services or other injected types.

Examples

You can browse the examples directory for usage examples.

Versioning

The package follows semantic versioning and uses Nerdbank.GitVersioning under the hood to automate versioning based on Git commits.

fluentvalidation.aspnetcore.http's People

Contributors

carl-hugo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.