FluentValidationExtensions.AutoMapper 7.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package FluentValidationExtensions.AutoMapper --version 7.0.1
                    
NuGet\Install-Package FluentValidationExtensions.AutoMapper -Version 7.0.1
                    
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="FluentValidationExtensions.AutoMapper" Version="7.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FluentValidationExtensions.AutoMapper" Version="7.0.1" />
                    
Directory.Packages.props
<PackageReference Include="FluentValidationExtensions.AutoMapper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FluentValidationExtensions.AutoMapper --version 7.0.1
                    
#r "nuget: FluentValidationExtensions.AutoMapper, 7.0.1"
                    
#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.
#:package FluentValidationExtensions.AutoMapper@7.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FluentValidationExtensions.AutoMapper&version=7.0.1
                    
Install as a Cake Addin
#tool nuget:?package=FluentValidationExtensions.AutoMapper&version=7.0.1
                    
Install as a Cake Tool

FluentValidation.Extensions.AutoMapper

FluentValidation Extensions for AutoMapper 7, 8 and 11 (and up).

Problem

When the normal MVC validation is defined on a ViewModel and additional validation is done in the business-layer on the dto's using FluentValidation, the error messages are using the property names from the dto instead of the view-model. The following code shows this problem:

ViewModel Example

The flattened Person view model class.

public class Person
{
    [Required]
    public string FirstName { get; set; }

    public string Street { get; set; }

    public string City { get; set; }
}

DTO models example

The PersonDto model class:

public class PersonDto
{
    public string First { get; set; }

    public AddressDto Address { get; set; }
}

The AddressDto model class:

public class AddressDto
{
    public string Street { get; set; }

    public string City { get; set; }
}

When posting an invalid Person object to the WebAPI, you get an error back like (notice that the property names from the DTO are used!):

{
    "First": [
        "no 'a' allowed"
    ],
    "Address.City": [
        "The length of 'City' must be 2 characters or fewer. You entered 3 characters."
    ],
    "Address.Street": [
        "The length of 'Street' must be 3 characters or fewer. You entered 4 characters."
    ]
}

Solution

This library solves this issue by implementing a custom FluentValidation - PropertyNameResolver which uses the AutoMapper mapping definitions to translate the errors with DTO property names into view model errors. Example response will be like:

{
    "City": [
        "The length of 'City' must be 2 characters or fewer. You entered 3 characters."
    ],
    "Street": [
        "The length of 'Street' must be 3 characters or fewer. You entered 4 characters."
    ],
    "FirstName": [
        "no 'a' allowed"
    ]
}

Code Changes

To get this working you need the following changes for a DotNet Core MVC WebAPI project:

1. Install the FluentValidationExtensions.AutoMapper package
dotnet add package FluentValidationExtensions.AutoMapper
2. Dependency Injection Configuration changes

Let the DI framework inject an instance of IMapper into your Configure(...) method. You can then use the injected IMapper to create a new instance from the FluentValidationPropertyNameResolver and assign this to the static ValidatorOptions class.

-public void Configure(IApplicationBuilder app, IHostingEnvironment env)
+public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMapper mapper)
{
     mapper.ConfigurationProvider.AssertConfigurationIsValid();

+    var resolver = new FluentValidationPropertyNameResolver(mapper);
+    ValidatorOptions.PropertyNameResolver = resolver.Resolve;
}

For a complete example, see the code from FluentValidationExample.Web.

Sponsors

Entity Framework Extensions and Dapper Plus are major sponsors and proud to contribute to the development of FluentValidation.Extensions.AutoMapper.

Entity Framework Extensions

Dapper Plus

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.6 is compatible.  netstandard2.0 was computed.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
11.0.1 102 6/22/2025
11.0.0 7,897 8/24/2022
11.0.0-preview-01 191 8/21/2022
8.0.1 93 6/22/2025
8.0.0 724 8/24/2022
8.0.0-preview-01 182 8/21/2022
7.0.1 91 6/22/2025
7.0.0 447 8/24/2022
7.0.0-preview-01 191 8/21/2022
1.0.0 6,625 12/30/2018

See ReleaseNotes.md