Skip to content

Commit

Permalink
bump zod to 3.22 and add support for ZodReadonly
Browse files Browse the repository at this point in the history
  • Loading branch information
kheyse-werk committed Dec 13, 2023
1 parent cbea74d commit f143c27
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
21 changes: 11 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@
"tslib": "^2.6.0",
"typescript": "5.1.6",
"validator": "^13.9.0",
"zod": "^3.21.4"
"zod": "^3.22.4"
},
"workspaces": [
"packages/**"
]
}

31 changes: 31 additions & 0 deletions packages/zod-openapi/src/lib/zod-openapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,4 +961,35 @@ describe('zodOpenapi', () => {
maximum: 10,
} satisfies SchemaObject);
});
test('should work with ZodReadonly', () => {
expect(generateSchema(z.object({ field: z.string() })))
.toMatchInlineSnapshot(`
Object {
"properties": Object {
"field": Object {
"type": "string",
},
},
"required": Array [
"field",
],
"type": "object",
}
`);

expect(generateSchema(z.object({ field: z.string() }).readonly()))
.toMatchInlineSnapshot(`
Object {
"properties": Object {
"field": Object {
"type": "string",
},
},
"required": Array [
"field",
],
"type": "object",
}
`);
});
});
13 changes: 13 additions & 0 deletions packages/zod-openapi/src/lib/zod-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,18 @@ function parsePipeline({
return generateSchema(zodRef._def.in, useOutput);
}

function parseReadonly({
zodRef,
useOutput,
schemas,
}: ParsingArgs<z.ZodReadonly<z.ZodAny>>): SchemaObject {
return merge(
generateSchema(zodRef._def.innerType, useOutput),
zodRef.description ? { description: zodRef.description } : {},
...schemas
);
}

const workerMap = {
ZodObject: parseObject,
ZodRecord: parseRecord,
Expand Down Expand Up @@ -556,6 +568,7 @@ const workerMap = {
ZodUnknown: catchAllParser,
ZodVoid: catchAllParser,
ZodPipeline: parsePipeline,
ZodReadonly: parseReadonly,
};
type WorkerKeys = keyof typeof workerMap;

Expand Down

0 comments on commit f143c27

Please sign in to comment.