1
1
'use strict' ;
2
2
3
- const { ToBulkRecipient, ToEmailAddress, ToCustomHeader} = require ( '../helpers/helpersClasses' ) ;
3
+ const { ToBulkRecipient, ToEmailAddress, ToCustomHeader } = require ( '../helpers/helpersClasses' ) ;
4
4
5
5
const sendResponse = require ( '../sendResponse' ) ;
6
6
const addressResult = require ( '../addressResult' ) ;
@@ -12,17 +12,16 @@ const MaximumRecipientsPerMessage = 50;
12
12
* Used by the SocketLabsClient to conduct basic validation on the message before sending to the Injection API.
13
13
*/
14
14
class SendValidator {
15
-
15
+
16
16
/**
17
17
* Validate the ServerId and Api Key pair prior before sending to the Injection API.
18
18
* @param {number } serverId - Your SocketLabs ServerId number.
19
19
* @param {string } apiKey - Your SocketLabs Injection API key.
20
20
* @returns A SendResponse with the validation results
21
21
*/
22
- validateCredentials ( serverId , apiKey )
23
- {
22
+ validateCredentials ( serverId , apiKey ) {
24
23
var result = new sendResponse ( ) ;
25
-
24
+
26
25
//String given
27
26
if ( typeof apiKey !== 'string' ) {
28
27
result . setResult ( sendResultEnum . AuthenticationValidationFailed ) ;
@@ -33,17 +32,17 @@ class SendValidator {
33
32
result . setResult ( sendResultEnum . AuthenticationValidationFailed ) ;
34
33
return result ;
35
34
}
36
-
35
+
37
36
//int given
38
- if ( typeof serverId !== 'number' ) {
37
+ if ( Number . isInteger ( serverId ) ) {
39
38
result . setResult ( sendResultEnum . AuthenticationValidationFailed ) ;
40
39
return result ;
41
40
}
42
41
43
42
if ( serverId <= 0 ) {
44
43
result . setResult ( sendResultEnum . AuthenticationValidationFailed ) ;
45
44
return result ;
46
- }
45
+ }
47
46
48
47
result . setResult ( sendResultEnum . Success ) ;
49
48
return result ;
@@ -70,7 +69,7 @@ class SendValidator {
70
69
result . setResult ( sendResultEnum . EmailAddressValidationMissingFrom ) ;
71
70
return result ;
72
71
}
73
- if ( ! from . isValid ( ) ) {
72
+ if ( ! from . isValid ( ) ) {
74
73
result . setResult ( sendResultEnum . EmailAddressValidationInvalidFrom ) ;
75
74
return result ;
76
75
}
@@ -95,7 +94,7 @@ class SendValidator {
95
94
}
96
95
}
97
96
98
- // Check email addresses
97
+ // Check email addresses
99
98
return this . validateEmailAddresses ( message . to , message . cc , message . bcc ) ;
100
99
101
100
}
@@ -121,7 +120,7 @@ class SendValidator {
121
120
result . setResult ( sendResultEnum . EmailAddressValidationMissingFrom ) ;
122
121
return result ;
123
122
}
124
- if ( ! from . isValid ( ) ) {
123
+ if ( ! from . isValid ( ) ) {
125
124
result . setResult ( sendResultEnum . EmailAddressValidationInvalidFrom ) ;
126
125
return result ;
127
126
}
@@ -151,7 +150,7 @@ class SendValidator {
151
150
152
151
}
153
152
154
-
153
+
155
154
/**
156
155
* Check if the fromAddress is not empty
157
156
* @param {emailAddress } fromAddress - the from email address
@@ -161,12 +160,12 @@ class SendValidator {
161
160
if ( typeof value === 'undefined' || ! value ) {
162
161
return false ;
163
162
}
164
- if ( value && value === '' ) {
163
+ if ( value && value === '' ) {
165
164
return false ;
166
165
}
167
- return true ;
166
+ return true ;
168
167
}
169
-
168
+
170
169
/**
171
170
* Check if the fromAddress is not empty
172
171
* @param {emailAddress } fromAddress - the from email address
@@ -176,7 +175,7 @@ class SendValidator {
176
175
if ( typeof value === 'undefined' || ! value ) {
177
176
return false ;
178
177
}
179
- if ( value && value . emailAddress === '' ) {
178
+ if ( value && value . emailAddress === '' ) {
180
179
return false ;
181
180
}
182
181
return true ;
@@ -189,16 +188,16 @@ class SendValidator {
189
188
*/
190
189
isValidReplyTo ( value ) {
191
190
if ( typeof value === 'undefined' || ! value ) {
192
- return true ; // undefined is allowed
191
+ return true ; // undefined is allowed
193
192
}
194
193
var replyTo = ToEmailAddress . convert ( value ) ;
195
- if ( replyTo && replyTo . emailAddress === '' && replyTo . friendlyName === '' ) {
194
+ if ( replyTo && replyTo . emailAddress === '' && replyTo . friendlyName === '' ) {
196
195
return true ;
197
196
}
198
- return replyTo . isValid ( ) ;
197
+ return replyTo . isValid ( ) ;
199
198
}
200
199
201
-
200
+
202
201
/**
203
202
* Check if the message has a Message Body
204
203
* If an Api Template is specified it will override the HtmlBody, and/or the textBody.
@@ -209,11 +208,11 @@ class SendValidator {
209
208
* @returns {bool } the validation result
210
209
*/
211
210
hasMessageBody ( apiTemplate , htmlBody , textBody ) {
212
-
211
+
213
212
if ( ( typeof apiTemplate !== 'undefined' || apiTemplate ) &&
214
- ( typeof apiTemplate === 'number' ) &&
213
+ ( typeof apiTemplate === 'number' ) &&
215
214
( apiTemplate > 0 ) ) {
216
- return true ;
215
+ return true ;
217
216
}
218
217
219
218
return ( this . hasValidString ( htmlBody ) || this . hasValidString ( textBody ) ) ;
@@ -224,10 +223,10 @@ class SendValidator {
224
223
* @param {customHeader[] } value - array of customHeader
225
224
* @returns {bool } the validation result
226
225
*/
227
- hasValidCustomHeaders ( value ) {
226
+ hasValidCustomHeaders ( value ) {
228
227
if ( typeof value === 'undefined' || ! value ) {
229
- return true ;
230
- }
228
+ return true ;
229
+ }
231
230
if ( ! Array . isArray ( value ) ) {
232
231
return false ;
233
232
}
@@ -238,7 +237,7 @@ class SendValidator {
238
237
try {
239
238
value . forEach ( element => {
240
239
if ( ! ToCustomHeader . convert ( element ) . isValid ( ) )
241
- return false ;
240
+ return false ;
242
241
} ) ;
243
242
} catch ( error ) {
244
243
return false ;
@@ -276,7 +275,7 @@ class SendValidator {
276
275
result . addressResults = invalidRec ;
277
276
return result ;
278
277
}
279
-
278
+
280
279
result . setResult ( sendResultEnum . Success ) ;
281
280
return result ;
282
281
@@ -292,7 +291,7 @@ class SendValidator {
292
291
293
292
var fullRecipientCount = 0 ;
294
293
if ( ( typeof to !== 'undefined' || to ) && ( Array . isArray ( to ) ) ) {
295
- fullRecipientCount = to . length ;
294
+ fullRecipientCount = to . length ;
296
295
}
297
296
if ( fullRecipientCount <= 0 ) {
298
297
result . setResult ( sendResultEnum . RecipientValidationNoneInMessage ) ;
@@ -302,14 +301,14 @@ class SendValidator {
302
301
result . setResult ( sendResultEnum . RecipientValidationMaxExceeded ) ;
303
302
return result ;
304
303
}
305
-
304
+
306
305
var invalidRec = this . hasInvalidBulkRecipients ( to ) ;
307
- if ( invalidRec != null && invalidRec . length > 0 ) {
306
+ if ( invalidRec != null && invalidRec . length > 0 ) {
308
307
result . setResult ( sendResultEnum . RecipientValidationInvalidRecipients ) ;
309
308
result . addressResults = invalidRec ;
310
309
return result ;
311
310
}
312
-
311
+
313
312
result . setResult ( sendResultEnum . Success ) ;
314
313
return result ;
315
314
}
@@ -334,7 +333,7 @@ class SendValidator {
334
333
if ( ( typeof bcc !== 'undefined' || bcc ) && ( Array . isArray ( bcc ) ) ) {
335
334
recipientCount += bcc . length ;
336
335
}
337
- return recipientCount ;
336
+ return recipientCount ;
338
337
}
339
338
340
339
/**
@@ -348,18 +347,18 @@ class SendValidator {
348
347
var invalid = [ ] ;
349
348
if ( ( typeof to !== 'undefined' || to ) && ( Array . isArray ( to ) ) ) {
350
349
this . findInvalidEmailAddresses ( to ) . forEach ( e => {
351
- invalid . push ( e )
352
- } ) ;
350
+ invalid . push ( e )
351
+ } ) ;
353
352
}
354
353
if ( ( typeof cc !== 'undefined' || cc ) && ( Array . isArray ( cc ) ) ) {
355
354
this . findInvalidEmailAddresses ( cc ) . forEach ( e => {
356
- invalid . push ( e )
357
- } ) ;
355
+ invalid . push ( e )
356
+ } ) ;
358
357
}
359
358
if ( ( typeof bcc !== 'undefined' || bcc ) && ( Array . isArray ( bcc ) ) ) {
360
359
this . findInvalidEmailAddresses ( bcc ) . forEach ( e => {
361
- invalid . push ( e )
362
- } ) ;
360
+ invalid . push ( e )
361
+ } ) ;
363
362
}
364
363
return invalid ;
365
364
}
@@ -386,20 +385,19 @@ class SendValidator {
386
385
try {
387
386
value . forEach ( element => {
388
387
var e = ToEmailAddress . convert ( element ) ;
389
- if ( ! e . isValid ( ) )
390
- {
388
+ if ( ! e . isValid ( ) ) {
391
389
invalid . push ( new addressResult (
392
390
{
393
391
accepted : false ,
394
392
emailAddress : e . emailAddress ,
395
393
errorCode : "InvalidAddress"
396
394
} )
397
395
) ;
398
- }
396
+ }
399
397
} ) ;
400
398
} catch ( error ) {
401
399
return invalid ;
402
- }
400
+ }
403
401
}
404
402
return invalid ;
405
403
}
@@ -415,20 +413,19 @@ class SendValidator {
415
413
try {
416
414
value . forEach ( element => {
417
415
var e = ToBulkRecipient . convert ( element ) ;
418
- if ( ! e . isValid ( ) )
419
- {
420
- invalid . push ( new addressResult (
416
+ if ( ! e . isValid ( ) ) {
417
+ invalid . push ( new addressResult (
421
418
{
422
419
accepted : false ,
423
420
emailAddress : e . emailAddress ,
424
421
errorCode : "InvalidAddress"
425
422
} )
426
423
) ;
427
- }
424
+ }
428
425
} ) ;
429
426
} catch ( error ) {
430
427
return invalid ;
431
- }
428
+ }
432
429
}
433
430
return invalid ;
434
431
}
0 commit comments