Skip to content

Commit 162b697

Browse files
authored
fix(zod): avoid adding redundant not to optional schemas #1593
1 parent 56e0760 commit 162b697

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

src/_vendor/zod-to-json-schema/parsers/optional.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { JsonSchema7Type, parseDef } from '../parseDef';
33
import { Refs } from '../Refs';
44

55
export const parseOptionalDef = (def: ZodOptionalDef, refs: Refs): JsonSchema7Type | undefined => {
6-
if (refs.currentPath.toString() === refs.propertyPath?.toString()) {
7-
return parseDef(def.innerType._def, refs);
6+
if (
7+
refs.propertyPath &&
8+
refs.currentPath.slice(0, refs.propertyPath.length).toString() === refs.propertyPath.toString()
9+
) {
10+
return parseDef(def.innerType._def, { ...refs, currentPath: refs.currentPath });
811
}
912

1013
const innerSchema = parseDef(def.innerType._def, {

tests/helpers/zod.test.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,11 @@ describe('zodResponseFormat', () => {
7171
"units": {
7272
"anyOf": [
7373
{
74-
"anyOf": [
75-
{
76-
"not": {},
77-
},
78-
{
79-
"enum": [
80-
"c",
81-
"f",
82-
],
83-
"type": "string",
84-
},
74+
"enum": [
75+
"c",
76+
"f",
8577
],
78+
"type": "string",
8679
},
8780
{
8881
"type": "null",

0 commit comments

Comments
 (0)