48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import { isValidHostname, isValidPort } from "../general";
|
|
import { isValidEmail } from "./email";
|
|
|
|
import type { MailerConfig } from "#types/config";
|
|
import type { simpleConfigValidation } from "#types/lib";
|
|
|
|
function validateMailerConfig(config: MailerConfig): simpleConfigValidation {
|
|
const errors: string[] = [];
|
|
|
|
const isValidSMTPAddress = isValidHostname(config.address);
|
|
|
|
if (!isValidSMTPAddress) {
|
|
errors.push(`Invalid SMTP address: ${config.address}`);
|
|
}
|
|
|
|
if (!isValidPort(config.port)) {
|
|
errors.push(
|
|
`Invalid SMTP port: ${config.port}. Port must be between 1 and 65535`,
|
|
);
|
|
}
|
|
|
|
if (!isValidEmail(config.from)) {
|
|
errors.push(`Invalid from email address: ${config.from}`);
|
|
}
|
|
|
|
if (!isValidEmail(config.username)) {
|
|
errors.push(`Invalid username email address: ${config.username}`);
|
|
}
|
|
|
|
if (!config.password || config.password.trim().length === 0) {
|
|
errors.push("SMTP password is required");
|
|
}
|
|
|
|
if (config.port === 465 && !config.secure) {
|
|
errors.push("Port 465 requires SMTP_SECURE=true");
|
|
}
|
|
|
|
if (config.port === 587 && config.secure) {
|
|
errors.push("Port 587 typically uses SMTP_SECURE=false with STARTTLS");
|
|
}
|
|
|
|
return {
|
|
isValid: errors.length === 0,
|
|
errors,
|
|
};
|
|
}
|
|
|
|
export { isValidPort, isValidEmail, validateMailerConfig };
|