zod-openapi 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -346,11 +346,40 @@ const header = z.string().openapi({
|
|
|
346
346
|
|
|
347
347
|
## Supported OpenAPI Versions
|
|
348
348
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
-
|
|
352
|
-
-
|
|
353
|
-
-
|
|
349
|
+
Currently the following versions of OpenAPI are supported
|
|
350
|
+
|
|
351
|
+
- `3.0.0`
|
|
352
|
+
- `3.0.1`
|
|
353
|
+
- `3.0.2`
|
|
354
|
+
- `3.0.3`
|
|
355
|
+
- `3.1.0`
|
|
356
|
+
|
|
357
|
+
Setting the `openapi` field will change how the some of the components are rendered.
|
|
358
|
+
|
|
359
|
+
```ts
|
|
360
|
+
createDocument({
|
|
361
|
+
openapi: '3.1.0',
|
|
362
|
+
});
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
For example in `z.string().nullable()` will be rendered differently
|
|
366
|
+
|
|
367
|
+
`3.0.0`
|
|
368
|
+
|
|
369
|
+
```json
|
|
370
|
+
{
|
|
371
|
+
"type": "string",
|
|
372
|
+
"nullable": true
|
|
373
|
+
}
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
`3.1.0`
|
|
377
|
+
|
|
378
|
+
```json
|
|
379
|
+
{
|
|
380
|
+
"type": ["string", "null"]
|
|
381
|
+
}
|
|
382
|
+
```
|
|
354
383
|
|
|
355
384
|
## Supported Zod Schema
|
|
356
385
|
|
|
@@ -386,6 +415,7 @@ const header = z.string().openapi({
|
|
|
386
415
|
- `pattern` mapping for `.regex()`
|
|
387
416
|
- ZodTuple
|
|
388
417
|
- `items` mapping for `.rest()`
|
|
418
|
+
- `prefixItems` mapping for OpenAPI 3.1.0+
|
|
389
419
|
- ZodUnion
|
|
390
420
|
|
|
391
421
|
If this library cannot determine a type for a Zod Schema, it will throw an error. To avoid this, declare a manual `type` in the `.openapi()` section of that schema.
|
|
@@ -447,4 +477,85 @@ To release a new beta version
|
|
|
447
477
|
|
|
448
478
|
## Credits
|
|
449
479
|
|
|
450
|
-
|
|
480
|
+
### [@asteasolutions/zod-to-openapi](https://github.com/asteasolutions/zod-to-openapi)
|
|
481
|
+
|
|
482
|
+
zod-openapi was created while trying to re-write the wonderful library to support auto registering schemas. However, the underlying structure of the library which consists of tightly coupled classes would not allow for this be done easily. As a result zod-openapi was born with the goal of keeping the Zod Schemas independent from the generation of the documentation.
|
|
483
|
+
|
|
484
|
+
#### Migration
|
|
485
|
+
|
|
486
|
+
1. Delete the OpenAPIRegistry and OpenAPIGenerator classes
|
|
487
|
+
2. Replace any `.register()` call made and replace them with `ref` in `.openapi()`.
|
|
488
|
+
|
|
489
|
+
```ts
|
|
490
|
+
const registry = new OpenAPIRegistry();
|
|
491
|
+
|
|
492
|
+
const foo = registry.register(
|
|
493
|
+
'foo',
|
|
494
|
+
z.string().openapi({ description: 'foo' }),
|
|
495
|
+
);
|
|
496
|
+
const bar = z.object({ foo });
|
|
497
|
+
|
|
498
|
+
// Replace with:
|
|
499
|
+
const foo = z.string().openapi({ ref: 'foo', description: 'foo' });
|
|
500
|
+
const bar = z.object({ foo });
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
3. Replace `registry.registerComponent()` with a regular OpenAPI component in the document.
|
|
504
|
+
|
|
505
|
+
```ts
|
|
506
|
+
const registry = new OpenAPIRegistry();
|
|
507
|
+
|
|
508
|
+
registry.registerComponent('securitySchemes', 'auth', {
|
|
509
|
+
type: 'http',
|
|
510
|
+
scheme: 'bearer',
|
|
511
|
+
bearerFormat: 'JWT',
|
|
512
|
+
description: 'An auth token issued by oauth",
|
|
513
|
+
});
|
|
514
|
+
// Replace with regular component declaration
|
|
515
|
+
|
|
516
|
+
const document = createDocument({
|
|
517
|
+
components: {
|
|
518
|
+
securitySchemes: { // declare directly in components
|
|
519
|
+
auth: {
|
|
520
|
+
type: 'http',
|
|
521
|
+
scheme: 'bearer',
|
|
522
|
+
bearerFormat: 'JWT',
|
|
523
|
+
description: 'An auth token issued by oauth",
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
4. Replace `registry.registerPath()` with a regular OpenAPI paths in the document.
|
|
531
|
+
|
|
532
|
+
```ts
|
|
533
|
+
const registry = new OpenAPIRegistry();
|
|
534
|
+
|
|
535
|
+
registry.registerPath({
|
|
536
|
+
method: 'get',
|
|
537
|
+
path: '/foo',
|
|
538
|
+
request: {
|
|
539
|
+
query: z.object({ a: z.string() }),
|
|
540
|
+
params: z.object({ b: z.string() }),
|
|
541
|
+
},
|
|
542
|
+
responses: {},
|
|
543
|
+
});
|
|
544
|
+
// Replace with regular path declaration
|
|
545
|
+
|
|
546
|
+
const getFoo: ZodOpenApiPathItemObject = {
|
|
547
|
+
get: {
|
|
548
|
+
requestParams: {
|
|
549
|
+
query: z.object({ a: z.string() }),
|
|
550
|
+
path: z.object({ b: z.string() }), // renamed params -> path
|
|
551
|
+
}, // renamed from request -> requestParams
|
|
552
|
+
responses: {},
|
|
553
|
+
},
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
const document = createDocument({
|
|
557
|
+
paths: {
|
|
558
|
+
'/foo': getFoo,
|
|
559
|
+
},
|
|
560
|
+
});
|
|
561
|
+
```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content.js","sourceRoot":"","sources":["../../src/create/content.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"content.js","sourceRoot":"","sources":["../../src/create/content.ts"],"names":[],"mappings":";;;AACA,6BAA8B;AAI9B,qCAA6C;AAE7C,MAAM,qBAAqB,GAAG,CAC5B,YAIa,EACb,UAA4B,EAC5B,IAAkB,EACsC,EAAE;IAC1D,IAAI,CAAC,YAAY,EAAE;QACjB,OAAO,SAAS,CAAC;KAClB;IAED,IAAI,CAAC,CAAC,YAAY,YAAY,aAAO,CAAC,EAAE;QACtC,OAAO,YAAY,CAAC;KACrB;IAED,OAAO,IAAA,0BAAiB,EAAC,YAAY,EAAE;QACrC,UAAU;QACV,IAAI;KACL,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,qBAAqB,GAAG,CAC5B,eAAsD,EACtD,UAA4B,EAC5B,IAAkB,EACiB,EAAE;IACrC,IAAI,CAAC,eAAe,EAAE;QACpB,OAAO,SAAS,CAAC;KAClB;IAED,OAAO;QACL,GAAG,eAAe;QAClB,MAAM,EAAE,qBAAqB,CAAC,eAAe,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,CAAC;KACxE,CAAC;AACJ,CAAC,CAAC;AAEK,MAAM,aAAa,GAAG,CAC3B,aAAsC,EACtC,UAA4B,EAC5B,IAAkB,EACG,EAAE,CACvB,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,MAAM,CAClC,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE,yBAAyB,CAAC,EAAuB,EAAE;IAC9D,MAAM,eAAe,GAAG,qBAAqB,CAC3C,yBAAyB,EACzB,UAAU,EACV,IAAI,CACL,CAAC;IAEF,IAAI,eAAe,EAAE;QACnB,GAAG,CAAC,IAAI,CAAC,GAAG,eAAe,CAAC;KAC7B;IACD,OAAO,GAAG,CAAC;AACb,CAAC,EACD,EAAE,CACH,CAAC;AAnBS,QAAA,aAAa,iBAmBtB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content.js","sourceRoot":"","sources":["../../src/create/content.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"content.js","sourceRoot":"","sources":["../../src/create/content.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAI9B,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAE7C,MAAM,qBAAqB,GAAG,CAC5B,YAIa,EACb,UAA4B,EAC5B,IAAkB,EACsC,EAAE;IAC1D,IAAI,CAAC,YAAY,EAAE;QACjB,OAAO,SAAS,CAAC;KAClB;IAED,IAAI,CAAC,CAAC,YAAY,YAAY,OAAO,CAAC,EAAE;QACtC,OAAO,YAAY,CAAC;KACrB;IAED,OAAO,iBAAiB,CAAC,YAAY,EAAE;QACrC,UAAU;QACV,IAAI;KACL,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,qBAAqB,GAAG,CAC5B,eAAsD,EACtD,UAA4B,EAC5B,IAAkB,EACiB,EAAE;IACrC,IAAI,CAAC,eAAe,EAAE;QACpB,OAAO,SAAS,CAAC;KAClB;IAED,OAAO;QACL,GAAG,eAAe;QAClB,MAAM,EAAE,qBAAqB,CAAC,eAAe,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,CAAC;KACxE,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,aAAsC,EACtC,UAA4B,EAC5B,IAAkB,EACG,EAAE,CACvB,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,MAAM,CAClC,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE,yBAAyB,CAAC,EAAuB,EAAE;IAC9D,MAAM,eAAe,GAAG,qBAAqB,CAC3C,yBAAyB,EACzB,UAAU,EACV,IAAI,CACL,CAAC;IAEF,IAAI,eAAe,EAAE;QACnB,GAAG,CAAC,IAAI,CAAC,GAAG,eAAe,CAAC;KAC7B;IACD,OAAO,GAAG,CAAC;AACb,CAAC,EACD,EAAE,CACH,CAAC"}
|
|
@@ -3,7 +3,7 @@ import { stringify } from 'yaml';
|
|
|
3
3
|
import { AnyZodObject, ZodType } from 'zod';
|
|
4
4
|
import { OpenApiVersion } from '../openapi';
|
|
5
5
|
export interface ZodOpenApiMediaTypeObject extends Omit<oas31.MediaTypeObject & oas30.MediaTypeObject, 'schema'> {
|
|
6
|
-
schema?:
|
|
6
|
+
schema?: ZodType | oas31.SchemaObject | oas31.ReferenceObject;
|
|
7
7
|
}
|
|
8
8
|
export interface ZodOpenApiContentObject {
|
|
9
9
|
'application/json'?: ZodOpenApiMediaTypeObject;
|