* @returns {{ isValid: boolean, validationError: (string | undefined) }} an object specifying whether this message is validly formatted, and the first validation error if it is not.
* @returns {{ isValid: boolean, validationError: (string | undefined) }} an object specifying whether this message is validly formatted, and the first validation error if it is not.
*/
*/
checkValidity() {
checkValidity() {
if (typeof this.header.from !== 'string' &&
if (typeof this.header.from !== 'string' &&
Array.isArray(this.header.from) === false) {
Array.isArray(this.header.from) === false) {
return {
return {
isValid: false,
isValid: false,
validationError: 'Message must have a `from` header',
validationError: 'Message must have a `from` header',
};
};
}
}
if (typeof this.header.to !== 'string' &&
if (typeof this.header.to !== 'string' &&
Array.isArray(this.header.to) === false &&
Array.isArray(this.header.to) === false &&
typeof this.header.cc !== 'string' &&
typeof this.header.cc !== 'string' &&
Array.isArray(this.header.cc) === false &&
Array.isArray(this.header.cc) === false &&
typeof this.header.bcc !== 'string' &&
typeof this.header.bcc !== 'string' &&
Array.isArray(this.header.bcc) === false) {
Array.isArray(this.header.bcc) === false) {
return {
return {
isValid: false,
isValid: false,
validationError: 'Message must have at least one `to`, `cc`, or `bcc` header',
validationError: 'Message must have at least one `to`, `cc`, or `bcc` header',
};
};
}
}
if (this.attachments.length > 0) {
if (this.attachments.length > 0) {
const failed = [];
const failed = [];
this.attachments.forEach((attachment) => {
this.attachments.forEach((attachment) => {
if (attachment.path) {
if (attachment.path) {
if (fs.existsSync(attachment.path) === false) {
if (existsSync(attachment.path) === false) {
failed.push(`${attachment.path} does not exist`);
failed.push(`${attachment.path} does not exist`);
}
}
}
}
else if (attachment.stream) {
else if (attachment.stream) {
if (!attachment.stream.readable) {
if (!attachment.stream.readable) {
failed.push('attachment stream is not readable');
failed.push('attachment stream is not readable');
}
}
}
}
else if (!attachment.data) {
else if (!attachment.data) {
failed.push('attachment has no data associated with it');
failed.push('attachment has no data associated with it');
* @deprecated does not conform to the `errback` style followed by the rest of the library, and will be removed in the next major version. use `checkValidity` instead.
* @deprecated does not conform to the `errback` style followed by the rest of the library, and will be removed in the next major version. use `checkValidity` instead.