Template WebApi: Request Validation

A part of the Template WebAPI series.

Request validation can be a powerful tool for enforcing correct usage of your service. By setting up requirements for usage beforehand, you can prevent errors from occurring within your service that may create side effects.

For our implementation, we'll be using FluentValidation, a library for strongly-typed validation rules.

In this post, we'll go over:
  1. Installing FluentValidation
  2. Create a Validator
  3. Create a Filter
  4. Integrate with WebApi

1. Installing FluentValidation

To install .NET Core FluentValidation, install the package: FluentValidation.AspNetCore

2. Creating a Validator

A validator is a class that you create in order to define the rules for how you should validate your object.

We define a CreateCustomerRequest which contains some basic properties. Then we define our validation rules by creating an inner class Validator. Going with this inner class instead of a separate file is to group like code together, reducing the need to switch files. It makes sense that the validation rules should be grouped with their properties they are validating against.

3. Creating a Filter

Next we'll need to the mechanism that tells WebApi that on each request, apply the validation. We'll use the Filter component of ASP.NET to achieve this. Filters are handlers that act on incoming requests, which can be define on the Endpoint level, Controller level, or Globally across all controllers. Think of filters as a pipeline of actions that can occur when a request is being handled.

4. Integrating with WebApi

We'll need to do do a couple things in order for requests to start becoming validating. First we'll need to register the validators with the dependency injection container, and then we'll need to add our new filter to the request pipeline. We do this in the ConfigureServices step of Startup.cs

Conclusion

Now you've setup request validation and you'll be able to create validator classes as needed and will be applied automatically.

Comments

Popular posts from this blog

Uncle Bob's Clean Architecture

C4 Model: Describing Software Architecture

Multi-Tenant Design: The Basics