Skip to content

fix(zod): generated schemas fail to compile when a delegate discrimin ator field has default value #1705

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

Merged
merged 1 commit into from
Sep 16, 2024
Merged
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
10 changes: 6 additions & 4 deletions packages/schema/src/plugins/zod/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,10 @@ export function ${refineFuncName}<T, D extends z.ZodTypeDef>(schema: z.ZodType<T
}

// delegate discriminator fields are to be excluded from mutation schemas
const delegateFields = model.fields.filter((field) => isDiscriminatorField(field));
const delegateDiscriminatorFields = model.fields.filter((field) => isDiscriminatorField(field));
const omitDiscriminators =
delegateFields.length > 0
? `.omit({ ${delegateFields.map((f) => `${f.name}: true`).join(', ')} })`
delegateDiscriminatorFields.length > 0
? `.omit({ ${delegateDiscriminatorFields.map((f) => `${f.name}: true`).join(', ')} })`
: '';

////////////////////////////////////////////////
Expand Down Expand Up @@ -485,9 +485,11 @@ export const ${upperCaseFirst(model.name)}PrismaUpdateSchema = ${prismaUpdateSch

// mark fields with default as optional
if (fieldsWithDefault.length > 0) {
// delegate discriminator fields are omitted from the base schema, so we need
// to take care not to make them partial otherwise the schema won't compile
createSchema = this.makePartial(
createSchema,
fieldsWithDefault.map((f) => f.name)
fieldsWithDefault.filter((f) => !delegateDiscriminatorFields.includes(f)).map((f) => f.name)
);
}

Expand Down
20 changes: 20 additions & 0 deletions tests/regression/tests/issue-1693.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('issue 1693', () => {
it('regression', async () => {
await loadSchema(
`
model Animal {
id String @id @default(uuid())
animalType String @default("")
@@delegate(animalType)
}

model Dog extends Animal {
name String
}
`,
{ fullZod: true }
);
});
});
Loading