From ec2a3f6238ce73acf2089ded92b2fe815e568e75 Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Fri, 18 Jul 2025 11:57:54 -0600 Subject: [PATCH] Update refiner for palettes --- .../organizations/update-organization.ts | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/database/src/queries/organizations/update-organization.ts b/packages/database/src/queries/organizations/update-organization.ts index 5de0785d8..56a3644b0 100644 --- a/packages/database/src/queries/organizations/update-organization.ts +++ b/packages/database/src/queries/organizations/update-organization.ts @@ -19,10 +19,9 @@ const OrganizationColorPaletteSchema = z.object({ name: z.string().min(1).max(255), }); -// Input validation schema -const UpdateOrganizationInputSchema = z.object({ - organizationId: z.string().uuid('Organization ID must be a valid UUID'), - organizationColorPalettes: z.object({ +// Organization Color Palettes schema (extracted to its own schema) +const OrganizationColorPalettesSchema = z + .object({ selectedId: z.string().min(1).max(255).nullable(), palettes: z.array(OrganizationColorPaletteSchema).refine( (palettes) => { @@ -35,7 +34,26 @@ const UpdateOrganizationInputSchema = z.object({ message: 'All color palette IDs must be unique', } ), - }), + }) + .refine( + (data) => { + // If selectedId is null, no validation needed + if (data.selectedId === null) return true; + + // If selectedId is provided, it must exist in the palettes array + const paletteIds = data.palettes.map((palette) => palette.id); + return paletteIds.includes(data.selectedId); + }, + { + message: 'Selected ID must exist in the palettes array', + path: ['selectedId'], // Point the error to the selectedId field + } + ); + +// Input validation schema +const UpdateOrganizationInputSchema = z.object({ + organizationId: z.string().uuid('Organization ID must be a valid UUID'), + organizationColorPalettes: OrganizationColorPalettesSchema, }); type UpdateOrganizationInput = z.infer;