zod: ZodObject that are `.refine` or `.superRefine` returns a ZodEffects type
Zod version 3.21.4
Problem
Using .refine() or .superRefine() on ZodObject (z.object()) prevents the use of .shape or .merge() method as the returned type is a ZodEffects and not a ZodObject.
Example
const schema1 = z.object({ foo: z.string() }) // = type ZodObject
const schema2 = z.object({
bar: z.string(),
attributeRelatedToBar: z.string(),
}).superRefine(handleSchema2Errors) // = type ZodEffects
/**
* Impossible because `.merge` expect `schema2` to be type of `ZodObject` instead of `ZodEffects`:
* TS2352: Conversion of type 'ZodEffects […] to type 'ZodObject ' may be a mistake because
* neither type sufficiently overlaps with the other.
* If this was intentional, convert the expression to 'unknown' first.
*/
const finalSchema = schema1.merge(schema2)
/**
* Same error with `.shape` that expect a `ZodObject` instead of `ZodEffects`:
* TS2339: Property 'shape' does not exist on type 'ZodEffects'.
*/
schema2.shape.bar
schema2.shape.attributeRelatedToBar
Expected behavior
Using .superRefine() or .refine() on z.object() should return a ZodObject type
– or –
.merge() & .shape should be functional on ZodEffects applied to a ZodObject.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 102
- Comments: 39 (3 by maintainers)
This will be changed in Zod 4 — refinements will be stored in the schema itself instead of in an enclosing
ZodEffects.Is this what you are looking for?
If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 https://github.com/sponsors/JacobWeisenburger
It also prevents the use of
discrimatedUnion.This unfortunately make complex types into a real pain to use with this library
How is what I gave you not “fully functional”?
I used
superRefinein my solution. ✅I fail to see the problem that you are talking about. Please explain more.
Considering Zod doesn’t release very often, your choices are to use a workaround in the mean time or wait for your problem to be addressed and then wait for it to be released. Based on how things have been going, that could be 6 months. If that’s ok for you, then great.
Generally when someone tries to help you, it’s not the best approach to belittle their efforts. But thanks.
This isn’t only an issue for the object schema. It is an issue for all schemas.
.refine()and.superRefine()should return a schema that has all the same properties as what was there before it was used.You should use
.andsee https://github.com/colinhacks/zod/issues/1147Zod 4! We are waiting…!
@oyatek It breaks the composition
Example 1
Let’s imagine that we have a reusable type utility:
And you want to reuse it in multiple places:
The current approach makes it difficult to implement a schema utility and, in some ways, breaks schema encapsulation.
Example 2
You have some schema that represent API endpoint
But for the API POST method, you want to modify your schema and omit
idpropYou may have noticed that there is an issue with the current implementation of your application. Specifically, you may need to refine some of the schemas during the latest stage of development. However, the proposed solution would require you to rewrite a large portion of your application.
Seems that
.omit()isn’t allowed after a.refine(), which is not useful.This is the expected behavior. The unexpected behavior needs to be documented.
I don’t see how
.andis a replacement for calling refine or superRefine on a whole object to be able to access the properties in validation logic.@colinhacks Do you have any updates on when Zod 4 might be done? The current version has been great, but it sounds like Zod 4 will be a huge improvement.
Up on this topic ? Issue still the same with zod
^3.22.4@thibaultleouay Thank you. But I don’t see
.andas a solution but as a workaround 😄.This is fixed in Zod 4. See the announcement.
@matomesc same problem for me, did you guys find any solution to this? Is v4 going to solve the issue?
I actually need to extend my zod schemas and not being able to use .omit after refine is really troublesome.
I’ve stumbled upon this as well…
I create a super refiner programmatically, so I don’t really have access to the base schema:
what about this? (I just discovered
innerType)Does it suck? It that intended to be used this way?
Same issue here! I have pretty large object schemas that need to be changed altogether, and I am trying to use functions similar to the following for that.
the issue is that the date validation does’t get picked up because it is using
refine()but I cannot use
z.ZodEffectsbecause then everything that isn’t adate()validation is also going to get triggered.The same goes if I use
and(), since the outcome is that everything becomes an intersection instead of an effect.Is there any way for me to pick up the validation type even after using refine? I am fine with any workarounds for now
Hey any updates on this or reliable to handle this cases?
Struggling with the same problem… Waiting for Zod 4. Now its a bit painfull to reset another field if other is false.
The schema is a validation schema and must be fully functional. So in my context, this is a necessity to have a
refine|superRefineto make the validation work.There is a problem between
ZodObjectandZodEffectstype that needs to be fixed. I don’t want to use a workaround.Even if I wanted to use a workaround, the solution you suggested would have been easy to find.
any news on Zod v4 ?
@KikoCosmetics innerType() seems to remove the refine logic, at least for me it is no longer running. So no, this is not a solution.
I’m running into issues as well. It doesn’t make sense to me for the object to transform itself into a different type if
refineis chained to it.I have the same issue. This is my use case: I want to transform a field to have other sibling value but
transformover the wholez.ZodObjectmakes it az.ZodEffectand I cannot use it in further schemas:The same happens if I use
.andbecause I useotherSchemalater expecting it to be az.ZodObjectand notz.ZodIntersectionI have the same issue. I have a schema where I all
.refine()and then later I need to call.pick(),.omit()and.merge()but these methods are not available in theZodEffectstype. Using.and()doesn’t work either as the resultingZodIntersectiondoesn’t have these methods either.Any timeline on the release of v4?
I was hoping to get a workaround here, I have been battling this issue all day long, hopefully Zod 4 will be released soon.
Making these changes to the repo fixes the core issue https://github.com/colinhacks/zod/compare/master...Dan503:zod:2474-refine-not-returning-correct-types-info
I haven’t made a PR because it still has type conflict errors and is failing a lot of unit tests.
I am running into the same issue but after using
.transform. My use case is as follows:Because my
parentSchema.numberPropis set to aZodEffectthe.requireddoes not apply to it sinceZodEffectdoes not extendZodOptionalorZodNullable.It is also a similar problem when using
.pipewhich returns aZodPipeline.I solved this by adding a wrapper object around the actual object to validate. This way it is possible to use refine on nested objects while returning a type compatible with ZodTypeAny.
const schema2 = z.object({ wrapper: z.object({ bar: z.string(), attributeRelatedToBar: z.string(), }).superRefine(handleSchema2Errors) }).
Then add a wrapper middleware to wrap the post data before you parse it and add some custom unwrapper to get the proper form field errors.
Hey how are people actually solving this issue? seems like something almost everyone will experience…
In my scenario it is a solution, cause I only picked a field that is not refined.
But yes if I had to pick the passwords I should refine them again. Not very cool, but that’s essentially the reason why I created that ‘passwordMatchRefiner’ function…
Has anyone managed to resolve this? I have this problem, I use .refine and now I want to use .pick but it doesn’t exist in ZodEffect.
I came across as well. How to fix this? By fix I mean that I want to keep the chaining of the schema, like merging another schema or do anything else.
If refine shouldn’t mean to support this, what does so?
I am working on a dynamic form that can change depending on the persona. So I’ve created a schema by input, which is then assembled according to the displayed input. Some inputs have special validation and need a
refineattached to a schema to be more reusable and easily importable. In this case, if I have to do what you suggest for each schema, it doesn’t make the code more readable or maintainable and can be prone to forgetting.Yes, but at the end of the final schema and if I need to merge multiple object, what the method refine receive in parameter will change according to what it’s merged inside. Not easy to maintain.
To be more concrete, say I have a WYSIWYG input and I need to count how many characters are written (without taking into account html tags). To do that, I need to use
refineto count plain text instead of generated html. On top of that, I have multiple inputs that need special validation using refine/superfine according to persona and some user configuration.I have another workaround on the validation side after a
.safeParse(). I’m not happy with it, but it’s the least dirty I can do for now.Sorry, I didn’t mean to be mean, but from the way I described the bug, you can imagine that I’m not a junior. The proposed solution wasn’t very complex and the reason for this issue was a real problem with the library. I just want to show this problem so that a solution can be found.