Skip to content

Custom registries cannot support callbacks with schemas as arguments #5375

@florianmartens

Description

@florianmartens

Maybe I'm doing something wrong here but I'm trying to store a callback function in a metadata library but getting pretty nasty type errors. The error happens when the argument to my callback contains a ZodType.

Here's a minimal example:

import z, { ZodType } from "zod";

type Context = {
  schema: ZodType;
};

type MetadataObject = {
  a: string;
  b: "a" | "b";
};

export const registry = z.registry<
  MetadataObject | ((_validationContext: Context) => MetadataObject)
>();

function _text({ context, schema }: { context: Context; schema: ZodType }) {
  const found = registry.get(schema);
  const _metadata =
    found && typeof found === "function"
      ? found(context) // <- Error
      : found
        ? found
        : undefined;
}

Either it's something in my setup or drizzle has a problem resolving schema-based arguments to the correct type.

The type error:

Argument of type 'Context' is not assignable to parameter of type '{ schema: { def: { type: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "optional" | "nonoptional" | "nullable" | "default" | "prefault" | ... 26 more ... | "custom"; error?: ((issue: never) => string | ... 2 more ... | undefined) | undefined; checks?: { ...; }[] | unde...'.
The types of 'schema.def.checks' are incompatible between these types.
  Type '$ZodCheck<never>[] | undefined' is not assignable to type '{ _zod: { def: { check: string; error?: ((issue: never) => string | { message: string; } | null | undefined) | undefined; abort?: boolean | undefined; when?: ((payload: { value: unknown; issues: ({ [x: string]: unknown; ... 6 more ...; readonly continue?: boolean | undefined; } | ... 9 more ... | { ...; })[]; aborte...'.

Tested in zod 4.1.12

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