Skip to content

Commit

Permalink
Merge pull request #32 from information-security/master
Browse files Browse the repository at this point in the history
Added `additionalTopLevelDomains` option that enables support for cus…
  • Loading branch information
mfbx9da4 authored Aug 1, 2021
2 parents 1b744be + 5d53264 commit 7f34d7b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function validate(
}

if (options.validateTypo) {
const typoResponse = await checkTypo(email)
const typoResponse = await checkTypo(email, options.additionalTopLevelDomains)
if (typoResponse) return createOutput('typo', typoResponse)
}

Expand Down
8 changes: 6 additions & 2 deletions src/options/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ type Options = {
validateSMTP: boolean
}

export type ValidatorOptions = Partial<Options> & { email: string }
type ValidatorOptionsFinal = Options & { email: string }
type MailCheckOptions = {
additionalTopLevelDomains?: string[]
}

export type ValidatorOptions = Partial<Options> & { email: string } & MailCheckOptions
type ValidatorOptionsFinal = Options & { email: string } & MailCheckOptions

export function getOptions(
emailOrOptions: string | ValidatorOptions
Expand Down
10 changes: 8 additions & 2 deletions src/typo/typo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ type TypoSuggestion = {
full: string
}

export const checkTypo = async (email: string): Promise<string | undefined> =>
export const checkTypo = async (email: string, additionalTLDs?: string[]): Promise<string | undefined> =>
new Promise(r =>
{
let topLevelDomains = undefined
if (additionalTLDs && additionalTLDs.length > 0) {
topLevelDomains = [...mailCheck.defaultTopLevelDomains, ...additionalTLDs]
}
mailCheck.run({
email,
topLevelDomains: topLevelDomains,
suggested: (suggestion: TypoSuggestion) => {
r(`Likely typo, suggested email: ${suggestion.full}`)
},
empty: function() {
r()
},
})
)
})
23 changes: 23 additions & 0 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,26 @@ Object {
},
}
`;

exports[`validation tests passes with custom TLD 1`] = `
Object {
"valid": true,
"validators": Object {
"disposable": Object {
"valid": true,
},
"mx": Object {
"valid": true,
},
"regex": Object {
"valid": true,
},
"smtp": Object {
"valid": true,
},
"typo": Object {
"valid": true,
},
},
}
`;
15 changes: 15 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,19 @@ describe('validation tests', () => {
},
elevenSeconds
)

it(
'passes with custom TLD',
async () => {
const res = await validate({
email: '[email protected]',
validateSMTP: false,
additionalTopLevelDomains: ['ir']
})
expect(res.valid).toBe(true)
expect(every(values(res.validators), x => x && x.valid)).toBe(true)
expect(res).toMatchSnapshot()
},
elevenSeconds
)
})

0 comments on commit 7f34d7b

Please sign in to comment.