Skip to content

Commit

Permalink
Update email validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsle committed Oct 2, 2024
1 parent f98c812 commit a085ed1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
9 changes: 6 additions & 3 deletions packages/teams-js/src/internal/emailAddressValidation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export function validateEmailAddress(emailString: string): void {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(emailString)) {
export function validateEmailAddress(emailString: string | null | undefined): void {
const emailIsEmptyOrUndefined = emailString ? emailString.length <= 0 : true;
const atIndex = emailString?.indexOf('@');
const periodIndexAfterAt = emailString?.indexOf('.', atIndex);

if (emailIsEmptyOrUndefined || atIndex === -1 || periodIndexAfterAt === -1) {
throw new Error('Input email address does not have the correct format.');
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { validateEmailAddress } from '../../src/internal/emailAddressValidation';

describe('emailAddressValidation', () => {
const invalidEmails = ['@@domain.com', 'firstname lastname@domain.com', 'name@domain'];
const invalidEmails = ['domain.com', 'name.domain@com', 'name@domain', '', null];
invalidEmails.forEach((invalidEmail) => {
it('should throw errors for invalid email addresses', () => {
expect(() => validateEmailAddress(invalidEmail)).toThrow('Input email address does not have the correct format.');
Expand All @@ -16,8 +16,9 @@ describe('emailAddressValidation', () => {
validEmails.forEach((validEmail) => {
it('should not throw errors for valid email addresses', () => {
expect(() => validateEmailAddress(validEmail)).not.toThrow(
'Input email address does not have the correct format.',
'Input email address does not have the correct format.'
);
});
});
});

2 changes: 1 addition & 1 deletion packages/teams-js/test/public/emailAddress.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EmailAddress } from '../../src/public';

describe('emailAddress', () => {
const invalidEmails = ['@@domain.com', 'firstname lastname@domain.com', 'name@domain'];
const invalidEmails = ['domain.com', 'name.domain@com', 'name@domain'];
invalidEmails.forEach((invalidEmail) => {
it('should throw errors for invalid email addresses', () => {
expect(() => new EmailAddress(invalidEmail)).toThrowError(
Expand Down

0 comments on commit a085ed1

Please sign in to comment.