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

new validator: isMailtoURI, validate the mailto link URI format #2188

Merged
merged 8 commits into from
Mar 27, 2023
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ Validator | Description
**isLuhnNumber(str)** | check if the string passes the [Luhn algorithm check](https://en.wikipedia.org/wiki/Luhn_algorithm).
**isMACAddress(str [, options])** | check if the string is a MAC address.<br/><br/>`options` is an object which defaults to `{ no_separators: false }`. If `no_separators` is true, the validator will allow MAC addresses without separators. Also, it allows the use of hyphens, spaces or dots e.g. '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'. The options also allow a `eui` property to specify if it needs to be validated against EUI-48 or EUI-64. The accepted values of `eui` are: 48, 64.
**isMagnetURI(str)** | check if the string is a [Magnet URI format][Magnet URI Format].
**isMailtoURI(str, [, options])** | check if the string is a [Magnet URI format][Mailto URI Format].<br/><br/>`options` is an object of validating emails inside the URI (check `isEmail`s options for details).
**isMD5(str)** | check if the string is a MD5 hash.<br/><br/>Please note that you can also use the `isHash(str, 'md5')` function. Keep in mind that MD5 has some collision weaknesses compared to other algorithms (e.g., SHA).
**isMimeType(str)** | check if the string matches to a valid [MIME type][MIME Type] format.
**isMobilePhone(str [, locale [, options]])** | check if the string is a mobile phone number,<br/><br/>`locale` is either an array of locales (e.g. `['sk-SK', 'sr-RS']`) OR one of `['am-Am', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-EH', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-PS', 'ar-SA', 'ar-SY', 'ar-TN', 'ar-YE', 'az-AZ', 'az-LB', 'az-LY', 'be-BY', 'bg-BG', 'bn-BD', 'bs-BA', 'ca-AD', 'cs-CZ', 'da-DK', 'de-AT', 'de-CH', 'de-DE', 'de-LU', 'dv-MV', 'dz-BT', 'el-CY', 'el-GR', 'en-AG', 'en-AI', 'en-AU', 'en-BM', 'en-BS', 'en-BW', 'en-CA', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-IE', 'en-IN', 'en-JM', 'en-KE', 'en-KI', 'en-KN', 'en-LS', 'en-MO', 'en-MT', 'en-MU', 'en-NG', 'en-NZ', 'en-PG', 'en-PH', 'en-PK', 'en-RW', 'en-SG', 'en-SL', 'en-SS', 'en-TZ', 'en-UG', 'en-US', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-CU', 'es-DO', 'es-EC', 'es-ES', 'es-HN', 'es-MX', 'es-NI', 'es-PA', 'es-PE', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-AF', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-BF', 'fr-BJ', 'fr-CD', 'fr-CF', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', 'ga-IE', 'he-IL', 'hu-HU', 'id-ID', 'ir-IR', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'ky-KG', 'lt-LT', 'mg-MG', 'mn-MN', 'ms-MY', 'my-MM', 'mz-MZ', 'nb-NO', 'ne-NP', 'nl-AW', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-AO', 'pt-BR', 'pt-PT', 'ro-Md', 'ro-RO', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'so-SO', 'sq-AL', 'sr-RS', 'sv-SE', 'tg-TJ', 'th-TH', 'tk-TM', 'tr-TR', 'uk-UA', 'uz-UZ', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-MO', 'zh-TW']` OR defaults to `'any'`. If 'any' or a falsey value is used, function will check if any of the locales match).<br/><br/>`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`. Locale list is `validator.isMobilePhoneLocales`.
Expand Down Expand Up @@ -301,6 +302,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[ISSN]: https://en.wikipedia.org/wiki/International_Standard_Serial_Number
[Luhn Check]: https://en.wikipedia.org/wiki/Luhn_algorithm
[Magnet URI Format]: https://en.wikipedia.org/wiki/Magnet_URI_scheme
[Mailto URI Format]: https://en.wikipedia.org/wiki/Mailto
[MIME Type]: https://en.wikipedia.org/wiki/Media_type
[mongoid]: http://docs.mongodb.org/manual/reference/object-id/
[RFC 3339]: https://tools.ietf.org/html/rfc3339
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import isBase58 from './lib/isBase58';
import isBase64 from './lib/isBase64';
import isDataURI from './lib/isDataURI';
import isMagnetURI from './lib/isMagnetURI';
import isMailtoURI from './lib/isMailtoURI';

import isMimeType from './lib/isMimeType';

Expand Down Expand Up @@ -213,6 +214,7 @@ const validator = {
isBase64,
isDataURI,
isMagnetURI,
isMailtoURI,
isMimeType,
isLatLong,
ltrim,
Expand Down
67 changes: 67 additions & 0 deletions src/lib/isMailtoURI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import trim from './trim';
import isEmail from './isEmail';
import assertString from './util/assertString';

function parseMailtoQueryString(queryString) {
const allowedParams = new Set(['subject', 'body', 'cc', 'bcc']),
query = { cc: '', bcc: '' };
let isParseFailed = false;

const queryParams = queryString.split('&');

if (queryParams.length > 4) {
return false;
}

for (const q of queryParams) {
const [key, value] = q.split('=');

// checked for invalid and duplicated query params
if (key && !allowedParams.has(key)) {
isParseFailed = true;
break;
}

if (value && (key === 'cc' || key === 'bcc')) {
query[key] = value;
}

if (key) {
allowedParams.delete(key);
}
}

return isParseFailed ? false : query;
}

export default function isMailtoURI(url, options) {
assertString(url);

if (url.indexOf('mailto:') !== 0) {
return false;
}

const [to = '', queryString = ''] = url.replace('mailto:', '').split('?');

if (!to && !queryString) {
return true;
}

const query = parseMailtoQueryString(queryString);

if (!query) {
return false;
}

return `${to},${query.cc},${query.bcc}`
.split(',')
.every((email) => {
email = trim(email, ' ');

if (email) {
return isEmail(email, options);
}

return true;
});
}
51 changes: 51 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14179,4 +14179,55 @@ describe('Validators', () => {
],
});
});
it('should validate mailto URI', () => {
test({
validator: 'isMailtoURI',
valid: [
'mailto:?subject=something&[email protected]',
'mailto:?subject=something&[email protected],[email protected],',
'mailto:?subject=something&[email protected]',
'mailto:?subject=something&[email protected],[email protected]',
'mailto:[email protected],[email protected]',
'mailto:[email protected],[email protected]',
'mailto:[email protected]',
'mailto:[email protected]',
'mailto:?subject=something&body=something else',
'mailto:?subject=something&body=something else&[email protected],[email protected]',
'mailto:?subject=something&body=something else&[email protected],[email protected]',
'mailto:?subject=something&body=something else&[email protected]&[email protected],[email protected]',
'mailto:[email protected]',
'mailto:[email protected]?',
'mailto:[email protected]?subject=something',
'mailto:[email protected]?subject=something&[email protected]',
'mailto:[email protected]?subject=something&[email protected],[email protected],',
'mailto:[email protected]?subject=something&[email protected]',
'mailto:[email protected]?subject=something&[email protected],[email protected]',
'mailto:[email protected][email protected],[email protected]',
'mailto:[email protected][email protected],[email protected]',
'mailto:[email protected][email protected]',
'mailto:[email protected][email protected]&',
'mailto:[email protected]?subject=something&body=something else',
'mailto:[email protected]?subject=something&body=something else&[email protected],[email protected]',
'mailto:[email protected]?subject=something&body=something else&[email protected],[email protected]',
'mailto:[email protected]?subject=something&body=something else&[email protected]&[email protected],[email protected]',
'mailto:',
],
invalid: [
'',
'somthing',
'[email protected]',
'mailto:?subject=okay&subject=444',
'mailto:?subject=something&wrong=888',
'mailto:somename@gmail.com',
'mailto:[email protected]?cc=somename@gmail.com',
'mailto:[email protected]?bcc=somename@gmail.com',
'mailto:[email protected]?bcc=somename@gmail.com&bcc',
'mailto:[email protected]?subject=anything&body=nothing&cc=&bcc=&key=',
'mailto:[email protected]?cc=somename',
'mailto:somename',
'mailto:[email protected]?subject=something&body=something else&[email protected]&[email protected],[email protected]&',
'mailto:?subject=something&body=something else&[email protected]&[email protected],[email protected]&',
],
});
});
});