Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin authored Nov 14, 2024
2 parents 3ddd763 + f54599c commit fdca132
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
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
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;
}
4 changes: 2 additions & 2 deletions src/lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -70,7 +70,7 @@ const phones = {
'en-UG': /^(\+?256|0)?[7]\d{8}$/,
'en-US': /^((\+1|1)?( |-)?)?(\([2-9][0-9]{2}\)|[2-9][0-9]{2})( |-)?([2-9][0-9]{2}( |-)?[0-9]{4})$/,
'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
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
88 changes: 88 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5394,12 +5394,42 @@ describe('Validators', () => {
valid: ['abc', 'de', 'a', ''],
invalid: ['abcd'],
});
test({
validator: 'isLength',
args: [{ max: 6, discreteLengths: 5 }],
valid: ['abcd', 'vfd', 'ff', '', 'k'],
invalid: ['abcdefgh', 'hfjdksks'],
});
test({
validator: 'isLength',
args: [{ min: 2, max: 6, discreteLengths: 5 }],
valid: ['bsa', 'vfvd', 'ff'],
invalid: ['', ' ', 'hfskdunvc'],
});
test({
validator: 'isLength',
args: [{ min: 1, discreteLengths: 2 }],
valid: [' ', 'hello', 'bsa'],
invalid: [''],
});
test({
validator: 'isLength',
args: [{ max: 0 }],
valid: [''],
invalid: ['a', 'ab'],
});
test({
validator: 'isLength',
args: [{ min: 5, max: 10, discreteLengths: [2, 6, 8, 9] }],
valid: ['helloguy', 'shopping', 'validator', 'length'],
invalid: ['abcde', 'abcdefg'],
});
test({
validator: 'isLength',
args: [{ discreteLengths: '9' }],
valid: ['a', 'abcd', 'abcdefghijkl'],
invalid: [],
});
test({
validator: 'isLength',
valid: ['a', '', 'asds'],
Expand Down Expand Up @@ -8023,6 +8053,7 @@ describe('Validators', () => {
'0502345671',
'0242345671',
'0542345671',
'0532345671',
'0272345671',
'0572345671',
'0262345671',
Expand All @@ -8033,6 +8064,7 @@ describe('Validators', () => {
'+233502345671',
'+233242345671',
'+233542345671',
'+233532345671',
'+233272345671',
'+233572345671',
'+233262345671',
Expand Down Expand Up @@ -8764,13 +8796,16 @@ describe('Validators', () => {
'+260966684590',
'+260976684590',
'260976684590',
'+260779493521',
'+260760010936',
],
invalid: [
'12345',
'',
'Vml2YW11cyBmZXJtZtesting123',
'010-38238383',
'966684590',
'760010936',
],
},
{
Expand Down Expand Up @@ -12724,6 +12759,9 @@ describe('Validators', () => {
'39100-000',
'22040-020',
'39400-152',
'39100000',
'22040020',
'39400152',
],
invalid: [
'79800A12',
Expand Down Expand Up @@ -13050,6 +13088,55 @@ describe('Validators', () => {
});
});

it('should validate ISO6346 shipping container IDs with checksum digit 10 represented as 0', () => {
test({
validator: 'isISO6346',
valid: [
'APZU3789870',
'TEMU1002030',
'DFSU1704420',
'CMAU2221480',
'SEGU5060260',
'FCIU8939320',
'TRHU3495670',
'MEDU3871410',
'CMAU2184010',
'TCLU2265970',
],
invalid: [
'APZU3789871', // Incorrect check digit
'TEMU1002031',
'DFSU1704421',
'CMAU2221481',
'SEGU5060261',
],
});
});
it('should validate ISO6346 shipping container IDs with checksum digit 10 represented as 0', () => {
test({
validator: 'isFreightContainerID',
valid: [
'APZU3789870',
'TEMU1002030',
'DFSU1704420',
'CMAU2221480',
'SEGU5060260',
'FCIU8939320',
'TRHU3495670',
'MEDU3871410',
'CMAU2184010',
'TCLU2265970',
],
invalid: [
'APZU3789871', // Incorrect check digit
'TEMU1002031',
'DFSU1704421',
'CMAU2221481',
'SEGU5060261',
],
});
});

// EU-UK valid numbers sourced from https://ec.europa.eu/taxation_customs/tin/specs/FS-TIN%20Algorithms-Public.docx or constructed by @tplessas.
it('should validate taxID', () => {
test({
Expand Down Expand Up @@ -13985,6 +14072,7 @@ describe('Validators', () => {
new Date([2014, 2, 15]),
new Date('2014-03-15'),
'29.02.2020',
'02.29.2020.20',
'2024-',
'2024-05',
'2024-05-',
Expand Down

0 comments on commit fdca132

Please # to comment.