Skip to content

Commit

Permalink
Merge pull request #279 from IlmariKu/patch-1
Browse files Browse the repository at this point in the history
Example on how to add the openapi-extension to your code
  • Loading branch information
AGalabov authored Dec 6, 2024
2 parents 81f003d + 8ea3a86 commit fa0cfdb
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ It has three overloads:

For this to work, you need to call `extendZodWithOpenApi` once in your project.

Note: This should be done only once in a common-entrypoint file of your project (for example an `index.ts`/`app.ts`). If you're using tree-shaking with Webpack, mark that file as having side-effects.
This should be done only once in a common-entrypoint file of your project (for example an `index.ts`/`app.ts`). If you're using tree-shaking with Webpack, mark that file as having side-effects.

It can be bit tricky to achieve this in your codebase, because *require* is synchronous and *import* is a async.

### The basic idea

```ts
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
Expand All @@ -135,6 +139,35 @@ extendZodWithOpenApi(z);
z.string().openapi({ description: 'Some string' });
```
### Example 1: Calling the openapi-extension using tsx
```
//zod-extend.ts

import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
import { z } from 'zod';

extendZodWithOpenApi(z);

// package.json

"scripts": {
"start": "tsx --import ./zod-extend.ts ./index.ts",
```
### Example 2 - require-syntax
```
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
import { z } from 'zod';

extendZodWithOpenApi(z);

const { startServer } = require('./server/start');
startServer();
```


### The Registry

The `OpenAPIRegistry` is a utility that can be used to collect definitions which would later be passed to a `OpenApiGeneratorV3` or `OpenApiGeneratorV31` instance.
Expand Down

0 comments on commit fa0cfdb

Please sign in to comment.