Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use z.ZodType<object> instead of z.AnyZodObject #21

Open
dirkluijk opened this issue Mar 11, 2025 · 2 comments
Open

Use z.ZodType<object> instead of z.AnyZodObject #21

dirkluijk opened this issue Mar 11, 2025 · 2 comments

Comments

@dirkluijk
Copy link

dirkluijk commented Mar 11, 2025

The current type-definition allows the schema to be only a z.object():

type Config<T extends z.AnyZodObject = z.AnyZodObject> = {
  schema: T;

  // ...
}

How-ever, this means you cannot pass in any other Zod type, e.g. that uses Zod effects like .transform(), .preprocesses() or .refine():

await loadConfig({
    schema: z.preprocess((input) => {
        // do stuff
        return input;
    }, myConfigSchema),
}),

Image

I believe a more correct type-definition, that maintains the restriction of the config to be an object, would be:

type Config<T extends z.ZodType<object> = z.ZodType<object>> = {
  schema: T;
  // ...
}

Since it makes the type-definition less strict, I believe it should be backwards compatible.

The only major issue I see, is that it would not allow zod-config nor any adapter to inspect ZodObject-specific things, for example the shape property. This might be crucial if we want to support #22.

@alexmarqs
Copy link
Owner

Hi @dirkluijk! It is not currently supported, you are right. Using z.ZodType<object> to allow usage of Zod effects will bring some other limitations like turning the schema ZodEffects and not a typical Zod Object instance. Let me check later the issues that you created to see if this impacts your end goal :).

Note: Looks like in Zod 4 this will be solved colinhacks/zod#2474, in the mean time if you are using a custom adapter you can wrap your config data in a nested property and this one can be used with effects (e.g. { config: z.object(...).refine(....) })

@dirkluijk
Copy link
Author

Super cool, didn't know about the issue you mentioned! :)

I think this issue also depends on the directions we discuss in #22.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants