Skip to content

Commit b679432

Browse files
authored
📦 NEW: Add validateOrFail method for validating email (#30)
* 👌 IMPROVE: Ignore new location for.engine directory * 📦 NEW: Add validateOrFail method for validating email This makes mail validation easier, so the client does not have to check the mail result and will instead know immediately whether the configured mail is valid.
1 parent 7633e37 commit b679432

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.tmp/**
55

66
.env
7-
test-harness/.engine/**
7+
.engine
88
test-harness/coldbox/**
99
test-harness/docbox/**
1010
test-harness/testbox/**

models/Mail.cfc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,20 @@ component accessors="true" {
195195
);
196196
}
197197

198+
/**
199+
* Run email validation and throw an InvalidMailException if required params are missing.
200+
*/
201+
Mail function validateOrFail(){
202+
if ( NOT this.validate() ){
203+
throw(
204+
type : "InvalidMailException",
205+
message: "One or more required fields are missing.",
206+
detail : "Please check the basic mail fields of To, From, Subject and Body as they are empty. To: #variables.config.to#, From: #variables.config.from#, Subject Len = #variables.config.subject.length()#, Body Len = #variables.config.body.length()#."
207+
);
208+
}
209+
return this;
210+
}
211+
198212
/**
199213
* Validate that the basic fields of from, to, subject, and body are set for sending mail
200214
*/

test-harness/tests/specs/MailTest.cfc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@
6161
expect( mail.validate() ).toBeTrue();
6262
} );
6363

64+
it( "validateOrFail will throw for invalid mail", function() {
65+
66+
expect(function() {
67+
mail.configure(
68+
69+
70+
// OMIT subject... "Oops! DID I DO THAT???"
71+
)
72+
.validateOrFail()
73+
}).toThrow( "InvalidMailException" );
74+
});
75+
76+
it( "validateOrFail won't throw for valid emails", function() {
77+
expect( function() {
78+
mail.configure(
79+
subject = "Hello",
80+
81+
82+
body = "Hello"
83+
).validateOrFail();
84+
}).notToThrow( "InvalidMailException" );
85+
});
86+
6487
it( "can set html types", function(){
6588
mail.configure(
6689
subject = "Hello",

0 commit comments

Comments
 (0)