mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 00:03:23 +00:00
Add validation for beforeSend hook to ensure required properties remain intact
- Validate that 'from' field is not removed - Validate that 'to' field is not removed or emptied - Validate that 'subject' field is not removed - Validate that at least 'html' or 'text' content exists - Throw clear error messages if validation fails - Bump version to 0.1.21
This commit is contained in:
@@ -285,6 +285,20 @@ export class MailingService implements IMailingService {
|
||||
if (this.config.beforeSend) {
|
||||
try {
|
||||
mailOptions = await this.config.beforeSend(mailOptions, email)
|
||||
|
||||
// Validate required properties remain intact after hook execution
|
||||
if (!mailOptions.from) {
|
||||
throw new Error('beforeSend hook must not remove the "from" property')
|
||||
}
|
||||
if (!mailOptions.to || (Array.isArray(mailOptions.to) && mailOptions.to.length === 0)) {
|
||||
throw new Error('beforeSend hook must not remove or empty the "to" property')
|
||||
}
|
||||
if (!mailOptions.subject) {
|
||||
throw new Error('beforeSend hook must not remove the "subject" property')
|
||||
}
|
||||
if (!mailOptions.html && !mailOptions.text) {
|
||||
throw new Error('beforeSend hook must not remove both "html" and "text" properties')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error in beforeSend hook:', error)
|
||||
throw new Error(`beforeSend hook failed: ${error instanceof Error ? error.message : 'Unknown error'}`)
|
||||
|
||||
Reference in New Issue
Block a user