Catch some common e-mail address formatting errors

master
Sven Slootweg 4 years ago
parent 3d2fdce169
commit 47e545a8d0

@ -3,11 +3,16 @@
const ValidationError = require("@validatem/error"); const ValidationError = require("@validatem/error");
const isString = require("@validatem/is-string"); const isString = require("@validatem/is-string");
let commonErrors = [
/^mailto:.+$/, // mailto:email@example.com
/^.+<[^>]+>$/, // Real Name <email@example.com>
];
module.exports = [ module.exports = [
isString, isString,
function isEmailAddress(value) { function isEmailAddress(value) {
// TODO: Include explanation in README as to how this really *is* the correct check in practice, and that any further validation should be done by actually trying to send an e-mail // TODO: Include explanation in README as to how checking for the presence of '@' really *is* the correct check in practice, and that any further validation should be done by actually trying to send an e-mail
if (!value.includes("@")) { if (!value.includes("@") || commonErrors.some((errorRegex) => errorRegex.test(value))) {
throw new ValidationError(`Must be an e-mail address`); throw new ValidationError(`Must be an e-mail address`);
} }
} }

Loading…
Cancel
Save