Member-only story

How to Use Fluent Validation in .NET Core

Streamline and elevate the data validation process.

Alex Maher
3 min readDec 19, 2023

Hey everyone! Let’s talk about Fluent Validation in .NET Core. It’s a tool that helps us make sure the information in our applications is correct and makes sense. Think of it as a helpful assistant that double-checks your work.

Getting Started

Setting up Fluent Validation in your project is straightforward.

First, you need to add it to your .NET Core application. This is done by installing a package named FluentValidation.AspNetCore. You can do this using NuGet Package Manager.

Writing Your First Validation Rules

Now, let’s talk about creating validation rules. Fluent Validation allows you to set specific rules for different types of data in your application. For example, if you have a User model with an email and password, you can create rules to check these fields.

Here’s a simple way to do it:

using FluentValidation;

public class UserValidator : AbstractValidator<User>
{
public UserValidator()
{
RuleFor(user => user.Email)
.NotEmpty().WithMessage("Email is required.")
.EmailAddress().WithMessage("Please enter a valid email.")…

--

--

Alex Maher
Alex Maher

Written by Alex Maher

.NET C# dev with 10+ yrs exp, self-taught & passionate web developer. Sharing tips & experiences in C# and web dev.

No responses yet