diff --git a/src/index.ts b/src/index.ts index 3dafbd3..7fc9cc3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) } diff --git a/src/options/options.ts b/src/options/options.ts index b74b21d..0bc77bc 100644 --- a/src/options/options.ts +++ b/src/options/options.ts @@ -17,8 +17,12 @@ type Options = { validateSMTP: boolean } -export type ValidatorOptions = Partial & { email: string } -type ValidatorOptionsFinal = Options & { email: string } +type MailCheckOptions = { + additionalTopLevelDomains?: string[] +} + +export type ValidatorOptions = Partial & { email: string } & MailCheckOptions +type ValidatorOptionsFinal = Options & { email: string } & MailCheckOptions export function getOptions( emailOrOptions: string | ValidatorOptions diff --git a/src/typo/typo.ts b/src/typo/typo.ts index a9cac36..f864b49 100644 --- a/src/typo/typo.ts +++ b/src/typo/typo.ts @@ -6,10 +6,16 @@ type TypoSuggestion = { full: string } -export const checkTypo = async (email: string): Promise => +export const checkTypo = async (email: string, additionalTLDs?: string[]): Promise => 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}`) }, @@ -17,4 +23,4 @@ export const checkTypo = async (email: string): Promise => r() }, }) - ) + }) diff --git a/test/__snapshots__/index.test.ts.snap b/test/__snapshots__/index.test.ts.snap index 393b996..3ca25c9 100644 --- a/test/__snapshots__/index.test.ts.snap +++ b/test/__snapshots__/index.test.ts.snap @@ -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, + }, + }, +} +`; diff --git a/test/index.test.ts b/test/index.test.ts index 061e69b..0e52e42 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -120,4 +120,19 @@ describe('validation tests', () => { }, elevenSeconds ) + + it( + 'passes with custom TLD', + async () => { + const res = await validate({ + email: 'info@utob.ir', + validateSMTP: false, + additionalTopLevelDomains: ['ir'] + }) + expect(res.valid).toBe(true) + expect(every(values(res.validators), x => x && x.valid)).toBe(true) + expect(res).toMatchSnapshot() + }, + elevenSeconds + ) })