REST API validation flow

Note

This topic describes validation flow of Micro.Rest

Concept

The validation itself implemented as Micro.Web middleware. Micro.Rest decorators enables this middleware for every request handler function it wraps. The validation schemas is defined with REST endpoint declaration domain language

Input validation

The validation rules for input are picked up in runtime based on request content type. There is only content type supported for now for input (and, btw, output) validation is application/json . The schema for input JSON is defined in @input tag. If the input JSON object passes validation, the framework invokes user-defined handler method. If the input JSON object`s schema differ with etalon schema, the middleware raises an exception which leads to 400 Bad Request response to the client.

Output validation

The validation rules for output can be enabled only for JSON output. The schema for output JSON is defined in @output tag. If the output JSON object passes validation, the framework will assign the status code which defined in the tag mentioned above, and return the response for processing. If the input JSON object`s schema differ with etalon schema, the middleware raises an exception which leads to 500 Internal Server Error response to the client.

Error validation

The validation rules for errors checks only the status codes of the exceptions. If the status code of exception happened during the request handling matches the status codes of the exceptions declared in the @raises tag, the client will receive the response with that status code. If the status code of exception doesn’t match any @raises definitions, or the exception doesn`t even inherit aiohttp.web_exceptions.HTTPError the user will receive 500 Internal Server Error