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