| 75 | * with a helpful field name so the error handler maps cleanly to a 400. |
| 76 | */ |
| 77 | export const validateEmailMessage = ( |
| 78 | message: IEmailMessage, |
| 79 | from: string |
| 80 | ): void => { |
| 81 | if (!isValidEmail(message.to)) { |
| 82 | throw ApiErrors.validation( |
| 83 | `Invalid recipient email: ${maskEmailForLogging(message.to)}`, |
| 84 | "to" |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | if (!isValidEmail(from)) { |
| 89 | throw ApiErrors.internal(`Invalid sender email configured: ${from}`); |
| 90 | } |
| 91 | |
| 92 | if (message.subject.trim().length === 0) { |
| 93 | throw ApiErrors.validation("Email subject is required", "subject"); |
| 94 | } |
| 95 | |
| 96 | if (message.html.trim().length === 0) { |
| 97 | throw ApiErrors.validation("Email HTML body is required", "html"); |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | /* |
| 102 | * --------------------------------------------------------------------------- |