diff --git a/packages/schema/src/plugins/zod/utils/schema-gen.ts b/packages/schema/src/plugins/zod/utils/schema-gen.ts index a13a316b4..c660ad606 100644 --- a/packages/schema/src/plugins/zod/utils/schema-gen.ts +++ b/packages/schema/src/plugins/zod/utils/schema-gen.ts @@ -102,6 +102,10 @@ export function makeFieldSchema(field: DataModelField, respectDefault = false) { schema += `.toUpperCase()`; break; } + case '@db.Uuid': { + schema += `.uuid()`; + break; + } case '@datetime': { schema += `.datetime({ offset: true${message ? ', message: ' + JSON.stringify(message) : ''} })`; break; diff --git a/tests/integration/tests/plugins/zod.test.ts b/tests/integration/tests/plugins/zod.test.ts index dd82f6786..d27d2bb45 100644 --- a/tests/integration/tests/plugins/zod.test.ts +++ b/tests/integration/tests/plugins/zod.test.ts @@ -2,6 +2,7 @@ /// import { loadSchema } from '@zenstackhq/testtools'; +import { randomUUID } from 'crypto'; import fs from 'fs'; import path from 'path'; @@ -235,6 +236,7 @@ describe('Zod plugin tests', () => { o Int? @lt(1, 'must be less than 1') p Int? @lte(1, 'must be less than or equal to 1') q Int[] + r String? @db.Uuid } `; @@ -295,6 +297,9 @@ describe('Zod plugin tests', () => { expect(schema.safeParse({ q: [1] }).success).toBeTruthy(); expect(schema.safeParse({ q: ['abc'] }).success).toBeFalsy(); + + expect(schema.safeParse({ r: 'abc' }).success).toBeFalsy(); + expect(schema.safeParse({ r: randomUUID() }).success).toBeTruthy(); }); it('refinement scalar fields', async () => {