Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding feature to whitelist domains and disallow as default behavior #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Email Validation can be configured with more advanced options as an object:
- `blacklist` _(Array)_ Add some email domains you want to blacklist (default is [])
- `allowFreemail` _(Boolean)_ Allow free emails such as @gmail.com, ... (default is false)
- `allowDisposable` _(Boolean)_ Allow disposable emails such as @trashmail.com, ... (default is false)
- `defaultType` _(String)_ Define the default behavior when a domain didn't match any rules, you can choose between valid or invalid (default is valid)

You can for example choose to allow freemails, and add a domain baddomain.com in addition to the preconfigured list

Expand All @@ -99,7 +100,7 @@ const ev = new EmailValidation({ allowFreemail: true, blacklist: ['baddomain.com
// This one should have result.valid = true because we allowed free mails such as gmail.com
ev.check('[email protected]')

// But this one is blacklisted now
// But this one is blacklisted now
ev.check('[email protected]')

```
Expand All @@ -111,6 +112,13 @@ const ev = new EmailValidation({ whitelist: ['gmail.com'] })

```

Or if you want to only accept one single domain and disallow anything else:

```javascript
const ev = new EmailValidation({ whitelist: ['random.com'], defaultType: 'invalid' })

```

You can check some examples in [`example.js`](https://github.com/romainsimon/emailvalid/blob/master/example.js)

#### Updating options on the fly
Expand Down
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const defaultOptions = {
whitelist: [],
blacklist: [],
allowFreemail: false,
allowDisposable: false
allowDisposable: false,
defaultType: 'valid'
}

class EmailValidation {
Expand All @@ -37,11 +38,14 @@ class EmailValidation {
result.email = email.trim().toLowerCase()
result.domain = result.email.split('@').pop()

var whitelisted = false

const type = this.domains[result.domain]
if (type) {
type === 'disposable' && !this.options.allowDisposable && result.errors.push(type)
type === 'freemail' && !this.options.allowFreemail && result.errors.push(type)
type === 'blacklist' && result.errors.push(type)
type === 'whitelist' && (whitelisted = true)
} else {
let smallestDistance = result.domain.length
for (const domain of popularDomains) {
Expand All @@ -54,7 +58,13 @@ class EmailValidation {
}
}

if (!result.errors.length) result.valid = true
if (!result.errors.length) {
if (whitelisted === true | this.options.defaultType === 'valid') {
result.valid = true
} else {
result.errors.push(this.options.defaultType)
}
}
return result
}

Expand Down
16 changes: 16 additions & 0 deletions index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,20 @@ describe('EmailValidation', () => {
expect(result.valid).eq(true)
})
})
describe('Default validation should be configurable', () => {
const ev = new EmailValidation()
it('should say domain random.com is invalid because default validation is not set to accept, normally random.com should have been accepted', () => {
ev.setOptions({ defaultType: 'invalid' })
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(false)
expect(result.errors[0]).eq('invalid')
})
it('should say domain random.com is valid because random.com has been whitelisted despite of default validation is not set to accept', () => {
ev.setOptions({ defaultType: 'invalid', whitelist: ['random.com'] })
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(true)
})
})
})