31 lines
1.1 KiB
TypeScript
31 lines
1.1 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import { templateSendSchema, templateToMail } from "./mail-template";
|
|
import { MailValidationError, normalizeMail, sendSchema, type NormalizedMail } from "./schema";
|
|
|
|
export const batchInputSchema = z.array(z.unknown()).min(1).max(100);
|
|
|
|
export function parseMailInput(body: unknown, fromDomain: string): NormalizedMail {
|
|
if (body && typeof body === "object" && "template" in body) {
|
|
const parsed = templateSendSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
throw new MailValidationError(parsed.error.issues.map((issue) => issue.message).join("; "));
|
|
}
|
|
return templateToMail(parsed.data, fromDomain);
|
|
}
|
|
|
|
const parsed = sendSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
throw new MailValidationError(parsed.error.issues.map((issue) => issue.message).join("; "));
|
|
}
|
|
return normalizeMail(parsed.data, fromDomain);
|
|
}
|
|
|
|
export function parseBatchInput(body: unknown): unknown[] {
|
|
const parsed = batchInputSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
throw new MailValidationError(parsed.error.issues.map((issue) => issue.message).join("; "));
|
|
}
|
|
return parsed.data;
|
|
}
|