colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.98k stars 1.19k forks source link

Descriptions from root schema carry over to schemas created with a chained `.array()` #3529

Open EthanBehrends opened 5 months ago

EthanBehrends commented 5 months ago

Related to changes made in #1756.

The above PR addresses an issue where descriptions would not carry over to schemas created using the chaining helper methods, like .optional().

// Before the above changes
z.string().describe("a description").description // "a description"
z.string().describe("a description").optional().description // undefined

// After
z.string().describe("a description").description // "a description"
z.string().describe("a description").optional().description // "a description"

The changes to fix this issue were also applied to .array(), which results in some unexpected behavior.

// With this bug,
z.string().describe("a description").array()
// is equivalent to:
z.string().describe("a description").array().describe("a description")
// but this works as expected
z.array(z.string().describe("a description"))

The fix was super simple, I'll open a PR shortly!