You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Enhance users API with detailed response schemas and OpenAPI documentation
- Added response schemas for user operations including success and error responses.
- Updated existing routes to include OpenAPI schema definitions for better documentation.
- Implemented user statistics retrieval and role-based user listing with appropriate schemas.
- Improved error handling and response consistency across user-related endpoints.
@@ -4,7 +4,7 @@ This document explains how to generate and use the OpenAPI specification for the
4
4
5
5
## Overview
6
6
7
-
The DeployStack Backend uses Fastify with Swagger plugins to automatically generate OpenAPI 3.0 specifications from route definitions. This provides:
7
+
The DeployStack Backend uses Fastify with Swagger plugins to automatically generate OpenAPI 3.0 specifications. Route schemas are defined using [Zod](https://zod.dev/) for type safety and expressiveness, and then converted to JSON Schema using the [zod-to-json-schema](https://www.npmjs.com/package/zod-to-json-schema) library. This provides:
8
8
9
9
-**Interactive Documentation**: Swagger UI interface for testing APIs
10
10
-**Postman Integration**: JSON/YAML specs that can be imported into Postman
@@ -86,31 +86,145 @@ When the server is running (`npm run dev`), you can access:
86
86
87
87
## Adding Documentation to Routes
88
88
89
-
To add OpenAPI documentation to your routes, include a schema object:
89
+
To add OpenAPI documentation to your routes, define your request body and response schemas using Zod. Then, use the `zodToJsonSchema` utility to convert these Zod schemas into the JSON Schema format expected by Fastify.
90
+
91
+
Make sure you have `zod` and `zod-to-json-schema` installed in your backend service.
92
+
93
+
### Recommended Approach: Automatic Validation with Zod
94
+
95
+
The power of Zod lies in providing **automatic validation** through Fastify's schema system. This approach eliminates manual validation and leverages Zod's full validation capabilities.
If validation fails, Fastify automatically returns a 400 error **before** your handler runs.
217
+
218
+
### Real-World Examples
219
+
220
+
See these files for complete examples of proper Zod validation:
221
+
-`src/routes/db/setup.ts` - Database setup with enum validation
222
+
-`src/routes/db/status.ts` - Simple GET endpoint with response schemas
223
+
-`src/routes/auth/loginEmail.ts` - Login with required string fields
224
+
-`src/routes/auth/registerEmail.ts` - Registration with complex validation rules
225
+
226
+
**Note**: Older examples in this document (like the "Logout Route Documentation" below) might still show manually crafted JSON schemas. The recommended approach is now to use Zod with automatic Fastify validation as shown above.
227
+
114
228
## Example: Logout Route Documentation
115
229
116
230
The logout route (`/api/auth/logout`) demonstrates proper documentation:
0 commit comments