Skip to content

Commit

Permalink
feat: custom stringarray validator
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitij-k-osmosys committed Sep 11, 2024
1 parent b7ea5d3 commit 24719fd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { registerDecorator, ValidationOptions, ValidationArguments } from 'class-validator';

export function IsStringOrStringArray(validationOptions?: ValidationOptions) {
return function (object: NonNullable<unknown>, propertyName: string) {
registerDecorator({
name: 'isStringOrStringArray',
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
validator: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
validate(value: any, args: ValidationArguments) {
if (typeof value === 'string') {
return true; // Valid if it's a string
}

if (Array.isArray(value) && value.every((item) => typeof item === 'string')) {
return true; // Valid if it's an array of strings
}

return false; // Invalid otherwise
},
defaultMessage(args: ValidationArguments) {
return `${args.property} must be either a string or an array of strings`;
},
},
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,29 @@ import {
AttachmentValidation,
CreateNotificationAttachmentDto,
} from '../create-notification-attachment.dto';
import { IsStringOrStringArray } from 'src/common/decorators/is-string-or-stringarray.decorator';

export class AwsSesDataDto {
@IsNotEmpty()
@IsString()
from: string | string[];
from: string;

@IsNotEmpty()
@IsString()
@IsStringOrStringArray({
message: 'The "to" field must be either a string or an array of strings',
})
to: string | string[];

@IsOptional()
@IsString()
@IsStringOrStringArray({
message: 'The "cc" field must be either a string or an array of strings',
})
cc?: string | string[];

@IsOptional()
@IsString()
@IsStringOrStringArray({
message: 'The "bcc" field must be either a string or an array of strings',
})
bcc?: string | string[];

@IsNotEmpty()
Expand All @@ -35,7 +42,9 @@ export class AwsSesDataDto {
html: string;

@IsOptional()
@IsString()
@IsStringOrStringArray({
message: 'The "replyTo" field must be either a string or an array of strings',
})
replyTo?: string | string[];

@IsOptional()
Expand Down
29 changes: 21 additions & 8 deletions apps/api/src/modules/providers/aws-ses/aws-ses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { Attachment } from 'nodemailer/lib/mailer';

export interface AwsSesData {
from: string;
to: string;
cc?: string;
bcc?: string;
to: string | string[];
cc?: string | string[];
bcc?: string | string[];
subject: string;
text?: string;
html?: string;
replyTo?: string;
replyTo?: string | string[];
attachment?: Attachment[] | undefined;
}

Expand Down Expand Up @@ -60,13 +60,13 @@ export class AwsSesService {
// Prepare mail option parameters
const mailOptions = {
from: formattedData.from,
to: formattedData.to.split(','),
to: this.normalizeEmails(formattedData.to),
subject: formattedData.subject,
text: formattedData.text,
html: formattedData.html,
cc: formattedData.cc?.split(',') || [],
bcc: formattedData.bcc?.split(',') || [],
replyTo: formattedData.replyTo?.split(',') || [],
cc: formattedData.cc ? this.normalizeEmails(formattedData.cc) : [],
bcc: formattedData.bcc ? this.normalizeEmails(formattedData.bcc) : [],
replyTo: formattedData.replyTo ? this.normalizeEmails(formattedData.replyTo) : [],
attachments: formattedData.attachment,
};

Expand All @@ -86,6 +86,19 @@ export class AwsSesService {
}
}

normalizeEmails(emails: string | string[]): string[] {
if (typeof emails === 'string') {
// Split comma-separated values, trim each email, and filter out any empty strings
return emails
.split(',')
.map((email) => email.trim())
.filter((email) => email !== '');
}

// If it's already an array, return the array with each email trimmed
return emails.map((email) => email.trim());
}

async formatNotificationData(
notificationData: Record<string, unknown>,
): Promise<Record<string, unknown>> {
Expand Down

0 comments on commit 24719fd

Please sign in to comment.