|
| 1 | +import { AnyNull, DbNull, JsonNull } from '@zenstackhq/orm'; |
| 2 | +import { createTestClient } from '@zenstackhq/testtools'; |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { schema } from './schema'; |
| 5 | + |
| 6 | +// https://github.com/zenstackhq/zenstack/issues/2411 |
| 7 | +// TypeScript errors with nullable custom JSON types when using DbNull/JsonNull/AnyNull |
| 8 | + |
| 9 | +describe('Regression for issue #2411', () => { |
| 10 | + it('should accept DbNull/JsonNull/AnyNull for nullable typed JSON fields in create/update/find', async () => { |
| 11 | + const db = await createTestClient(schema); |
| 12 | + const metadata = { someInt: 1, someString: 'test' }; |
| 13 | + |
| 14 | + /* --------------------------------- CREATE --------------------------------- */ |
| 15 | + |
| 16 | + // metadata (non nullable) - these should cause TS errors |
| 17 | + // @ts-expect-error - should not be able to set a null value to the non nullable field |
| 18 | + await expect(db.foo.create({ data: { metadata: DbNull } })).rejects.toThrow(); |
| 19 | + // @ts-expect-error - should not be able to set a null value to the non nullable field |
| 20 | + await expect(db.foo.create({ data: { metadata: JsonNull } })).rejects.toThrow(); |
| 21 | + // @ts-expect-error - should not be able to set a null value to the non nullable field |
| 22 | + await expect(db.foo.create({ data: { metadata: AnyNull } })).rejects.toThrow(); |
| 23 | + // @ts-expect-error - should not be able to set a null value to the non nullable field |
| 24 | + await expect(db.foo.create({ data: { metadata: null } })).rejects.toThrow(); |
| 25 | + |
| 26 | + await db.foo.create({ data: { metadata } }); // ✅ No typescript error |
| 27 | + |
| 28 | + // optionalMetadata (nullable) - DbNull/JsonNull should NOT cause TS errors |
| 29 | + await db.foo.create({ data: { metadata, optionalMetadata: DbNull } }); |
| 30 | + await db.foo.create({ data: { metadata, optionalMetadata: JsonNull } }); |
| 31 | + // @ts-expect-error - AnyNull is not accepted for typed JSON fields (TS + runtime rejection) |
| 32 | + await expect(db.foo.create({ data: { metadata, optionalMetadata: AnyNull } })).rejects.toThrow(); |
| 33 | + await db.foo.create({ data: { metadata, optionalMetadata: null } }); // ✅ No typescript error |
| 34 | + |
| 35 | + /* --------------------------------- UPDATE --------------------------------- */ |
| 36 | + |
| 37 | + const firstFoo = await db.foo.findFirst(); |
| 38 | + expect(firstFoo).not.toBeNull(); |
| 39 | + const where = { id: firstFoo!.id }; |
| 40 | + |
| 41 | + // metadata (non nullable) - these should cause TS errors |
| 42 | + // @ts-expect-error - should not be able to set a null value to the non nullable field |
| 43 | + await expect(db.foo.update({ where, data: { metadata: DbNull } })).rejects.toThrow(); |
| 44 | + // @ts-expect-error - should not be able to set a null value to the non nullable field |
| 45 | + await expect(db.foo.update({ where, data: { metadata: JsonNull } })).rejects.toThrow(); |
| 46 | + // @ts-expect-error - should not be able to set a null value to the non nullable field |
| 47 | + await expect(db.foo.update({ where, data: { metadata: AnyNull } })).rejects.toThrow(); |
| 48 | + // @ts-expect-error - should not be able to set a null value to the non nullable field |
| 49 | + await expect(db.foo.update({ where, data: { metadata: null } })).rejects.toThrow(); |
| 50 | + |
| 51 | + await db.foo.update({ where, data: { metadata } }); // ✅ No typescript error |
| 52 | + |
| 53 | + // optionalMetadata (nullable) - DbNull/JsonNull should NOT cause TS errors |
| 54 | + await db.foo.update({ where, data: { metadata, optionalMetadata: DbNull } }); |
| 55 | + await db.foo.update({ where, data: { metadata, optionalMetadata: JsonNull } }); |
| 56 | + // @ts-expect-error - AnyNull is not accepted for typed JSON fields (TS + runtime rejection) |
| 57 | + await expect(db.foo.update({ where, data: { metadata, optionalMetadata: AnyNull } })).rejects.toThrow(); |
| 58 | + await db.foo.update({ where, data: { metadata, optionalMetadata: null } }); // ✅ No typescript error |
| 59 | + |
| 60 | + /* ---------------------------------- FIND ---------------------------------- */ |
| 61 | + |
| 62 | + // metadata (non nullable) - these should cause TS errors |
| 63 | + // @ts-expect-error - should not be able to filter by DbNull on a non nullable field |
| 64 | + void db.foo.findMany({ where: { metadata: DbNull } }); |
| 65 | + // @ts-expect-error - should not be able to filter by JsonNull on a non nullable field |
| 66 | + void db.foo.findMany({ where: { metadata: JsonNull } }); |
| 67 | + // @ts-expect-error - should not be able to filter by AnyNull on a non nullable field |
| 68 | + void db.foo.findMany({ where: { metadata: AnyNull } }); |
| 69 | + // @ts-expect-error - should not be able to filter by null on a non nullable field |
| 70 | + void db.foo.findMany({ where: { metadata: null } }); |
| 71 | + |
| 72 | + // optionalMetadata (nullable) - these should NOT cause TS errors |
| 73 | + await db.foo.findMany({ where: { optionalMetadata: DbNull } }); |
| 74 | + await db.foo.findMany({ where: { optionalMetadata: JsonNull } }); |
| 75 | + await db.foo.findMany({ where: { optionalMetadata: AnyNull } }); |
| 76 | + await db.foo.findMany({ where: { optionalMetadata: null } }); // ✅ No typescript error |
| 77 | + }); |
| 78 | +}); |
0 commit comments