{"id":15823884,"url":"https://github.com/stefh/fluentvalidation.extensions.automapper","last_synced_at":"2025-05-08T20:28:47.810Z","repository":{"id":101484032,"uuid":"163545844","full_name":"StefH/FluentValidation.Extensions.AutoMapper","owner":"StefH","description":"FluentValidation Extensions for AutoMapper","archived":false,"fork":false,"pushed_at":"2025-05-01T07:11:31.000Z","size":41,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-06T22:29:28.098Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/StefH.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["StefH"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":"https://www.paypal.me/stefheyenrath"}},"created_at":"2018-12-29T22:41:14.000Z","updated_at":"2024-11-17T09:32:02.000Z","dependencies_parsed_at":"2025-04-19T07:40:10.341Z","dependency_job_id":"c9116ad4-65d8-49c4-ae41-43df142e2afa","html_url":"https://github.com/StefH/FluentValidation.Extensions.AutoMapper","commit_stats":{"total_commits":14,"total_committers":2,"mean_commits":7.0,"dds":0.4285714285714286,"last_synced_commit":"f69ce3de6292ab5eded205dd1bd15d4ff3703439"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StefH%2FFluentValidation.Extensions.AutoMapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StefH%2FFluentValidation.Extensions.AutoMapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StefH%2FFluentValidation.Extensions.AutoMapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StefH%2FFluentValidation.Extensions.AutoMapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StefH","download_url":"https://codeload.github.com/StefH/FluentValidation.Extensions.AutoMapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253143911,"owners_count":21860964,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-05T08:24:00.570Z","updated_at":"2025-05-08T20:28:47.787Z","avatar_url":"https://github.com/StefH.png","language":"C#","funding_links":["https://github.com/sponsors/StefH","https://www.paypal.me/stefheyenrath"],"categories":[],"sub_categories":[],"readme":"# FluentValidation.Extensions.AutoMapper\nFluentValidation Extensions for AutoMapper 7, 8 and 11.\n\n\n\n## NuGet\n[![NuGet Badge FluentValidation.Extensions.AutoMapper](https://img.shields.io/nuget/v/FluentValidationExtensions.AutoMapper)](https://www.nuget.org/packages/FluentValidationExtensions.AutoMapper)\n\n# Problem\n\nWhen 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.\nThe following code shows this problem:\n\n### ViewModel Example\nThe flattened **Person** view model class.\n\n``` c#\npublic class Person\n{\n    [Required]\n    public string FirstName { get; set; }\n\n    public string Street { get; set; }\n\n    public string City { get; set; }\n}\n```\n\n\n# DTO models example\n\nThe **PersonDto** model class:\n``` c#\npublic class PersonDto\n{\n    public string First { get; set; }\n\n    public AddressDto Address { get; set; }\n}\n```\n\nThe **AddressDto** model class:\n``` c#\npublic class AddressDto\n{\n    public string Street { get; set; }\n\n    public string City { get; set; }\n}\n```\n\nWhen posting an invalid Person object to the WebAPI, you get an error back like (notice that the property names from the DTO are used!):\n``` json\n{\n    \"First\": [\n        \"no 'a' allowed\"\n    ],\n    \"Address.City\": [\n        \"The length of 'City' must be 2 characters or fewer. You entered 3 characters.\"\n    ],\n    \"Address.Street\": [\n        \"The length of 'Street' must be 3 characters or fewer. You entered 4 characters.\"\n    ]\n}\n```\n\n# Solution\nThis 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.\nExample response will be like:\n``` json\n{\n    \"City\": [\n        \"The length of 'City' must be 2 characters or fewer. You entered 3 characters.\"\n    ],\n    \"Street\": [\n        \"The length of 'Street' must be 3 characters or fewer. You entered 4 characters.\"\n    ],\n    \"FirstName\": [\n        \"no 'a' allowed\"\n    ]\n}\n```\n\n## Code Changes\nTo get this working you need the following changes for a DotNet Core MVC WebAPI project:\n\n\n#### 1. Install the `FluentValidationExtensions.AutoMapper` package\n``` cmd\ndotnet add package FluentValidationExtensions.AutoMapper\n```\n\n#### 2. Dependency Injection Configuration changes\n\nLet the DI framework inject an instance of `IMapper` into your `Configure(...)` method.\nYou can then use the injected `IMapper` to create a new instance from the `FluentValidationPropertyNameResolver` and assign this to the static `ValidatorOptions` class.\n\n``` diff\n-public void Configure(IApplicationBuilder app, IHostingEnvironment env)\n+public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMapper mapper)\n{\n     mapper.ConfigurationProvider.AssertConfigurationIsValid();\n\n+    var resolver = new FluentValidationPropertyNameResolver(mapper);\n+    ValidatorOptions.PropertyNameResolver = resolver.Resolve;\n}\n```\n\nFor a complete example, see the code from [FluentValidationExample.Web](https://github.com/StefH/FluentValidation.Extensions.AutoMapper/tree/master/examples/FluentValidationExample.Web).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstefh%2Ffluentvalidation.extensions.automapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstefh%2Ffluentvalidation.extensions.automapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstefh%2Ffluentvalidation.extensions.automapper/lists"}