Request Body & Responses with Type Validation
Request bodies and responses are essential for defining the structure and validation of the data exchanged in your API. The Ascender Framework leverages Pydantic models to provide type safety, validation, and seamless integration with FastAPI's request and response handling.
What Are DTOs and Responses?
- DTOs (Data Transfer Objects): These models define the structure of incoming request bodies, ensuring that the data sent by clients is validated and adheres to the expected schema.
- Responses: These models define the structure of outgoing data, ensuring consistent responses across your API.
Ascender Framework simplifies their usage with base classes:
BaseDTO: The base class for request body models.BaseResponse: The base class for response models, optional but useful for nesting data or complex response structures.
Naming conventions are recommended but not strictly required:
- DTOs:
[Name]DTO - Responses:
[Name]Response
Defining DTOs
DTOs inherit from BaseDTO and specify the expected structure of the request body. Validation is automatically handled by Pydantic.
Here's how it might look like in example with CreateUserDTO:
CreateUserDTO ensures that any incoming request to create a user includes name and email fields as strings.
Defining Responses
Responses inherit from BaseResponse and define the structure of the API's responses. This is particularly useful for ensuring consistent output and handling nested or complex data.
UserResponse ensures that the API consistently returns user data with id, name, and email fields.
Using DTOs and Responses in Controllers
DTOs and responses are integrated directly into your controllers to handle request validation and response serialization.
create_user endpoint:
- Validates the incoming request body using
CreateUserDTO. - Returns a structured response using
UserResponse.
When to Use BaseResponse
While BaseResponse is optional, it is highly recommended for complex APIs requiring:
- Nested data structures.
- Consistent response formats.
For example:
Summary
- Use DTOs (
BaseDTO) for request bodies to validate incoming data. - Use Responses (
BaseResponse) for structured and consistent API responses. - Follow naming conventions (
[Name]DTO,[Name]Response) to maintain clarity and separation of concerns.