Skip to content

zod/v4: should be using .encode() for serializerCompiler #211

@racinette

Description

@racinette

Now that codecs were introduced, it's only logical to support the bidi decode <-> encode semantics. The serializer compiler implementation should rather look like this:

app.setSerializerCompiler(({ schema, method, url }) => (data) => {
      if (!(schema instanceof z.ZodType)) {
        throw new Error(`${method} ${url} has no zod schema: ${JSON.stringify(schema)}`);
      }
      const encoded = schema.encode(data);
      return JSON.stringify(encoded);
    });

Except .transform calls will throw with this approach, since transform is a decode-only operation.

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