FluentValidationLister.Filter 1.5.0

dotnet add package FluentValidationLister.Filter --version 1.5.0
NuGet\Install-Package FluentValidationLister.Filter -Version 1.5.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="FluentValidationLister.Filter" Version="1.5.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FluentValidationLister.Filter --version 1.5.0
#r "nuget: FluentValidationLister.Filter, 1.5.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 FluentValidationLister.Filter as a Cake Addin
#addin nuget:?package=FluentValidationLister.Filter&version=1.5.0

// Install FluentValidationLister.Filter as a Cake Tool
#tool nuget:?package=FluentValidationLister.Filter&version=1.5.0

Build Status NuGet Downloads

FluentValidationLister

v1.5.0

An ASP.NET Core extension for FluentValidation to provide additional endpoints that describe validator metadata for a Web API project.

Execute your server-side FluentValidation rules automatically in the front-end!

This package adds an ActionFilter which will describe the rules and messages defined for any validator. It has been developed using FluentValidation v11.9.0.

It also describes the expected JSON datatype for every field - regardless of whether it has any validation rule applied. This can be very helpful for automatic generation of dynamic forms.

There are generic strict TypeScript handlers for most scenarios included in the sample application. Feel free to copy and modify as required.

Table of Contents

Purpose

If you want to use clientside validation with the FluentValidation library (you really should), then the recommended way is to use the FluentValidation.AspNetCore package with ASP.NET Core MVC. It integrates perfectly and there's no need for this filter at all (note that the same is true for Razor implementations).

However, sometimes you may not be using MVC - you might be using an SPA application, such as React or Angular, or maybe a mobile application front-end. In this case you should probably want your clientside validators to match your server-side FluentValidation validators, without having to duplicate the effort - this filter exposes the validators on the clientside.

You still need to implement this in your front-end validation - this just provides a nice way to access the validator information for any endpoint, exposing and formatting the metadata provided by the FluentValidation library.

In addition to the validator metadata, the expected JSON datatypes are also described for every field - even if they do not have any FluentValidation rules applied. This could allow you to ensure the correct datatype is submitted, or more likely, to ensure the correct input is displayed to the end-user.

In the Web API sample project, I have provided a working TypeScript demonstration using jQuery. It is very basic and will require some extension for your own use, but it demonstrates strict-typing of the returned response and a commented sample of expected usage.

Installation

  1. Install the NuGet package

  2. In the ConfigureServices method of Startup.cs, include a call to AddFluentValidationFilter instead of AddFluentValidation.

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddFluentValidationFilter();
}
  1. In order for ASP.NET to discover your validators, they must be registered with the services collection. You must do this by calling the AddTransient method for each of your validators. Adding all validators in a specified assembly is not supported.
    services.AddFluentValidationFilter();

    services.AddTransient<IValidator<Person>, PersonValidator>();
    // (repeat for every validator)

How to use

For any given endpoint, add the query-string ?validation=1 to the endpoint URL in order to view the validator details, if applicable.

XML is only supported in versions prior to v1.2 but the documentation/examples will be using JSON. XML support was deprecated because none of the consumers of this package seemed to be using XML anyway(!)

Along with the validatorList you are also presented with an errorList of potential messages and a typeList that describes the JSON type for each form element.

Example output for JSON:

{
  "validatorList": {
    "foreName": {
      "required": true,
      "length": {
        "min": 2,
        "max": 10
      }
    },
    "age": {
      "required": true,
      "range": {
        "from": 16,
        "to": 60
      }
    },
    "address.line1": {
      "required": true
    }
  },
  "errorList": {
    "foreName": {
      "required": "'Forename' must not be empty.",
      "length": "'Forename' must be between 2 and 10 characters."
    },
    "age": {
      "required": "'Age' must not be empty.",
      "range": "'Age' must be between 16 and 60."
    },
    "address.line1": {
      "required": "'Line1' must not be empty."
    }
  },
  "typeList": {
    "foreName": "string",
    "age": "number",
    "address.line1": "string",
    "address.line2": "string"
  }
}

Note: You will need to put something in the body for POST/PUT requests, but if you add the validation=1 query-string parameter then the posted body will be ignored - in the demo we simply post {}.

How to use - Rules

The following validator rules are presented to the client:

  • NotNull/NotEmpty (required)
  • Matches (regex)
  • InclusiveBetween (range)
  • ExclusiveBetween (exclusiveRange)
  • Email
  • EqualTo (cross-property equality comparison)
  • MaxLength
  • MinLength
  • Length (including exactLength)

The following validators are presented as "remote" to the client (they can be validated using AJAX, see below):

How to use - AJAX validation

For unsupported validators or validators with custom server-side logic, the rule will be presented as "remote" - the field can be validated using an AJAX call to the back-end.

You can validate any particular field by adding the query string validate=fieldName and if the field is not valid, a problem details will be returned including errors only relevant to the specified field.

How to use - Advanced customisation

You should find all customisation requirements are encapsulated in the ValidationLister class.

If you want to customise this package (such as the returned names for rules), I would advise that you copy/alter this class. Doing so will invalidate the unit tests but you can maintain those in your own fork.

Should you find a more "standard" way of presenting the validation information then please contribute to the project!

Product Compatible and additional computed target framework versions.
.NET 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
1.5.0 98 2/2/2024
1.4.0 1,109 4/30/2022
1.3.0 417 3/13/2022
1.2.2 276 12/24/2021
1.2.1 283 8/6/2021
1.2.0 382 8/1/2021
1.1.2 1,040 10/4/2020
1.1.0 394 8/20/2020
1.0.10 431 8/19/2020
1.0.9 428 8/4/2020
1.0.7 501 2/19/2020
1.0.6 782 12/14/2019
1.0.5 547 12/4/2019
1.0.4 475 11/15/2019
1.0.3 511 11/6/2019
1.0.2 515 11/4/2019
1.0.1 552 10/24/2019
1.0.0 552 10/23/2019

Updated to FluentValidation 11.4.0