import { z } from "zod"; import { normalizeAllowedContentTypes } from "../../utils/appUtils"; export const AttachmentSchema = (allowedContentType, maxSizeAllowedInMB) => { const allowedTypes = normalizeAllowedContentTypes(allowedContentType); return z.object({ fileName: z.string().min(1, { message: "File name is required" }), base64Data: z.string().min(1, { message: "File data is required" }), contentType: z .string() .min(1, { message: "MIME type is required" }) .refine( (val) => (allowedTypes.length ? allowedTypes.includes(val) : true), { message: `File type must be one of: ${allowedTypes.join(", ")}`, } ), fileSize: z .number() .int() .nonnegative("fileSize must be ≥ 0") .max( (maxSizeAllowedInMB ?? 25) * 1024 * 1024, `fileSize must be ≤ ${maxSizeAllowedInMB ?? 25}MB` ), description: z.string().optional().default(""), isActive: z.boolean(), }); }; export const TagSchema = z.object({ name: z.string().min(1, "Tag name is required"), isActive: z.boolean().default(true), }); export const DocumentPayloadSchema = (docConfig = {}) => { const { isMandatory, regexExpression, allowedContentType, maxSizeAllowedInMB, } = docConfig; let documentIdSchema = z.string(); if (isMandatory) { documentIdSchema = documentIdSchema.min(1, { message: "DocumentId is required", }); } if (regexExpression) { documentIdSchema = documentIdSchema.regex( new RegExp(regexExpression), "Invalid DocumentId format" ); } return z.object({ name: z.string().min(1, "Name is required"), documentId: documentIdSchema, description: z.string().min(1, { message: "Description is required" }), // entityId: z.string().min(1, { message: "Please Select Document Entity" }), documentTypeId: z.string().min(1, { message: "Please Select Document Type" }), documentCategoryId: z .string() .min(1, { message: "Please Select Document Category" }), attachment: AttachmentSchema(allowedContentType, maxSizeAllowedInMB).nullable().refine( (val) => val !== null, { message: "Attachment is required" } ), tags: z.array(TagSchema).optional().default([]), }); }; export const defaultDocumentValues = { name: "", documentId: "", description: "", // entityId: "", documentTypeId: "", documentCategoryId: "", attachment: { fileName: "", base64Data: "", contentType: "", fileSize: 0, description: "", isActive: true, }, tags: [], }; //--------------------Filter------------------------- export const DocumentFilterSchema = z.object({ uploadedByIds: z.array(z.string()).default([]), documentCategoryIds: z.array(z.string()).default([]), documentTypeIds: z.array(z.string()).default([]), documentTagIds: z.array(z.string()).default([]), isUploadedAt: z.boolean().default(true), isVerified: z.boolean().nullable().optional(), startDate: z.string().nullable().optional(), endDate: z.string().nullable().optional(), }); export const DocumentFilterDefaultValues = { uploadedByIds: [], documentCategoryIds: [], documentTypeIds: [], documentTagIds: [], isUploadedAt: true, isVerified: null, startDate: null, endDate: null, };