From c98c758bc4e856d5fc9241e0a621cfdbcc7271df Mon Sep 17 00:00:00 2001 From: Saksham Goyal Date: Mon, 1 Apr 2024 23:26:26 -0400 Subject: [PATCH 1/2] zod uuid --- packages/schema/src/plugins/zod/utils/schema-gen.ts | 4 ++++ 1 file changed, 4 insertions(+) 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; From 9b3111b50e605fc2f998fdbe9cf135cd5fc58407 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Wed, 3 Apr 2024 04:52:00 +0800 Subject: [PATCH 2/2] add a test case --- tests/integration/tests/plugins/zod.test.ts | 5 +++++ 1 file changed, 5 insertions(+) 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 () => {