colinhacks / zod

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

Remove createParams cascade from .array() #3530

Open EthanBehrends opened 5 months ago

EthanBehrends commented 5 months ago

See this issue for more context.

Basically, the description for a type was getting unexpectedly duplicated when using .array(). For example:

z.string().describe("a description").array()

// ^ was functionally the same as v

z.array(
  z.string().describe("a description")
).describe("a description")

The issue was not present for z.array()

z.array(z.string().describe("a description")) // works as expected

This unexpected behavior was introduced in a fix trying to ensure that something like z.string().describe("a description").optional() persisted the description after being wrapped by ZodOptional.

While this behavior makes sense for types like ZodOptional or ZodNullable, ZodArray is a completely separate type.

Other types that do this cascading createParams:

I think the logic makes sense for all of these (maybe or and and are also exceptions? not sure)

Added a test for this as well :)