admin管理员组文章数量:1429276
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 badges1 Answer
Reset to default 3 +100You 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
本文标签: javascriptzod TypeError Cannot read properties of undefined (reading 39parse39)Stack Overflow
版权声明:本文标题:javascript - zod TypeError: Cannot read properties of undefined (reading '_parse') - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745427530a2658183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论