Skip to content

Commit

Permalink
Merge branch 'validatorjs:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulSingh044 authored Nov 23, 2024
2 parents 6e05f36 + 86911d8 commit 546adca
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 34 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Validator | Description
**isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.<br/><br/>`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`.<br/><br/>`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa', 'fa-AF', 'fa-IR', 'fr-FR', 'fr-CA', 'hu-HU', 'id-ID', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pl-Pl', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN']`.<br/>**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'.
**isDivisibleBy(str, number)** | check if the string is a number that is divisible by another.
**isEAN(str)** | check if the string is an [EAN (European Article Number)][European Article Number].
**isEmail(str [, options])** | check if the string is an email.<br/><br/>`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, allow_underscores: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. If `allow_display_name` is set to true, the validator will also match `Display Name <email-address>`. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name <email-address>`. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, email addresses without a TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by Gmail. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails. If `host_whitelist` is set to an array of strings and the part of the email after the `@` symbol matches none of the strings defined in it, the validation fails.
**isEmail(str [, options])** | check if the string is an email.<br/><br/>`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, allow_underscores: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. If `allow_display_name` is set to true, the validator will also match `Display Name <email-address>`. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name <email-address>`. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, email addresses without a TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by Gmail. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings or regexp, and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails. If `host_whitelist` is set to an array of strings or regexp, and the part of the email after the `@` symbol matches none of the strings defined in it, the validation fails.
**isEmpty(str [, options])** | check if the string has a length of zero.<br/><br/>`options` is an object which defaults to `{ ignore_whitespace: false }`.
**isEthereumAddress(str)** | check if the string is an [Ethereum][Ethereum] address. Does not validate address checksums.
**isFloat(str [, options])** | check if the string is a float.<br/><br/>`options` is an object which can contain the keys `min`, `max`, `gt`, and/or `lt` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`) it also has `locale` as an option.<br/><br/>`min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.<br/><br/>`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fr-CA', 'fr-FR', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`. Locale list is `validator.isFloatLocales`.
Expand Down Expand Up @@ -139,7 +139,7 @@ Validator | Description
**isJSON(str [, options])** | check if the string is valid JSON (note: uses JSON.parse).<br/><br/>`options` is an object which defaults to `{ allow_primitives: false }`. If `allow_primitives` is true, the primitives 'true', 'false' and 'null' are accepted as valid JSON values.
**isJWT(str)** | check if the string is valid JWT token.
**isLatLong(str [, options])** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.<br/><br/>`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) latitude-longitude format.
**isLength(str [, options])** | check if the string's length falls in a range.<br/><br/>`options` is an object which defaults to `{ min: 0, max: undefined }`. Note: this function takes into account surrogate pairs.
**isLength(str [, options])** | check if the string's length falls in a range and equal to any of the integers of the `discreteLengths` array if provided.<br/><br/>`options` is an object which defaults to `{ min: 0, max: undefined, discreteLengths: undefined }`. Note: this function takes into account surrogate pairs.
**isLicensePlate(str, locale)** | check if the string matches the format of a country's license plate.<br/><br/>`locale` is one of `['cs-CZ', 'de-DE', 'de-LI', 'en-IN', 'en-SG', 'en-PK', 'es-AR', 'hu-HU', 'pt-BR', 'pt-PT', 'sq-AL', 'sv-SE']` or `'any'`.
**isLocale(str)** | check if the string is a locale.
**isLowercase(str)** | check if the string is lowercase.
Expand Down
1 change: 1 addition & 0 deletions src/lib/isDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default function isDate(input, options) {
options = merge(options, default_date_options);
}
if (typeof input === 'string' && isValidFormat(options.format)) {
if (options.strictMode && input.length !== options.format.length) return false;
const formatDelimiter = options.delimiters
.find(delimiter => options.format.indexOf(delimiter) !== -1);
const dateDelimiter = options.strictMode
Expand Down
6 changes: 3 additions & 3 deletions src/lib/isEmail.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assertString from './util/assertString';
import checkHost from './util/checkHost';

import isByteLength from './isByteLength';
import isFQDN from './isFQDN';
Expand Down Expand Up @@ -60,7 +61,6 @@ function validateDisplayName(display_name) {
return true;
}


export default function isEmail(str, options) {
assertString(str);
options = merge(options, default_email_options);
Expand Down Expand Up @@ -97,11 +97,11 @@ export default function isEmail(str, options) {
const domain = parts.pop();
const lower_domain = domain.toLowerCase();

if (options.host_blacklist.includes(lower_domain)) {
if (options.host_blacklist.length > 0 && checkHost(lower_domain, options.host_blacklist)) {
return false;
}

if (options.host_whitelist.length > 0 && !options.host_whitelist.includes(lower_domain)) {
if (options.host_whitelist.length > 0 && !checkHost(lower_domain, options.host_whitelist)) {
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib/isISO6346.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export function isISO6346(str) {
} else sum += str[i] * (2 ** i);
}

const checkSumDigit = sum % 11;
let checkSumDigit = sum % 11;
if (checkSumDigit === 10) checkSumDigit = 0;
return Number(str[str.length - 1]) === checkSumDigit;
}

Expand Down
10 changes: 9 additions & 1 deletion src/lib/isLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ export default function isLength(str, options) {
assertString(str);
let min;
let max;

if (typeof (options) === 'object') {
min = options.min || 0;
max = options.max;
} else { // backwards compatibility: isLength(str, min [, max])
min = arguments[1] || 0;
max = arguments[2];
}

const presentationSequences = str.match(/(\uFE0F|\uFE0E)/g) || [];
const surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
const len = str.length - presentationSequences.length - surrogatePairs.length;
return len >= min && (typeof max === 'undefined' || len <= max);
const isInsideRange = len >= min && (typeof max === 'undefined' || len <= max);

if (isInsideRange && Array.isArray(options?.discreteLengths)) {
return options.discreteLengths.some(discreteLen => discreteLen === len);
}

return isInsideRange;
}
6 changes: 3 additions & 3 deletions src/lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const phones = {
'en-BS': /^(\+?1[-\s]?|0)?\(?242\)?[-\s]?\d{3}[-\s]?\d{4}$/,
'en-GB': /^(\+?44|0)7[1-9]\d{8}$/,
'en-GG': /^(\+?44|0)1481\d{6}$/,
'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|28|55|59)\d{7}$/,
'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|53|28|55|59)\d{7}$/,
'en-GY': /^(\+592|0)6\d{6}$/,
'en-HK': /^(\+?852[-\s]?)?[456789]\d{3}[-\s]?\d{4}$/,
'en-MO': /^(\+?853[-\s]?)?[6]\d{3}[-\s]?\d{4}$/,
Expand Down Expand Up @@ -74,7 +74,7 @@ const phones = {
'en-US': /^((\+1|1)?( |-)?)?(\([2-9][0-9]{2}\)|[2-9][0-9]{2})( |-)?([2-9][0-9]{2}( |-)?[0-9]{4})$/,
'en-VU': /^(\+678)?[5]\d{5}$/,
'en-ZA': /^(\+?27|0)\d{9}$/,
'en-ZM': /^(\+?26)?09[567]\d{7}$/,
'en-ZM': /^(\+?26)?0[79][567]\d{7}$/,
'en-ZW': /^(\+263)[0-9]{9}$/,
'en-BW': /^(\+?267)?(7[1-8]{1})\d{6}$/,
'es-AR': /^\+?549(11|[2368]\d)\d{8}$/,
Expand Down Expand Up @@ -125,7 +125,7 @@ const phones = {
'kk-KZ': /^(\+?7|8)?7\d{9}$/,
'kl-GL': /^(\+?299)?\s?\d{2}\s?\d{2}\s?\d{2}$/,
'ko-KR': /^((\+?82)[ \-]?)?0?1([0|1|6|7|8|9]{1})[ \-]?\d{3,4}[ \-]?\d{4}$/,
'ky-KG': /^(\+?7\s?\+?7|0)\s?\d{2}\s?\d{3}\s?\d{4}$/,
'ky-KG': /^(\+996\s?)?(22[0-9]|50[0-9]|55[0-9]|70[0-9]|75[0-9]|77[0-9]|880|990|995|996|997|998)\s?\d{3}\s?\d{3}$/,
'lt-LT': /^(\+370|8)\d{8}$/,
'lv-LV': /^(\+?371)2\d{7}$/,
'mg-MG': /^((\+?261|0)(2|3)\d)?\d{7}$/,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/isPostalCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const patterns = {
BA: /^([7-8]\d{4}$)/,
BE: fourDigit,
BG: fourDigit,
BR: /^\d{5}-\d{3}$/,
BR: /^\d{5}-?\d{3}$/,
BY: /^2[1-4]\d{4}$/,
CA: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s\-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i,
CH: fourDigit,
Expand Down
15 changes: 1 addition & 14 deletions src/lib/isURL.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assertString from './util/assertString';
import checkHost from './util/checkHost';

import isFQDN from './isFQDN';
import isIP from './isIP';
Expand Down Expand Up @@ -38,20 +39,6 @@ const default_url_options = {

const wrapped_ipv6 = /^\[([^\]]+)\](?::([0-9]+))?$/;

function isRegExp(obj) {
return Object.prototype.toString.call(obj) === '[object RegExp]';
}

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;
}

export default function isURL(url, options) {
assertString(url);
if (!url || /[\s<>]/.test(url)) {
Expand Down
13 changes: 13 additions & 0 deletions src/lib/util/checkHost.js
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;
}
Loading

0 comments on commit 546adca

Please sign in to comment.