Fix critical type safety and validation issues

Issue 2 - Type Safety:
- Remove dangerous 'as any' casts in sendEmail function
- Use proper typing for payload.create() calls
- Maintain type safety throughout email creation process

Issue 3 - Email Validation:
- Implement RFC 5322 compliant email regex
- Add comprehensive validation for common invalid patterns
- Check for consecutive dots, invalid domain formats
- Prevent emails like 'test@.com' and 'test@domain.'

Issue 4 - Error Message Logic:
- Add contextual error messages for template vs direct email modes
- Distinguish between template rendering failures and missing direct email content
- Provide clearer guidance to developers on what went wrong

Additional fixes:
- Update imports to use generated Email type instead of BaseEmailData
- Maintain compatibility with updated sendEmail interface

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-13 21:57:02 +02:00
parent 45559804b0
commit 6e4f754306
4 changed files with 33 additions and 12 deletions

View File

@@ -66,8 +66,17 @@ export const sendEmail = async <T extends Email = Email>(
throw new Error('Field "to" is required for sending emails')
}
if (!emailData.subject || !emailData.html) {
throw new Error('Fields "subject" and "html" are required when not using a template')
// Validate required fields based on whether template was used
if (options.template) {
// When using template, subject and html should have been set by renderTemplate
if (!emailData.subject || !emailData.html) {
throw new Error(`Template rendering failed: template "${options.template.slug}" did not provide required subject and html content`)
}
} else {
// When not using template, user must provide subject and html directly
if (!emailData.subject || !emailData.html) {
throw new Error('Fields "subject" and "html" are required when sending direct emails without a template')
}
}
// Process email addresses using shared validation (handle null values)
@@ -81,13 +90,13 @@ export const sendEmail = async <T extends Email = Email>(
emailData.bcc = parseAndValidateEmails(emailData.bcc as string | string[])
}
// Create the email in the collection
// Create the email in the collection with proper typing
const email = await payload.create({
collection: collectionSlug as any,
data: emailData as any
collection: collectionSlug,
data: emailData
})
return email as unknown as T
return email as T
}
export default sendEmail