Files
pn-new-crm/src/lib/validators/files.ts
Matt 0804944647 fix(documents): folderId=empty-string normalises to null at validator
The hub UI sends folderId='' when the user picks "root-only" in the
folder sidebar. The Zod validator was accepting it as a string and
the service then ran eq(folderId, '') instead of isNull(folderId),
returning zero results.

Adding a .transform on folderId converts empty string to null at the
boundary so the service receives the expected nullable shape. Pre-
existing bug from Wave 11.B that the hub rebuild made more visible.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 13:10:19 +02:00

51 lines
1.6 KiB
TypeScript

import { z } from 'zod';
import { baseListQuerySchema } from '@/lib/api/list-query';
export const uploadFileSchema = z.object({
filename: z.string().min(1).max(255),
clientId: z.string().optional(),
yachtId: z.string().optional(),
companyId: z.string().optional(),
folderId: z.string().uuid().optional(),
category: z.string().optional(),
entityType: z.string().optional(),
entityId: z.string().optional(),
});
export const updateFileSchema = z.object({
filename: z.string().min(1).max(255).optional(),
category: z.string().optional(),
});
export const listFilesSchema = baseListQuerySchema
.extend({
clientId: z.string().optional(),
yachtId: z.string().optional(),
companyId: z.string().optional(),
category: z.string().optional(),
folderId: z
.string()
.uuid()
.optional()
.transform((v) => (v === '' ? null : v)),
/** Entity-aggregated projection params — mutually exclusive with folderId. */
entityType: z.enum(['client', 'company', 'yacht']).optional(),
entityId: z.string().uuid().optional(),
})
.refine(
(q) => !(q.folderId !== undefined && (q.entityType !== undefined || q.entityId !== undefined)),
{
message: 'folderId is mutually exclusive with entityType/entityId',
path: ['folderId'],
},
)
.refine((q) => Boolean(q.entityType) === Boolean(q.entityId), {
message: 'entityType and entityId must be provided together',
path: ['entityType'],
});
export type UploadFileInput = z.infer<typeof uploadFileSchema>;
export type UpdateFileInput = z.infer<typeof updateFileSchema>;
export type ListFilesInput = z.infer<typeof listFilesSchema>;