Skip to content

Commit

Permalink
fix: allow 'null' values for optional fields in custom types (#1864)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Nov 16, 2024
1 parent 285b258 commit f377441
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export function generateTypeDefType(sourceFile: SourceFile, decl: TypeDef) {
field.comments.forEach((c) => writer.writeLine(` * ${unwrapTripleSlashComment(c)}`));
writer.writeLine(` */`);
}
// optional fields are also nullable (to be consistent with Prisma)
writer.writeLine(
` ${field.name}${field.type.optional ? '?' : ''}: ${zmodelTypeToTsType(field.type)};`
` ${field.name}${field.type.optional ? '?' : ''}: ${zmodelTypeToTsType(field.type)}${
field.type.optional ? ' | null' : ''
};`
);
});
});
Expand Down
45 changes: 45 additions & 0 deletions tests/regression/tests/issue-1857.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('issue 1857', () => {
it('regression', async () => {
const { zodSchemas } = await loadSchema(
`
type JSONContent {
type String
text String?
}
model Post {
id String @id @default(uuid())
content JSONContent @json
@@allow('all', true)
}
`,
{
provider: 'postgresql',
pushDb: false,
compile: true,
extraSourceFiles: [
{
name: 'main.ts',
content: `
import { PrismaClient } from '@prisma/client';
import { enhance } from '.zenstack/enhance';
async function main() {
const prisma = new PrismaClient();
await prisma.post.create({
data: {
content: { type: 'foo', text: null }
}
});
}
`,
},
],
}
);

zodSchemas.models.JSONContentSchema.parse({ type: 'foo', text: null });
});
});

0 comments on commit f377441

Please sign in to comment.