Skip to content

Mutation of ctx param is unintuitive #5466

@aaroncowie

Description

@aaroncowie

Lines such as this in the zod codebase result in mutation of the passed context object.
const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" as const }) : { direction: "backward" as const };

This causes weird issues if the same context object is used in multiple calls.

E.g.

const myOptions = { reportInput: true } as const;
z.decode(stringToDateCodec, someDateString, myOptions);
z.decode(stringToDateCodec, someOtherDateString, myOptions);

In my particular case it was causing a later decode call to run in reverse which passed a string to the encode function instead of a date.

The reason I am sharing an options object is because i'm trying to mimic global reportInput: true by simply passing it explicity in each call.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions