Skip to content
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
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 });
});
});
Loading