From 290b66fec137302ed307cafd28a7a435f0427caa Mon Sep 17 00:00:00 2001 From: Asis Ferrer Date: Fri, 26 Jun 2026 02:21:25 +0200 Subject: [PATCH] fix(schemas): use z.union + transform for teamId empty string handling --- src/schemas/person.schema.ts | 13 +++++-------- src/schemas/user.schema.ts | 32 ++++++++++++++++---------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/schemas/person.schema.ts b/src/schemas/person.schema.ts index c1dcb53..035088b 100644 --- a/src/schemas/person.schema.ts +++ b/src/schemas/person.schema.ts @@ -22,14 +22,11 @@ function buildPersonBaseSchema(copy: PersonSchemaCopy) { lastName: z.string().min(1, { error: copy.lastNameRequired, }), - teamId: z.preprocess( - (val) => (val === "" ? null : val), - z - .string() - .uuid({ error: copy.teamIdInvalid }) - .optional() - .nullable(), - ), + teamId: z + .union([z.string().uuid({ error: copy.teamIdInvalid }), z.literal("")]) + .transform((val) => (val === "" ? null : val)) + .nullable() + .optional(), email: z.string().optional().nullable(), phone: z.string().optional().nullable(), userId: z diff --git a/src/schemas/user.schema.ts b/src/schemas/user.schema.ts index 102d3de..d00f3b6 100644 --- a/src/schemas/user.schema.ts +++ b/src/schemas/user.schema.ts @@ -92,14 +92,14 @@ export function buildUnifiedUpdateSchema(copy: UnifiedSchemaCopy) { id: z.string().nonempty(copy.idRequired), firstName: z.string().trim().min(1, { error: copy.firstNameRequired }), lastName: z.string().trim().min(1, { error: copy.lastNameRequired }), - teamId: z.preprocess( - (val) => (val === "" ? null : val), - z - .string() - .uuid({ error: copy.teamIdInvalid }) - .optional() - .nullable(), - ), + teamId: z + .union([ + z.string().uuid({ error: copy.teamIdInvalid }), + z.literal(""), + ]) + .transform((val) => (val === "" ? null : val)) + .nullable() + .optional(), email: z .union([z.email({ error: copy.emailInvalid }), z.literal(""), z.null()]) .optional(), @@ -133,14 +133,14 @@ export function buildUnifiedCreateSchema(copy: UnifiedSchemaCopy) { .object({ firstName: z.string().trim().min(1, { error: copy.firstNameRequired }), lastName: z.string().trim().min(1, { error: copy.lastNameRequired }), - teamId: z.preprocess( - (val) => (val === "" ? null : val), - z - .string() - .uuid({ error: copy.teamIdInvalid }) - .optional() - .nullable(), - ), + teamId: z + .union([ + z.string().uuid({ error: copy.teamIdInvalid }), + z.literal(""), + ]) + .transform((val) => (val === "" ? null : val)) + .nullable() + .optional(), email: z.email({ error: copy.emailInvalid }), phone: z.string().optional().nullable(), role: unifiedFormRoleSchema,