Giter Club home page Giter Club logo

fluentvalidation.extensions.automapper's Introduction

FluentValidation.Extensions.AutoMapper

FluentValidation Extensions for AutoMapper 7 and 8

NuGet

NuGet Badge FluentValidation.Extensions.AutoMapper

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.

fluentvalidation.extensions.automapper's People

Contributors

goldsam avatar samgzman avatar stefh 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.