zod: Cannot read properties of undefined (reading '_parse')
Hey, I’ve come across this issue in my project while composing schemas from imported zod objects/enums.Here is the code:
CountryCode.ts
import { z } from 'zod';
const countryCode = z.enum(['IE', 'GB']);
type CountryCode = z.infer<typeof countryCode>;
const countrySchema = z.object({
alpha2: z.string(),
alpha3: z.string(),
code: countryCode,
name: z.string(),
region: z.string(),
regionCode: z.number(),
subRegion: z.string(),
subRegionCode: z.number(),
});
type Country = z.infer<typeof countrySchema>;
export { countryCode, countrySchema };
export type { Country, CountryCode };
CurrencyCode.ts
import { z } from 'zod';
const currencyCode = z.enum(['EUR', 'GBP']);
type CurrencyCode = z.infer<typeof currencyCode>;
const currencySchema = z.object({
code: currencyCode,
name: z.string(),
minorUnit: z.number(),
});
type Currency = z.infer<typeof currencySchema>;
export { currencyCode, currencySchema };
export type { CurrencyCode, Currency };
If i import the above like so: invitePerson.ts
import { z } from 'zod';
import { countryCode, currencyCode } from '../../api';
const invitePersonSchema = z.object({
startDate: z.date().optional(),
firstName: z.string().min(1, 'First name is required'),
lastName: z.string().min(1, 'Last name is required'),
role: z.string().optional(),
dateOfBirth: z.date().optional(),
email: z.string().email('Email is required'),
residentialAddress: z.object({
addressLine1: z.string().optional(),
addressLine2: z.string().optional(),
addressLine3: z.string().optional(),
city: z.string().optional(),
countryCode: countryCode,
postalCode: z.string().optional(),
}),
annualSalary: z
.string()
.min(1, 'Annual salary is required')
.transform(value => parseInt(value, 10)),
salaryCurrency: currencyCode,
taxId: z.string().optional(),
});
type InvitePersonData = z.infer<typeof invitePersonSchema>;
export { invitePersonSchema };
export type { InvitePersonData };
I get the error in the title. However, if I define the enums directly in the invitePersonSchema and don’t import, there is no error. I would have expected this to be allowed, so please forgive me if I am misusing the library in some way.
Note: this was already discussed here, but the last comment provides a reproducible example which I have copied below, just in case it helps: https://github.com/colinhacks/zod/issues/643
I ran into the same error message and found this issue. Now while my problem is likely a different another one, I thought sharing this might still help others ending up here.
Essentially what I was doing is exporting a schema from one file, using it in another one to compose another schema, and then importing that composed schema back into the first file. While there isn’t a cyclic dependency per se, it causes the mentioned error at runtime.
See this CodeSandbox for an example: https://codesandbox.io/s/patient-butterfly-hsg3q?file=/src/index.ts The message isn’t as descriptive as in my local (larger) project, but removing this exact kind of import solved it for me. (Maybe this is generally “wrong” in terms of my tsconfig or simply unsupported by zod and I don’t know it, but I would intuitively expect it to work 🙈.)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 2
- Comments: 15 (1 by maintainers)
I solved it by modifying some imported files: splitting them or joining other parts together. Apparently this is some kind of circular dependency issue - even though the rest of the code did just work fine all the time. But after restructuring everything worked. (Note that I also had to seperate stuff which actually wasn’t even called but webpack did combine it into a bundle and therefore caused that issue)
Had the same issue, turned out to be a circular dependency between two files
@cromaniuc
That’s circular dependency 😁
bite the bullet — stick all models in a single file
I have a 2000 line zod model file. Not sorry
I had a Zod schema defined in a file using schemas defined in another. I was having the about the same issue (
Cannot read properties of undefined (reading '_asyncParse').Moving the schemas in the same file fixed it.
I then got the issue again in another context. I looked at this issue which was mentioned earlier and avoiding the “circular import” mentioned here fixed it, definitely I hope.
this issue is not really related to zod but to the way circular imports work. when you have a circular dependency between two files, the second one to be loaded will get “undefined” for all the objects exported by the first one.
for example, if you have these two files:
when running
a.ts, you get this output:note that
ais undefined when logging from fileb.ts. this happens regardless of whether there is a circular dependency between the objectsaandbthemselves.in your example,
index.tsimportsotherFile.tswhich importsindex.ts. the valueSeverityis undefined when creating the schemaIssue, which then fails when calling parse.Facing the same issue and this is a clear reproducible example without any circular dependency: https://codesandbox.io/s/patient-butterfly-hsg3q?file=/src/index.ts
I just discovered zod recently and was about to migrate our project to it, but apparently I’m running into this issue too.
The Enum is in a different file (where it belongs to) and I get this cryptic error message (“TypeError: Cannot read properties of undefined (reading ‘_parseSync’)”). If I temporarly put the enums inline in the same file where the zod object is created, everything works.
This is what worked for me: Initially, I had a folder for schemas and exported all my schemas in my index.ts file inside the schema folder like this:
Instead of exporting everything from each file in my index.ts, I switched to specific exports, only exporting what I actually needed:
This resolved my issue. 🚀 Hope this helps!