Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag grouping common schema import issue #309

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/src/template-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,20 @@ export const getZodClientTemplateContext = (

const addDependencyIfNeeded = (schemaName: string) => {
if (!schemaName) return;
if (schemaName.startsWith("z.")) return;
// Responses can refer to both z chaining methods and models
// To be able to import them we need to extract them from the raw zod expression.
// E.g: `z.array(z.union([FooBar.and(Bar).and(Foo), z.union([FooBar, Bar, Foo])]))`
const isZodExpression = schemaName.includes("z.")
if (isZodExpression) {
for (const name in result.zodSchemaByName) {
// Matching whole word to avoid false positive e.g: avoid macthing `Foo` with `FooBar`
const regex = new RegExp(String.raw`\b(${name})\b`);
if (regex.test(schemaName)) {
dependencies.add(name);
}
}
return
}
// Sometimes the schema includes a chain that should be removed from the dependency
const [normalizedSchemaName] = schemaName.split(".");
dependencies.add(normalizedSchemaName!);
Expand Down
Loading