UNPKG

n4s

Version:

typed schema validation version of enforce

1 lines 10.3 kB
{"version":3,"file":"email.mjs","names":["_typeof","obj"],"sources":["../../../../node_modules/validator/es/lib/isByteLength.js","../../../../node_modules/validator/es/lib/isEmail.js","../../src/exports/email.ts"],"sourcesContent":["function _typeof(obj) { \"@babel/helpers - typeof\"; if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return _typeof(obj); }\n\nimport assertString from './util/assertString';\n/* eslint-disable prefer-rest-params */\n\nexport default function isByteLength(str, options) {\n assertString(str);\n var min;\n var max;\n\n if (_typeof(options) === 'object') {\n min = options.min || 0;\n max = options.max;\n } else {\n // backwards compatibility: isByteLength(str, min [, max])\n min = arguments[1];\n max = arguments[2];\n }\n\n var len = encodeURI(str).split(/%..|./).length - 1;\n return len >= min && (typeof max === 'undefined' || len <= max);\n}","import assertString from './util/assertString';\nimport merge from './util/merge';\nimport isByteLength from './isByteLength';\nimport isFQDN from './isFQDN';\nimport isIP from './isIP';\nvar default_email_options = {\n allow_display_name: false,\n require_display_name: false,\n allow_utf8_local_part: true,\n require_tld: true,\n blacklisted_chars: '',\n ignore_max_length: false,\n host_blacklist: [],\n host_whitelist: []\n};\n/* eslint-disable max-len */\n\n/* eslint-disable no-control-regex */\n\nvar splitNameAddress = /^([^\\x00-\\x1F\\x7F-\\x9F\\cX]+)</i;\nvar emailUserPart = /^[a-z\\d!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]+$/i;\nvar gmailUserPart = /^[a-z\\d]+$/;\nvar quotedEmailUser = /^([\\s\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f\\x21\\x23-\\x5b\\x5d-\\x7e]|(\\\\[\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]))*$/i;\nvar emailUserUtf8Part = /^[a-z\\d!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]+$/i;\nvar quotedEmailUserUtf8 = /^([\\s\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f\\x21\\x23-\\x5b\\x5d-\\x7e\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]|(\\\\[\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]))*$/i;\nvar defaultMaxEmailLength = 254;\n/* eslint-enable max-len */\n\n/* eslint-enable no-control-regex */\n\n/**\n * Validate display name according to the RFC2822: https://tools.ietf.org/html/rfc2822#appendix-A.1.2\n * @param {String} display_name\n */\n\nfunction validateDisplayName(display_name) {\n var display_name_without_quotes = display_name.replace(/^\"(.+)\"$/, '$1'); // display name with only spaces is not valid\n\n if (!display_name_without_quotes.trim()) {\n return false;\n } // check whether display name contains illegal character\n\n\n var contains_illegal = /[\\.\";<>]/.test(display_name_without_quotes);\n\n if (contains_illegal) {\n // if contains illegal characters,\n // must to be enclosed in double-quotes, otherwise it's not a valid display name\n if (display_name_without_quotes === display_name) {\n return false;\n } // the quotes in display name must start with character symbol \\\n\n\n var all_start_with_back_slash = display_name_without_quotes.split('\"').length === display_name_without_quotes.split('\\\\\"').length;\n\n if (!all_start_with_back_slash) {\n return false;\n }\n }\n\n return true;\n}\n\nexport default function isEmail(str, options) {\n assertString(str);\n options = merge(options, default_email_options);\n\n if (options.require_display_name || options.allow_display_name) {\n var display_email = str.match(splitNameAddress);\n\n if (display_email) {\n var display_name = display_email[1]; // Remove display name and angle brackets to get email address\n // Can be done in the regex but will introduce a ReDOS (See #1597 for more info)\n\n str = str.replace(display_name, '').replace(/(^<|>$)/g, ''); // sometimes need to trim the last space to get the display name\n // because there may be a space between display name and email address\n // eg. myname <address@gmail.com>\n // the display name is `myname` instead of `myname `, so need to trim the last space\n\n if (display_name.endsWith(' ')) {\n display_name = display_name.slice(0, -1);\n }\n\n if (!validateDisplayName(display_name)) {\n return false;\n }\n } else if (options.require_display_name) {\n return false;\n }\n }\n\n if (!options.ignore_max_length && str.length > defaultMaxEmailLength) {\n return false;\n }\n\n var parts = str.split('@');\n var domain = parts.pop();\n var lower_domain = domain.toLowerCase();\n\n if (options.host_blacklist.includes(lower_domain)) {\n return false;\n }\n\n if (options.host_whitelist.length > 0 && !options.host_whitelist.includes(lower_domain)) {\n return false;\n }\n\n var user = parts.join('@');\n\n if (options.domain_specific_validation && (lower_domain === 'gmail.com' || lower_domain === 'googlemail.com')) {\n /*\n Previously we removed dots for gmail addresses before validating.\n This was removed because it allows `multiple..dots@gmail.com`\n to be reported as valid, but it is not.\n Gmail only normalizes single dots, removing them from here is pointless,\n should be done in normalizeEmail\n */\n user = user.toLowerCase(); // Removing sub-address from username before gmail validation\n\n var username = user.split('+')[0]; // Dots are not included in gmail length restriction\n\n if (!isByteLength(username.replace(/\\./g, ''), {\n min: 6,\n max: 30\n })) {\n return false;\n }\n\n var _user_parts = username.split('.');\n\n for (var i = 0; i < _user_parts.length; i++) {\n if (!gmailUserPart.test(_user_parts[i])) {\n return false;\n }\n }\n }\n\n if (options.ignore_max_length === false && (!isByteLength(user, {\n max: 64\n }) || !isByteLength(domain, {\n max: 254\n }))) {\n return false;\n }\n\n if (!isFQDN(domain, {\n require_tld: options.require_tld,\n ignore_max_length: options.ignore_max_length\n })) {\n if (!options.allow_ip_domain) {\n return false;\n }\n\n if (!isIP(domain)) {\n if (!domain.startsWith('[') || !domain.endsWith(']')) {\n return false;\n }\n\n var noBracketdomain = domain.slice(1, -1);\n\n if (noBracketdomain.length === 0 || !isIP(noBracketdomain)) {\n return false;\n }\n }\n }\n\n if (user[0] === '\"') {\n user = user.slice(1, user.length - 1);\n return options.allow_utf8_local_part ? quotedEmailUserUtf8.test(user) : quotedEmailUser.test(user);\n }\n\n var pattern = options.allow_utf8_local_part ? emailUserUtf8Part : emailUserPart;\n var user_parts = user.split('.');\n\n for (var _i = 0; _i < user_parts.length; _i++) {\n if (!pattern.test(user_parts[_i])) {\n return false;\n }\n }\n\n if (options.blacklisted_chars) {\n if (user.search(new RegExp(\"[\".concat(options.blacklisted_chars, \"]+\"), 'g')) !== -1) return false;\n }\n\n return true;\n}","import isEmail from 'validator/es/lib/isEmail';\n\nimport { enforce } from '../n4s';\n\nenforce.extend({ isEmail });\n\ndeclare global {\n namespace n4s {\n interface EnforceMatchers {\n isEmail: typeof isEmail;\n }\n }\n}\n"],"x_google_ignoreList":[0,1],"mappings":";;;;;AAAA,SAAS,QAAQ,KAAK;AAAE;AAA2B,KAAI,OAAO,WAAW,cAAc,OAAO,OAAO,aAAa,SAAY,WAAU,SAASA,UAAQ,OAAK;AAAE,SAAO,OAAOC;;KAAiB,WAAU,SAASD,UAAQ,OAAK;AAAE,SAAOC,SAAO,OAAO,WAAW,cAAcA,MAAI,gBAAgB,UAAUA,UAAQ,OAAO,YAAY,WAAW,OAAOA;;AAAU,QAAO,QAAQ,IAAI;;AAKvX,SAAwB,aAAa,KAAK,SAAS;AACjD,cAAa,IAAI;CACjB,IAAI;CACJ,IAAI;AAEJ,KAAI,QAAQ,QAAQ,KAAK,UAAU;AACjC,QAAM,QAAQ,OAAO;AACrB,QAAM,QAAQ;QACT;AAEL,QAAM,UAAU;AAChB,QAAM,UAAU;;CAGlB,IAAI,MAAM,UAAU,IAAI,CAAC,MAAM,QAAQ,CAAC,SAAS;AACjD,QAAO,OAAO,QAAQ,OAAO,QAAQ,eAAe,OAAO;;;;;ACf7D,IAAI,wBAAwB;CAC1B,oBAAoB;CACpB,sBAAsB;CACtB,uBAAuB;CACvB,aAAa;CACb,mBAAmB;CACnB,mBAAmB;CACnB,gBAAgB,EAAE;CAClB,gBAAgB,EAAE;CACnB;AAKD,IAAI,mBAAmB;AACvB,IAAI,gBAAgB;AACpB,IAAI,gBAAgB;AACpB,IAAI,kBAAkB;AACtB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