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:
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.
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:
- Installing FluentValidation
- Create a Validator
- Create a Filter
- Integrate with WebApi
1. Installing FluentValidation
To install .NET Core FluentValidation, install the package: FluentValidation.AspNetCore2. 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CreateCustomerRequest | |
{ | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public string Email { get; set; } | |
public string PhoneNumber { get; set; } | |
public class Validator : AbstractValidator<CreateCustomerRequest> | |
{ | |
public Validator() | |
{ | |
RuleFor(x => x.FirstName).NotEmpty(); | |
RuleFor(x => x.LastName).NotEmpty(); | |
RuleFor(x => x.Email).NotEmpty(); | |
RuleFor(x => x.PhoneNumber); | |
} | |
} | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ValidateModelAttribute : ActionFilterAttribute | |
{ | |
public override void OnActionExecuting(ActionExecutingContext context) | |
{ | |
if (!context.ModelState.IsValid) | |
{ | |
context.Result = new BadRequestObjectResult(context.ModelState); | |
} | |
} | |
} |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services | |
.AddMvc(opt => | |
{ | |
opt.Filters.Add(new ValidateModelAttribute()); | |
}) | |
.AddFluentValidation(fvc => fvc.RegisterValidatorsFromAssemblyContaining<Startup>()) | |
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1); | |
} |
Conclusion
Now you've setup request validation and you'll be able to create validator classes as needed and will be applied automatically.
Comments
Post a Comment