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
@@ -9,7 +9,7 @@ This document explains how to generate and use the OpenAPI specification for the
9
9
10
10
## Overview
11
11
12
-
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:
12
+
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-openapi](https://www.npmjs.com/package/zod-openapi) library. This provides:
13
13
14
14
-**Interactive Documentation**: Swagger UI interface for testing APIs
15
15
-**Postman Integration**: JSON/YAML specs that can be imported into Postman
@@ -171,7 +171,7 @@ Each route file should follow this pattern:
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.
321
+
To add OpenAPI documentation to your routes, define your request body and response schemas using Zod. Then, use the `createSchema` utility to convert these Zod schemas into the JSON Schema format expected by Fastify.
325
322
326
-
Make sure you have `zod` and `zod-to-json-schema` installed in your backend service.
323
+
Make sure you have `zod` and `zod-openapi` installed in your backend service.
327
324
328
-
### Recommended Approach: Automatic Validation with Zod
325
+
### Recommended Approach: Dual-Schema Pattern for Validation + Documentation
329
326
330
-
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.
327
+
**IMPORTANT**: After the Zod v4 migration, we use a **dual-schema approach**to ensure both proper Fastify validation and accurate OpenAPI documentation.
error: z.string().describe("Error message detailing what went wrong")
352
349
});
353
350
354
-
// 2. Construct the Fastify route schema using zodToJsonSchema
351
+
// 2. Construct the Fastify route schema using DUAL-SCHEMA PATTERN
355
352
const routeSchema = {
356
353
tags: ['Category'], // Your API category
357
354
summary: 'Brief description of your endpoint',
358
355
description: 'Detailed description of what this endpoint does, its parameters, and expected outcomes. Requires Content-Type: application/json header when sending request body.',
359
356
security: [{ cookieAuth: [] }], // Include if authentication is required
360
-
body: zodToJsonSchema(myRequestBodySchema, {
361
-
$refStrategy: 'none', // Keeps definitions inline, often simpler for Fastify
362
-
target: 'openApi3'// Ensures compatibility with OpenAPI 3.0
363
-
}),
357
+
358
+
// ✅ CRITICAL: Use plain JSON Schema for Fastify validation
The Fastify server is configured with custom AJV options to ensure compatibility with `zod-openapi` schema generation. This configuration is in `src/server.ts`:
598
+
599
+
```typescript
600
+
const server =fastify({
601
+
logger: loggerConfig,
602
+
disableRequestLogging: true,
603
+
ajv: {
604
+
customOptions: {
605
+
strict: false, // Allows unknown keywords in schemas
606
+
strictTypes: false, // Disables strict type checking
-**`strict: false`**: AJV v8+ runs in strict mode by default, which rejects schemas containing unknown keywords. The `zod-openapi` library generates schemas that may include keywords AJV doesn't recognize in strict mode.
616
+
-**`strictTypes: false`**: Prevents strict type validation errors that can occur with complex Zod schemas.
617
+
-**`strictTuples: false`**: Allows more flexible tuple handling for array schemas.
618
+
619
+
**Important**: These settings don't affect validation behavior - they only allow the schema compilation to succeed. All validation rules defined in your Zod schemas still work exactly as expected.
620
+
621
+
### Swagger Configuration
622
+
623
+
The Swagger documentation configuration is also in `src/server.ts`:
0 commit comments