Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 78 additions & 43 deletions src/zod/openapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,107 @@ label: OpenAPI
---

::: danger
The SwaggerModule is currently in Alpha, maaaany features are missing. If something you need is not here yet, [please fill an issue/feature request](https://github.com/Savory/Danet-Swagger/issues)
This page assumes that you are familiar with are familiar with Danet's controllers. If you are not, please read the [Controllers](/overview/controllers) page first.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix duplicated wording in the intro warning.

There’s a typo: “familiar with are familiar with”.

Suggested fix
-This page assumes that you are familiar with are familiar with Danet's controllers. If you are not, please read the [Controllers](/overview/controllers) page first.
+This page assumes that you are familiar with Danet's controllers. If you are not, please read the [Controllers](/overview/controllers) page first.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
This page assumes that you are familiar with are familiar with Danet's controllers. If you are not, please read the [Controllers](/overview/controllers) page first.
This page assumes that you are familiar with Danet's controllers. If you are not, please read the [Controllers](/overview/controllers) page first.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/zod/openapi.md` at line 7, The intro warning contains duplicated wording
"familiar with are familiar with"; update the sentence in src/zod/openapi.md
(the intro warning line) to read cleanly, e.g. "This page assumes that you are
familiar with Danet's controllers. If you are not, please read the
[Controllers](/overview/controllers) page first." — remove the extra "are
familiar with" and ensure spacing and punctuation are correct.

:::


## Pre-requiresites
## Body, Query

Zod integration in Danet is very straightforward. First, you need to define your schemas using Zod. Then, you can use the `@Body()` and `@Query()` decorators from `@danet/zod` to validate the request body and query parameters.

First, if you don't know how to use Danet's swagger module first read the dedicated page [here](/openapi/introduction).
Second, in order to generate openAPI definition from zod, you will need to extend zod using [zod-to-openapi](https://www.npmjs.com/package/@anatine/zod-openapi). As follows:
For example:

```ts
import { extendZodWithOpenApi } from 'zod-openapi';

extendZodWithOpenApi(z);
```
import { Controller, Post } from '@danet/core';
import { Body } from '@danet/zod';

Danet Swagger module uses the following versions:
const CreateTodoSchema = z.object({
title: z.string(),
description: z.string(),
});

```
"zod": "npm:zod@3.23.8",
"zod-openapi": "npm:@anatine/zod-openapi@2.2.6"
```
type CreateTodoSchema = z.infer<typeof CreateTodoSchema>;
Comment on lines +19 to +27
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add missing z import in both TypeScript examples.

Both snippets use z.object / z.infer but don’t import z, so the examples aren’t copy-paste runnable.

Suggested fix
 import { Controller, Post } from '@danet/core';
 import { Body } from '@danet/zod';
+import { z } from 'zod';
 import { Controller, Get } from '@danet/core';
 import { Query } from '@danet/zod';
+import { z } from 'zod';

Also applies to: 45-53

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/zod/openapi.md` around lines 19 - 27, The examples use z.object and
z.infer but never import z, so add the missing import (e.g., "import { z } from
'zod';") at the top of each TypeScript snippet that defines CreateTodoSchema
(the snippets referencing z.object and type CreateTodoSchema = z.infer<...>) so
the examples are copy-paste runnable; apply the same change to the second
example as noted (lines showing the same CreateTodoSchema usage).


Last, you will need to add an openApi title attribute to your zod schema as follows, so we know how to name the model in the openAPI definition:
```ts
const Cat = z.object({
name: z.string(),
breed: z.string(),
dob: z.date(),
isHungry: z.boolean().optional(),
hobbies: z.array(z.any())
}).openapi({
title: 'Cat'
})
@Controller('todos')
export class TodosController {
constructor(private readonly todoService: TodoService) {}

@Post()
async create(@Body(CreateTodoSchema) createTodoDto: CreateTodoSchema) {
this.todoService.create(createTodoDto);
}
}
```

## Body, Query
will automatically validate the request body against the `CreateTodoSchema` schema.

The `SwaggerModule` searches for all `@Body()` and `@Query()` from `@danet/zod` decorators in route handlers to generate the API document. It also creates corresponding model definitions . Consider the following code:
You can also use the `@Query()` decorator to validate query parameters:

```ts
@Post()
async create(@Body(CreateTodoSchema) createTodoDto: CreateTodoSchema) {
this.todoService.create(createTodoDto);
import { Controller, Get } from '@danet/core';
import { Query } from '@danet/zod';

const GetTodoQuery = z.object({
id: z.string(),
});

type GetTodoQuery = z.infer<typeof GetTodoQuery>;

@Controller('todos')
export class TodosController {
constructor(private readonly todoService: TodoService) {}

@Get(':id')
async getById(@Query(GetTodoQuery) query: GetTodoQuery) {
return this.todoService.getById(query.id);
}
}
```

## Returned schema
::: tip
Using these decorators will also allow you to generate OpenAPI documentation for your routes. More information on this can be found in the [OpenAPI](/zod/openapi) section.
:::

You can use the `@ReturnedSchema` decorator to say what your endpoint will return :

# ZOD Version 4 Support

Danet now supports Zod version 4 `@danet/zod@^0.1.0`, which is a major upgrade from version 3.

::: note
All previous versions of `@danet/zod` support only Zod version 3.
:::

One of the new features is the ability to generate JSON Schema from Zod schemas and vice versa.

### z.fromJSONSchema()
Comment on lines +70 to +80
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix heading-level jump to satisfy markdown lint.

Line 80 jumps to ### right after a # heading, which violates MD001.

Suggested fix
-### z.fromJSONSchema()
+## z.fromJSONSchema()

(And keep ## z.toJSONSchema() at Line 97, which then matches correctly.)

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# ZOD Version 4 Support
Danet now supports Zod version 4 `@danet/zod@^0.1.0`, which is a major upgrade from version 3.
::: note
All previous versions of `@danet/zod` support only Zod version 3.
:::
One of the new features is the ability to generate JSON Schema from Zod schemas and vice versa.
### z.fromJSONSchema()
# ZOD Version 4 Support
Danet now supports Zod version 4 `@danet/zod@^0.1.0`, which is a major upgrade from version 3.
::: note
All previous versions of `@danet/zod` support only Zod version 3.
:::
One of the new features is the ability to generate JSON Schema from Zod schemas and vice versa.
## z.fromJSONSchema()
🧰 Tools
🪛 markdownlint-cli2 (0.22.0)

[warning] 80-80: Heading levels should only increment by one level at a time
Expected: h2; Actual: h3

(MD001, heading-increment)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/zod/openapi.md` around lines 70 - 80, The heading level jumps from "# ZOD
Version 4 Support" to "### z.fromJSONSchema()", causing an MD001 lint error;
change the "### z.fromJSONSchema()" heading to "## z.fromJSONSchema()" so it is
a second-level heading like "## z.toJSONSchema()", ensuring consistent heading
hierarchy and keeping "## z.toJSONSchema()" unchanged.


```ts
@ReturnedSchema(TodoSchema)
@Get(':id')
async getById(@Param('id') id: string): Todo {
return this.todoService.getById(id);
}
import { z } from 'zod';

const jsonSchema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
},
required: ['name', 'age']
};

const zodSchema = z.fromJSONSchema(jsonSchema);
```

If your route returns an array, pass `true` as the second argument of `ReturnedSchema` :

### z.toJSONSchema()

```ts
@ReturnedSchema(TodoSchema, true)
@Get()
async getTodos(): Todo[] {
return this.todoService.getAll();
}
```
import { z } from 'zod';

const zodSchema = z.object({
name: z.string(),
age: z.number()
});

const jsonSchema = z.toJSONSchema(zodSchema);
```
Comment on lines +72 to +108
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🌐 Web query:

For Zod v4, are z.fromJSONSchema()andz.toJSONSchema()official public APIs? Also verify whether@danet/zod@^0.1.0 is documented as the version that adds Zod v4 support.

💡 Result:

For Zod v4, z.toJSONSchema is an official public API, introduced as a stable first-party feature in the release notes and documented in the JSON Schema guide. z.fromJSONSchema is also an official API but marked as experimental and not part of the stable API, likely to change in future releases, as stated in the official JSON Schema documentation. @danet/zod@^0.1.0 is not documented as the version adding Zod v4 support. The latest version is 0.0.9 (published ~5 months ago), and Danet documentation specifies compatibility with Zod 3.23.8. No changelog, release notes, or documentation mention Zod v4 support in any version, including around 0.1.0.

Citations:


Remove or correct section claiming Zod v4 support—it does not exist in @danet/zod.

The documentation claims @danet/zod@^0.1.0 adds Zod v4 support, but the latest published version is 0.0.9 (released ~5 months ago) and only supports Zod 3.23.8. No version of @danet/zod supports Zod v4. Additionally, z.fromJSONSchema() is marked experimental in Zod v4 and not part of the stable API. The code examples shown cannot execute with the current package. Either remove this section or update it to reflect actual Zod 3 capabilities.

🧰 Tools
🪛 markdownlint-cli2 (0.22.0)

[warning] 80-80: Heading levels should only increment by one level at a time
Expected: h2; Actual: h3

(MD001, heading-increment)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/zod/openapi.md` around lines 72 - 108, The docs incorrectly claim
`@danet/zod`@^0.1.0 (and thus Zod v4) is supported and show examples for
z.fromJSONSchema() / z.toJSONSchema(); update the section to reflect reality by
removing or correcting the Zod v4 claim and the non-working examples: state that
current `@danet/zod` (latest 0.0.9) targets Zod v3 (≈3.23.8), remove or replace
the z.fromJSONSchema() example (since fromJSONSchema is not available in Zod v3
and is experimental in Zod v4), and either remove the z.toJSONSchema() example
or rewrite it to demonstrate only features supported by Zod v3; also remove the
version string `@danet/zod`@^0.1.0 or change it to the actual released version and
add a short note indicating that Zod v4 support is not yet available.


For more information on Zod's JSON Schema: https://zod.dev/json-schema.