Utilities to validate/ensure emails against a whitelist. 👮📧
$ npm install furey/email-whitelist#semver:^v1
// example.js
const { isSafeEmail, getSafeEmail } = require('email-whitelist')
/**
* Assuming the following `process.env` variables are set…
*
* - EMAIL_WHITELIST=me@personal-domain.com,@work-domain.com
* - EMAIL_DEFAULT=me@work-domain.com
*
* */
console.log(isSafeEmail('me@personal-domain.com')) // true
console.log(isSafeEmail('other@work-domain.com')) // true
console.log(isSafeEmail('someone-else@other-domain.com')) // false
console.log(getSafeEmail('me@personal-domain.com')) // 'me@personal-domain.com'
console.log(getSafeEmail('other@work-domain.com')) // 'other@work-domain.com'
console.log(getSafeEmail('someone-else@other-domain.com')) // 'me@work-domain.com'
$ node example.js
true
true
false
me@personal-domain.com
other@work-domain.com
me@work-domain.com
Returns whether email
validates against the set EMAIL_WHITELIST
.
Returns email
if isSafeEmail(email)
, otherwise returns EMAIL_DEFAULT
.
The comma-delimited list of values to test against, e.g.:
EMAIL_WHITELIST=me@personal-domain.com,@work-domain.com
EMAIL_WHITELIST
values should be complete email addresses…
account@domain.com
…and/or whole domains:
@domain.com
The optional default value to return from getSafeEmail()
when an email fails validation, e.g.:
EMAIL_DEFAULT=me@work-domain.com
$ npm ci
$ npm run test
ISC License
Copyright (c) 2019, James Furey
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.