Giter VIP home page Giter VIP logo

sonic198 / fluentvalidation.autovalidation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sharpgrip/fluentvalidation.autovalidation

0.0 0.0 0.0 80 KB

SharpGrip FluentValidation AutoValidation is an extension of the FluentValidation library enabling automatic asynchronous validation in MVC controllers and minimal APIs (endpoints).

Home Page: https://sharpgrip.net

License: MIT License

C# 100.00%

fluentvalidation.autovalidation's Introduction

SharpGrip FluentValidation AutoValidation

Builds

FluentValidation.AutoValidation [Build]

Quality Gate Status
Maintainability Rating
Reliability Rating
Security Rating
Coverage

Introduction

SharpGrip FluentValidation AutoValidation is an extension of the FluentValidation (v10+) library enabling automatic asynchronous validation in MVC controllers and minimal APIs (endpoints). The library FluentValidation.AspNetCore is no longer being maintained and is unsupported. As a result, support for automatic validation provided by this library is no longer available. This library re-introduces this functionality for MVC controllers and introduces automatic validation for minimal APIs (endpoints). It enables developers to easily implement automatic validation in their projects.

Installation

Register your validators with the Microsoft DI service container, for instructions on setting that up please see https://docs.fluentvalidation.net/en/latest/di.html.

MVC controllers NuGet

For MVC controllers reference NuGet package SharpGrip.FluentValidation.AutoValidation.Mvc (https://www.nuget.org/packages/SharpGrip.FluentValidation.AutoValidation.Mvc).

using SharpGrip.FluentValidation.AutoValidation.Mvc.Extensions;

builder.Services.AddFluentValidationAutoValidation();

Minimal APIs (endpoints) NuGet

For minimal APIs (endpoints) reference NuGet package SharpGrip.FluentValidation.AutoValidation.Endpoints (https://www.nuget.org/packages/SharpGrip.FluentValidation.AutoValidation.Endpoints).

Enabling minimal API (endpoint) automatic validation can be done on both route groups and routes.

using SharpGrip.FluentValidation.AutoValidation.Endpoints.Extensions;

builder.Services.AddFluentValidationAutoValidation();

var app = builder.Build();
var endpointGroup = app.MapGroup("/some-group").AddFluentValidationAutoValidation();
endpointGroup.MapPost("/", (SomeModel someModel) => $"Hello {someModel.Name}");

app.MapPost("/", (SomeOtherModel someOtherModel) => $"Hello again {someOtherModel.Name}").AddFluentValidationAutoValidation();

Configuration

MVC controllers

Property Default value Description
DisableBuiltInModelValidation false Disables the built-in .NET model (data annotations) validation.
ValidationStrategy ValidationStrategy.All Configures the validation strategy. Validation strategy ValidationStrategy.All enables asynchronous automatic validation on all controllers inheriting from ControllerBase. Validation strategy ValidationStrategy.Annotations enables asynchronous automatic validation on controllers inheriting from ControllerBase decorated (class or method) with a [FluentValidationAutoValidationAttribute] attribute.
EnableBodyBindingSourceAutomaticValidation true Enables asynchronous automatic validation for parameters bound from BindingSource.Body binding sources (typically parameters decorated with the [FromBody] attribute).
EnableFormBindingSourceAutomaticValidation false Enables asynchronous automatic validation for parameters bound from BindingSource.Form binding sources (typically parameters decorated with the [FromForm] attribute).
EnableQueryBindingSourceAutomaticValidation true Enables asynchronous automatic validation for parameters bound from BindingSource.Query binding sources (typically parameters decorated with the [FromQuery] attribute).
EnablePathBindingSourceAutomaticValidation false Enables asynchronous automatic validation for parameters bound from BindingSource.Path binding sources (typically parameters decorated with the [FromRoute] attribute).
EnableCustomBindingSourceAutomaticValidation false Enables asynchronous automatic validation for parameters bound from BindingSource.Custom binding sources.
using SharpGrip.FluentValidation.AutoValidation.Mvc.Extensions;

builder.Services.AddFluentValidationAutoValidation(configuration =>
{
    // Disable the built-in .NET model (data annotations) validation.
    configuration.DisableBuiltInModelValidation = true;

    // Only validate controllers decorated with the `FluentValidationAutoValidation` attribute.
    configuration.ValidationStrategy = ValidationStrategy.Annotation;

    // Enable validation for parameters bound from `BindingSource.Body` binding sources.
    configuration.EnableBodyBindingSourceAutomaticValidation = true;

    // Enable validation for parameters bound from `BindingSource.Form` binding sources.
    configuration.EnableFormBindingSourceAutomaticValidation = true;

    // Enable validation for parameters bound from `BindingSource.Query` binding sources.
    configuration.EnableQueryBindingSourceAutomaticValidation = true;

    // Enable validation for parameters bound from `BindingSource.Path` binding sources.
    configuration.EnablePathBindingSourceAutomaticValidation = true;

    // Enable validation for parameters bound from 'BindingSource.Custom' binding sources.
    configuration.EnableCustomBindingSourceAutomaticValidation = true;

    // Replace the default result factory with a custom implementation.
    configuration.OverrideDefaultResultFactoryWith<CustomResultFactory>();
});

public class CustomResultFactory : IFluentValidationAutoValidationResultFactory
{
    public IActionResult CreateActionResult(ActionExecutingContext context, ValidationProblemDetails? validationProblemDetails)
    {
        return new BadRequestObjectResult(new {Title = "Validation errors", ValidationErrors = validationProblemDetails?.Errors});
    }
}

Minimal APIs (endpoints)

using SharpGrip.FluentValidation.AutoValidation.Endpoints.Extensions;

builder.Services.AddFluentValidationAutoValidation(configuration =>
{
    // Replace the default result factory with a custom implementation.
    configuration.OverrideDefaultResultFactoryWith<CustomResultFactory>();
});

public class CustomResultFactory : IFluentValidationAutoValidationResultFactory
{
    public IResult CreateResult(EndpointFilterInvocationContext context, ValidationResult validationResult)
    {
        var validationProblemErrors = validationResult.ToValidationProblemErrors();
        return Results.ValidationProblem(validationProblemErrors, "Some details text.", "Some instance text.", (int) HttpStatusCode.BadRequest, "Some title.");
    }
}

fluentvalidation.autovalidation's People

Contributors

mvdgun avatar jonmotos avatar alshuriga 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.