Skip to content

Commit 2925f5c

Browse files
authored
fix(firestore-send-email): remove .email validation of from field (#2433)
* fix(firestore-send-email): remove .email validation of from field * refactor: reference demo project instead
1 parent 623e438 commit 2925f5c

File tree

6 files changed

+62
-10
lines changed

6 files changed

+62
-10
lines changed

firestore-send-email/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Version 0.2.3
2+
3+
fix: remove strict validation of email "from" field
4+
15
## Version 0.2.2
26

37
fix: fix validation of payloads

firestore-send-email/extension.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
name: firestore-send-email
16-
version: 0.2.2
16+
version: 0.2.3
1717
specVersion: v1beta
1818

1919
displayName: Trigger Email from Firestore

firestore-send-email/functions/__tests__/e2e/sendgrid.test.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ const TEST_COLLECTIONS = ["emailCollection", "emailTemplates"] as const;
6868
beforeAll(() => {
6969
// Initialize with emulator settings
7070
admin.initializeApp({
71-
projectId: "dev-extensions-testing",
72-
databaseURL: "http://localhost:8080?ns=dev-extensions-testing",
71+
projectId: "demo-test",
7372
});
7473

7574
// Point Firestore to the emulator
@@ -141,5 +140,45 @@ const TEST_COLLECTIONS = ["emailCollection", "emailTemplates"] as const;
141140
expect(updatedData?.delivery.info?.rejected).toEqual([]);
142141
expect(updatedData?.delivery.info?.pending).toEqual([]);
143142
});
143+
144+
test("should process an email with friendly name in from field", async () => {
145+
const db = admin.firestore();
146+
147+
const testData = {
148+
message: {
149+
attachments: [],
150+
html: "<p>Test email with friendly name</p>",
151+
text: "Test email with friendly name",
152+
subject: "Test Friendly Name",
153+
from: "Friendly Firebaser test@example.com",
154+
},
155+
to: TEST_EMAIL,
156+
};
157+
158+
// Write the document to the emulator
159+
const docRef = db.collection("emailCollection").doc("test-friendly-name");
160+
await docRef.set(testData);
161+
162+
// Wait a bit for the function to process
163+
await new Promise((resolve) => setTimeout(resolve, 2000));
164+
165+
// Verify the document was updated
166+
const doc = await docRef.get();
167+
const updatedData = doc.data();
168+
169+
// Assert the delivery state was updated to SUCCESS
170+
console.log("updatedData with friendly name", updatedData);
171+
expect(updatedData?.delivery.state).toBe("SUCCESS");
172+
expect(updatedData?.delivery.attempts).toBe(1);
173+
expect(updatedData?.delivery.endTime).toBeDefined();
174+
expect(updatedData?.delivery.error).toBeNull();
175+
176+
// Verify SendGrid specific info
177+
expect(updatedData?.delivery.info).toBeDefined();
178+
expect(updatedData?.delivery.info?.messageId).toBeDefined();
179+
expect(updatedData?.delivery.info?.accepted).toContain(TEST_EMAIL);
180+
expect(updatedData?.delivery.info?.rejected).toEqual([]);
181+
expect(updatedData?.delivery.info?.pending).toEqual([]);
182+
});
144183
}
145184
);

firestore-send-email/functions/__tests__/validation.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ describe("validatePayload", () => {
9191
expect(() => validatePayload(validPayload)).not.toThrow();
9292
});
9393

94+
it("should validate payload with friendly name in from field", () => {
95+
const validPayload = {
96+
to: "test@example.com",
97+
from: "Friendly Firebaser test@example.com",
98+
message: {
99+
subject: "Test Subject",
100+
text: "Test message",
101+
},
102+
};
103+
expect(() => validatePayload(validPayload)).not.toThrow();
104+
});
105+
94106
it("should validate a template payload without html/text fields", () => {
95107
const validPayload = {
96108
to: "test@example.com",

firestore-send-email/functions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"test:local": "concurrently --kill-others \"npm run local:emulator\" \"npm run testIfEmulatorRunning\"",
1515
"test:watch": "concurrently \"npm run local:emulator\" \"jest --watch\"",
1616
"test:coverage": "concurrently --kill-others \"npm run local:emulator\" \"wait-on tcp:4001 && jest --coverage\"",
17-
"test:e2e:sendgrid": "E2E_SENDGRID=true jest __tests__/e2e/sendgrid.test.ts",
17+
"test:e2e:sendgrid": "cd ../../_emulator && firebase emulators:exec --project=demo-test \" cd ../firestore-send-email/functions && E2E_SENDGRID=true jest __tests__/e2e/sendgrid.test.ts\"",
1818
"generate-readme": "firebase ext:info .. --markdown > ../README.md"
1919
},
2020
"keywords": [],

firestore-send-email/functions/src/validation.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@ const templateSchema = z.object({
116116
/**
117117
* Schema for email recipients (single email or array of emails).
118118
*/
119-
const recipientSchema = z.union([
120-
z.string().email(),
121-
z.array(z.string().email()),
122-
]);
119+
const recipientSchema = z.union([z.string(), z.array(z.string())]);
123120

124121
/**
125122
* Schema for arrays of UIDs.
@@ -137,8 +134,8 @@ const payloadSchema = z
137134
toUids: uidArraySchema.optional(),
138135
ccUids: uidArraySchema.optional(),
139136
bccUids: uidArraySchema.optional(),
140-
from: z.string().email().optional(),
141-
replyTo: z.string().email().optional(),
137+
from: z.string().optional(),
138+
replyTo: z.string().optional(),
142139
message: baseMessageSchema.optional(),
143140
template: templateSchema.optional(),
144141
sendGrid: sendGridSchema.optional(),

0 commit comments

Comments
 (0)