Skip to content

Commit 7f34d7b

Browse files
authored
Merge pull request #32 from information-security/master
Added `additionalTopLevelDomains` option that enables support for cus…
2 parents 1b744be + 5d53264 commit 7f34d7b

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function validate(
1919
}
2020

2121
if (options.validateTypo) {
22-
const typoResponse = await checkTypo(email)
22+
const typoResponse = await checkTypo(email, options.additionalTopLevelDomains)
2323
if (typoResponse) return createOutput('typo', typoResponse)
2424
}
2525

src/options/options.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ type Options = {
1717
validateSMTP: boolean
1818
}
1919

20-
export type ValidatorOptions = Partial<Options> & { email: string }
21-
type ValidatorOptionsFinal = Options & { email: string }
20+
type MailCheckOptions = {
21+
additionalTopLevelDomains?: string[]
22+
}
23+
24+
export type ValidatorOptions = Partial<Options> & { email: string } & MailCheckOptions
25+
type ValidatorOptionsFinal = Options & { email: string } & MailCheckOptions
2226

2327
export function getOptions(
2428
emailOrOptions: string | ValidatorOptions

src/typo/typo.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ type TypoSuggestion = {
66
full: string
77
}
88

9-
export const checkTypo = async (email: string): Promise<string | undefined> =>
9+
export const checkTypo = async (email: string, additionalTLDs?: string[]): Promise<string | undefined> =>
1010
new Promise(r =>
11+
{
12+
let topLevelDomains = undefined
13+
if (additionalTLDs && additionalTLDs.length > 0) {
14+
topLevelDomains = [...mailCheck.defaultTopLevelDomains, ...additionalTLDs]
15+
}
1116
mailCheck.run({
1217
email,
18+
topLevelDomains: topLevelDomains,
1319
suggested: (suggestion: TypoSuggestion) => {
1420
r(`Likely typo, suggested email: ${suggestion.full}`)
1521
},
1622
empty: function() {
1723
r()
1824
},
1925
})
20-
)
26+
})

test/__snapshots__/index.test.ts.snap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,26 @@ Object {
316316
},
317317
}
318318
`;
319+
320+
exports[`validation tests passes with custom TLD 1`] = `
321+
Object {
322+
"valid": true,
323+
"validators": Object {
324+
"disposable": Object {
325+
"valid": true,
326+
},
327+
"mx": Object {
328+
"valid": true,
329+
},
330+
"regex": Object {
331+
"valid": true,
332+
},
333+
"smtp": Object {
334+
"valid": true,
335+
},
336+
"typo": Object {
337+
"valid": true,
338+
},
339+
},
340+
}
341+
`;

test/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,19 @@ describe('validation tests', () => {
120120
},
121121
elevenSeconds
122122
)
123+
124+
it(
125+
'passes with custom TLD',
126+
async () => {
127+
const res = await validate({
128+
129+
validateSMTP: false,
130+
additionalTopLevelDomains: ['ir']
131+
})
132+
expect(res.valid).toBe(true)
133+
expect(every(values(res.validators), x => x && x.valid)).toBe(true)
134+
expect(res).toMatchSnapshot()
135+
},
136+
elevenSeconds
137+
)
123138
})

0 commit comments

Comments
 (0)