DEV Community

Cover image for ASP .NET Core FluentValidation
Karen Payne
Karen Payne

Posted on

ASP .NET Core FluentValidation

Introduction

The best NuGet package for validating data for C# for any project type is FluentValidation. The focus here is on transitioning from the NuGet package for ASP.NET Core FluentValidation.AspNetCore, which has been deprecated, is now using two different packages, adding a language extension, if used, one or more enum members.

Tools used

  • Google deep AI using a simple prompt: fluentvalidation.aspnetcore deprecated. This is done from a standard search.
  • Google search for: AddToModelState which point to a Stackoverflow post in the first three recommendations
  • JetBrains ReSharper (made the code transition easier than without)

Source code

Scenario

A developer has a Microsoft Visual Studio solution that contains one or more projects for presenting information in an ASP.NET Core project, which uses the deprecated NuGet package FluentValidation.AspNetCore. The task is to move away from the deprecated NuGet package.

Preparation

  • Ensure current code compiles and functions
  • Perform a commit to a remote repository

🛑 From this point on, do not commit any changes until the code functions during the Migration.

Migration steps

Open each project file by clicking on the file in Microsoft Visual Studio's solution explorer, and remove the following.

<ItemGroup>
   <PackageReference Include="FluentValidation.AspNetCore" Version="11.3.1" />
</ItemGroup>
Enter fullscreen mode Exit fullscreen mode

Add the following. Next open dependency window and check for upgrades.

<ItemGroup>
   <PackageReference Include="FluentValidation" Version="12.0.0" />
   <PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="12.0.0" />
   <PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.3.0" />
</ItemGroup>
Enter fullscreen mode Exit fullscreen mode

Build

For this build, expect errors; address the easy errors first, such as invalid using statements. Depending on what has been used exclusively with FluentValidation.AspNetCore, like the following CascadeMode.StopOnFirstFailure will not exist; instead, use CascadeMode.Stop.

There may be extensions and methods that do not exists such as AddToModelState which need to have replacements as shown below.

public static void AddToModelState(this ValidationResult result, ModelStateDictionary modelState, string prefix)
{

    if (result.IsValid) return;

    foreach (var error in result.Errors)
    {
        string key = string.IsNullOrEmpty(prefix)
            ? error.PropertyName
            : string.IsNullOrEmpty(error.PropertyName)
                ? prefix
                : $"{prefix}.{error.PropertyName}";
        modelState.AddModelError(key, error.ErrorMessage);
    }
}    
Enter fullscreen mode Exit fullscreen mode

Continue fixing build errors until a clean build is achieved, followed by testing all functionality. Once satisfied, commit your changes.

Summary
What has been presented should be sufficient to perform the migration, although there may be fringe cases that require the same handling as shown here.

Top comments (0)