Skip to content

Validation

moattarwork edited this page Dec 14, 2022 · 1 revision

Validation is an integral part of any high-quality API. However, this is another example of a plumbing code that we write over and over again. Wouldn't it be nice to have this done for you? And this is why we have chosen to use FluentValidation for our validation needs. To use this framework we need to define validators for the objects we want to validate. Please refer to the FluentValidation Documentation on how to do this, however assuming we have those validators defined, they will be executed upon API resource is requested. Incase validation errors occur, they will be appended to ModelState errors collection and returned to the user with a StatusCode 4xx message. If you want to know how the client errors flow through the API, please refer to Exception Handling section.

Validation Action Filters

Validation also has a preconfigured ActionFilter which intercepts every call to the ANY app controller and makes sure every parameter is supplied. Parameters with NULL values are treated as errors and will produce a StatusCode 4xx response. While this may or may not be the behavior you may want, the thinking behind this choice is that NULL/Optional parameters create confusion and ambiguity, which results in hard to track down API failures. Therefore this is a discouraged practice and we simply DO NOT ALLOW such option.

Clone this wiki locally