javascript - zod TypeError: Cannot read properties of undefined (reading '_parse') - Stack Overflow

I have a Vite library using Zod. I want to parse configurations and my folder structure is similiar to

I have a Vite library using Zod. I want to parse configurations and my folder structure is similiar to the configuration object structure. index.ts files always export all files in their own directory and everything from their subdirectories e.g. export * from './subDir'; so the root file exports "the whole lib".

The following setup shows a single configuration branch

Sample code on Stackblitz

.
├── src
│   ├── api
│   │   ├── dataSources
|   |   |   ├── dataSource
|   |   |   |   ├── following
|   |   |   |   |   ├── puted
|   |   |   |   |   |   ├── followingComputedDataSourceConfigurationSchema.ts ( extends dataSourceConfigurationSchema )
|   │   │   |   |   |   └── index.ts
|   |   |   |   |   ├── entity
|   |   |   |   |   |   ├── followingEntityDataSourceConfigurationSchema.ts ( extends leadingDataSourceConfigurationSchema )
|   │   │   |   |   |   └── index.ts
|   │   │   |   |   └── index.ts
|   |   |   |   ├── leading
|   |   |   |   |   ├── leadingDataSourceConfigurationSchema.ts ( extends dataSourceConfigurationSchema )
|   │   │   |   |   └── index.ts
|   |   |   |   ├── dataSourceConfigurationSchema.ts ( base schema )
│   │   |   |   └── index.ts
|   |   |   ├── dataSourcesConfigurationSchema.ts ( expects leading and array of followings )
│   │   |   └── index.ts
|   |   ├── apiConfigurationSchema.ts ( expects dataSources )
│   │   └── index.ts
│   └── index.ts 
└── test
    └── basic.test.ts

The problem is that I think I'm running into circular dependency imports. I checked the schema with a test using Vitest

it('fails.', () => {
  expect(() => apiConfigurationSchema.parse({})).not.toThrow();
});

By doing so I get the following error

TypeError: Cannot read properties of undefined (reading '_parse')

I don't want to merge the schemas into a single big file because subdirectories might also contain custom validation functions for this specific section.

Do you have any ideas how to fix this setup?

I have a Vite library using Zod. I want to parse configurations and my folder structure is similiar to the configuration object structure. index.ts files always export all files in their own directory and everything from their subdirectories e.g. export * from './subDir'; so the root file exports "the whole lib".

The following setup shows a single configuration branch

Sample code on Stackblitz

.
├── src
│   ├── api
│   │   ├── dataSources
|   |   |   ├── dataSource
|   |   |   |   ├── following
|   |   |   |   |   ├── puted
|   |   |   |   |   |   ├── followingComputedDataSourceConfigurationSchema.ts ( extends dataSourceConfigurationSchema )
|   │   │   |   |   |   └── index.ts
|   |   |   |   |   ├── entity
|   |   |   |   |   |   ├── followingEntityDataSourceConfigurationSchema.ts ( extends leadingDataSourceConfigurationSchema )
|   │   │   |   |   |   └── index.ts
|   │   │   |   |   └── index.ts
|   |   |   |   ├── leading
|   |   |   |   |   ├── leadingDataSourceConfigurationSchema.ts ( extends dataSourceConfigurationSchema )
|   │   │   |   |   └── index.ts
|   |   |   |   ├── dataSourceConfigurationSchema.ts ( base schema )
│   │   |   |   └── index.ts
|   |   |   ├── dataSourcesConfigurationSchema.ts ( expects leading and array of followings )
│   │   |   └── index.ts
|   |   ├── apiConfigurationSchema.ts ( expects dataSources )
│   │   └── index.ts
│   └── index.ts 
└── test
    └── basic.test.ts

The problem is that I think I'm running into circular dependency imports. I checked the schema with a test using Vitest

it('fails.', () => {
  expect(() => apiConfigurationSchema.parse({})).not.toThrow();
});

By doing so I get the following error

TypeError: Cannot read properties of undefined (reading '_parse')

I don't want to merge the schemas into a single big file because subdirectories might also contain custom validation functions for this specific section.

Do you have any ideas how to fix this setup?

Share Improve this question asked Apr 6, 2023 at 4:47 baitendbidzbaitendbidz 8054 gold badges22 silver badges71 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3 +100

You have a cyclic dependency (in 2 index.ts)

// index.ts
export { apiConfigurationSchema } from './apiConfigurationSchema';
export * from './dataSources';
// apiConfigurationSchema.ts
import { dataSourcesConfigurationSchema } from '.';
export const used = __use(dataSourcesConfigurationSchema )

where you import and use re-exported * BEFORE it gets re-exported

So, all you need to fix is...

// index.ts
export * from './dataSources';
export { apiConfigurationSchema } from './apiConfigurationSchema';

... just to swap two lines in two files

https://stackblitz./edit/vitest-dev-vitest-8uh3ya?file=src/api/index.ts



research details: I've created a file

// a.ts
import { apiConfigurationSchema } from './src/api/apiConfigurationSchema';
apiConfigurationSchema.parse({});

, installed tsx package, ran tsx watch a, and looked for where the error is until it was fixed

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745427530a4627248.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信