Simple test that verifies Prisma JSON Types Generator works correctly.
The test leverages TypeScript compiler to validate that JSON fields are typed correctly:
prisma.test.create({
data: {
testfield: {
name: 'Test value' // This must be TestType type
}
}
});If TypeScript compiles ✅ = JSON types generator works
If TypeScript throws errors ❌ = JSON types generator doesn't work
# 1. Install dependencies
npm install
# 2. Run test (generates + tests automatically)
npm run testThe test automatically does:
prisma generate- Generates Prisma client + JSON typestsc --noEmit --strict- Checks TypeScript types- If both succeed → ✅ Test passed
prisma/schema.prisma- Test model withtestfield Json?fieldsrc/types.ts-PrismaJson.TestTypeinterface definitionsrc/index.ts- Simple test that uses typed fieldssrc/generated/- Prisma generates client files heretsconfig.json- TypeScript configuration with strict mode
When test succeeds:
✅ All tests passed! JSON types are correctly generated.
When test fails, you'll see TypeScript errors that tell you what's wrong.
To test that the test correctly catches failures, comment out the JSON types generator in prisma/schema.prisma:
# generator json {
# provider = "prisma-json-types-generator"
# }Then run npm test - the TypeScript compilation should fail, demonstrating that the test properly validates the generator's functionality.
When the JSON types generator is commented out or fails, you'll see this TypeScript error:
✔ Generated Prisma Client (6.18.0) to .\src\generated in 34ms
src/index.ts:15:7 - error TS2322: Type 'TestType' is not assignable to type 'NullableJsonNullValueInput | InputJsonValue | undefined'.
Type 'TestType' is not assignable to type 'InputJsonObject'.
Index signature for type 'string' is missing in type 'TestType'.
15 testfield: testField
~~~~~~~~~
Found 1 error in src/index.ts:15
This error occurs because:
- Without the JSON types generator, Prisma treats JSON fields as generic
InputJsonValuetype - The
PrismaJson.TestTypeinterface is not recognized by the Prisma client - TypeScript cannot match our custom
TestTypewith the generic JSON input type
This demonstrates that the JSON types generator is essential for proper TypeScript integration with JSON fields in Prisma.