-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(isEmail): allow regexp in host_whitelist and host_blacklist (#2494)
* enhance isEmail to have regexp for host_whitelist * update README.md * refactor code * update test name * fix code logic for checkHost --------- Co-authored-by: Rubin Bhandari <[email protected]>
- Loading branch information
1 parent
f54599c
commit 86911d8
Showing
5 changed files
with
56 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function isRegExp(obj) { | ||
return Object.prototype.toString.call(obj) === '[object RegExp]'; | ||
} | ||
|
||
export default function checkHost(host, matches) { | ||
for (let i = 0; i < matches.length; i++) { | ||
let match = matches[i]; | ||
if (host === match || (isRegExp(match) && match.test(host))) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -325,6 +325,25 @@ describe('Validators', () => { | |
}); | ||
}); | ||
|
||
it('should allow regular expressions in the host blacklist of isEmail', () => { | ||
test({ | ||
validator: 'isEmail', | ||
args: [{ | ||
host_blacklist: ['bar.com', 'foo.com', /\.foo\.com$/], | ||
}], | ||
valid: [ | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
], | ||
invalid: [ | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
], | ||
}); | ||
}); | ||
|
||
it('should validate only email addresses with whitelisted domains', () => { | ||
test({ | ||
validator: 'isEmail', | ||
|
@@ -341,6 +360,25 @@ describe('Validators', () => { | |
}); | ||
}); | ||
|
||
it('should allow regular expressions in the host whitelist of isEmail', () => { | ||
test({ | ||
validator: 'isEmail', | ||
args: [{ | ||
host_whitelist: ['bar.com', 'foo.com', /\.foo\.com$/], | ||
}], | ||
valid: [ | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
], | ||
invalid: [ | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
], | ||
}); | ||
}); | ||
|
||
it('should validate URLs', () => { | ||
test({ | ||
validator: 'isURL', | ||
|