Skip to content
Draft
14 changes: 13 additions & 1 deletion firestore-send-email/functions/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export const attachmentSchema = z
});

export const attachmentsSchema = z
.array(attachmentSchema)
.array(attachmentSchema, {
invalid_type_error:
"Field 'attachments' must be an array. If you have a single attachment object, wrap it in an array (e.g., [{ filename: '...', path: '...' }])",
})
.optional()
.transform((attachments) =>
attachments
Expand Down Expand Up @@ -184,12 +187,21 @@ function formatZodError(
const path = issue.path.length > 0 ? issue.path.join(".") : context;
switch (issue.code) {
case "invalid_type":
if (issue.received === "undefined") {
const expected = issue.expected === "object" ? "map" : issue.expected;
const article = ["a", "e", "i", "o", "u"].includes(expected[0])
? "an"
: "a";
return `Field '${path}' must be ${article} ${expected}`;
}

if (issue.expected === "string") {
return `Field '${path}' must be a string`;
}
if (issue.expected === "array") {
return `Field '${path}' must be an array`;
}
Comment thread
IzaakGough marked this conversation as resolved.

if (issue.expected === "object") {
return `Field '${path}' must be a map`;
}
Expand Down
Loading