@@ -15,19 +15,19 @@ Requires Zod v4.
1515## Quick Start
1616
1717``` js
18- import { createSchemas } from ' @api-platform/zod'
19- import { safeParse } from ' zod/v4'
18+ import { createSchemas } from " @api-platform/zod" ;
19+ import { safeParse } from " zod/v4" ;
2020
2121// Generate schemas from an API entrypoint
22- const { schemas , collections } = await createSchemas (' https://api.example.com' )
22+ const { schemas , collections } = await createSchemas (" https://api.example.com" );
2323
2424// schemas.Book -> z.looseObject({ '@id': z.string(), '@type': z.literal('Book'), title: z.string(), ... })
2525// collections.Book -> Hydra collection schema wrapping Book
2626
2727// Validate API responses
28- const result = safeParse (schemas .Book , responseData)
28+ const result = safeParse (schemas .Book , responseData);
2929if (result .success ) {
30- console .log (result .data .title )
30+ console .log (result .data .title );
3131}
3232```
3333
@@ -38,14 +38,18 @@ if (result.success) {
3838High-level function that fetches and parses API documentation, then generates Zod schemas.
3939
4040``` js
41- const { schemas , collections , resources , api , response } = await createSchemas (' https://api.example.com' )
41+ const { schemas , collections , resources , api , response } = await createSchemas (
42+ " https://api.example.com" ,
43+ );
4244```
4345
4446** Parameters:**
47+
4548- ` entrypoint ` — The API entrypoint URL
4649- ` options ` — Options passed to ` parseHydraDocumentation `
4750
4851** Returns:**
52+
4953- ` schemas ` — ` { [ResourceName]: ZodSchema } ` for each resource
5054- ` collections ` — ` { [ResourceName]: ZodSchema } ` Hydra collection schemas
5155- ` resources ` — The parsed resource objects from api-doc-parser
@@ -57,59 +61,59 @@ const { schemas, collections, resources, api, response } = await createSchemas('
5761Lower-level function that works with pre-parsed Resource objects (from api-doc-parser). Useful when you already have the parsed API documentation.
5862
5963``` js
60- import { schemasFromResources } from ' @api-platform/zod'
64+ import { schemasFromResources } from " @api-platform/zod" ;
6165
62- const { schemas , collections } = schemasFromResources (api .resources )
66+ const { schemas , collections } = schemasFromResources (api .resources );
6367```
6468
6569### ` resourceToSchema(resource, schemaMap?) `
6670
6771Converts a single api-doc-parser Resource into a ` z.looseObject ` schema with ` @id ` , ` @type ` , and all readable fields.
6872
6973``` js
70- import { resourceToSchema } from ' @api-platform/zod'
74+ import { resourceToSchema } from " @api-platform/zod" ;
7175
72- const bookSchema = resourceToSchema (bookResource)
76+ const bookSchema = resourceToSchema (bookResource);
7377```
7478
7579### ` fieldToZod(field, schemaMap?) `
7680
7781Converts a single api-doc-parser Field into a Zod type.
7882
7983``` js
80- import { fieldToZod } from ' @api-platform/zod'
84+ import { fieldToZod } from " @api-platform/zod" ;
8185
82- const zodType = fieldToZod (field)
86+ const zodType = fieldToZod (field);
8387```
8488
8589### ` collectionSchema(itemSchema) `
8690
8791Creates a Hydra collection schema wrapping the given item schema.
8892
8993``` js
90- import { collectionSchema } from ' @api-platform/zod'
94+ import { collectionSchema } from " @api-platform/zod" ;
9195
92- const booksCollectionSchema = collectionSchema (bookSchema)
96+ const booksCollectionSchema = collectionSchema (bookSchema);
9397```
9498
9599## Type Mapping
96100
97- | Field Type | Zod Type |
98- | ---| ---|
99- | ` string ` , ` password ` , ` byte ` , ` binary ` , ` hexBinary ` , ` base64Binary ` , ` duration ` | ` z.string() ` |
100- | ` email ` | ` z.string().email() ` |
101- | ` url ` | ` z.string().url() ` |
102- | ` uuid ` | ` z.string().uuid() ` |
103- | ` integer ` | ` z.int() ` |
104- | ` positiveInteger ` | ` z.int().min(1) ` |
105- | ` negativeInteger ` | ` z.int().max(-1) ` |
106- | ` nonNegativeInteger ` | ` z.int().min(0) ` |
107- | ` nonPositiveInteger ` | ` z.int().max(0) ` |
108- | ` number ` , ` decimal ` , ` double ` , ` float ` | ` z.number() ` |
109- | ` boolean ` | ` z.boolean() ` |
110- | ` date ` | ` z.string().date() ` |
111- | ` dateTime ` | ` z.string().datetime() ` |
112- | ` time ` | ` z.string().time() ` |
101+ | Field Type | Zod Type |
102+ | ------------------------------------------------------------------------------- | ----------------------- |
103+ | ` string ` , ` password ` , ` byte ` , ` binary ` , ` hexBinary ` , ` base64Binary ` , ` duration ` | ` z.string() ` |
104+ | ` email ` | ` z.string().email() ` |
105+ | ` url ` | ` z.string().url() ` |
106+ | ` uuid ` | ` z.string().uuid() ` |
107+ | ` integer ` | ` z.int() ` |
108+ | ` positiveInteger ` | ` z.int().min(1) ` |
109+ | ` negativeInteger ` | ` z.int().max(-1) ` |
110+ | ` nonNegativeInteger ` | ` z.int().min(0) ` |
111+ | ` nonPositiveInteger ` | ` z.int().max(0) ` |
112+ | ` number ` , ` decimal ` , ` double ` , ` float ` | ` z.number() ` |
113+ | ` boolean ` | ` z.boolean() ` |
114+ | ` date ` | ` z.string().date() ` |
115+ | ` dateTime ` | ` z.string().datetime() ` |
116+ | ` time ` | ` z.string().time() ` |
113117
114118### Special Cases
115119
@@ -127,11 +131,11 @@ Schemas use `z.looseObject()`, which allows unknown properties to pass through w
127131``` js
128132// Extra fields in the response are preserved, not stripped
129133const result = safeParse (schemas .Book , {
130- ' @id' : ' /api/books/1' ,
131- ' @type' : ' Book' ,
132- title: ' The Great Gatsby' ,
133- newFieldAddedLater: ' still available' ,
134- })
134+ " @id" : " /api/books/1" ,
135+ " @type" : " Book" ,
136+ title: " The Great Gatsby" ,
137+ newFieldAddedLater: " still available" ,
138+ });
135139// result.data.newFieldAddedLater === 'still available'
136140```
137141
0 commit comments