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

feat(isIdentityCard): Add PL locale #1745

Merged
merged 14 commits into from
Oct 2, 2021
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Validator | Description
**isCreditCard(str)** | check if the string is a credit card.
**isCurrency(str [, options])** | check if the string is a valid currency amount.<br/><br/>`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.<br/>**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3].
**isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs).
**isPESEL(str)** |
**isDate(input [, options])** | Check if the input is a valid date. e.g. [`2002-07-15`, new Date()].<br/><br/> `options` is an object which can contain the keys `format`, `strictMode` and/or `delimiters`<br/><br/>`format` is a string and defaults to `YYYY/MM/DD`.<br/><br/>`strictMode` is a boolean and defaults to `false`. If `strictMode` is set to true, the validator will reject inputs different from `format`.<br/><br/> `delimiters` is an array of allowed date delimiters and defaults to `['/', '-']`.
**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` determine 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', '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's divisible by another.
Expand All @@ -115,7 +116,7 @@ Validator | Description
**isHexColor(str)** | check if the string is a hexadecimal color.
**isHSL(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).<br/><br/>Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`).
**isIBAN(str)** | check if a string is a IBAN (International Bank Account Number).
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES', 'IN', 'IT', 'IR', 'MZ', 'NO', 'TH', 'zh-TW', 'he-IL', 'ar-LY', 'ar-TN', 'zh-CN']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['PL', 'ES', 'IN', 'IT', 'IR', 'MZ', 'NO', 'TH', 'zh-TW', 'he-IL', 'ar-LY', 'ar-TN', 'zh-CN']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
**isIMEI(str [, options]))** | check if the string is a valid IMEI number. Imei should be of format `###############` or `##-######-######-#`.<br/><br/>`options` is an object which can contain the keys `allow_hyphens`. Defaults to first format . If allow_hyphens is set to true, the validator will validate the second format.
**isIn(str, values)** | check if the string is in a array of allowed values.
**isInt(str [, options])** | check if the string is an integer.<br/><br/>`options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4).
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import isISIN from './lib/isISIN';
import isISBN from './lib/isISBN';
import isISSN from './lib/isISSN';
import isTaxID from './lib/isTaxID';
import isPESEL from './lib/isPESEL';

import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePhone';

Expand Down Expand Up @@ -142,6 +143,7 @@ const validator = {
isIBAN,
isBIC,
isAlpha,
isPESEL,
isAlphaLocales,
isAlphanumeric,
isAlphanumericLocales,
Expand Down
42 changes: 42 additions & 0 deletions src/lib/isIdentityCard.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
import assertString from './util/assertString';
import isInt from './isInt';

const validators = {
PL: (str) => {
assertString(str);

const weightOfDigits = {
1: 1,
2: 3,
3: 7,
4: 9,
5: 1,
6: 3,
7: 7,
8: 9,
9: 1,
10: 3,
11: 1,
};

if (str != null && str.length === 11 && isInt(str, { allow_leading_zeroes: true })) {
const digits = str.split('');
let sum = 0;

digits.forEach((digit, index) => {
// Ignored because handling else is unnecesary
/* istanbul ignore else */
if (index !== 10) {
sum += Number(digit) * weightOfDigits[index + 1];
}
});
wiktorwojcik112 marked this conversation as resolved.
Show resolved Hide resolved

const modulo = sum % 10;
const lastDigit = Number(str.charAt(str.length - 1));

// Ignored because handling else is unnecesary
/* istanbul ignore else */
if ((modulo === 0 && lastDigit === 0) || (lastDigit === 10 - modulo)) {
wiktorwojcik112 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
} else {
return false;
wiktorwojcik112 marked this conversation as resolved.
Show resolved Hide resolved
}
},
ES: (str) => {
assertString(str);

Expand Down
53 changes: 53 additions & 0 deletions src/lib/isPESEL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import assertString from './util/assertString';
import isInt from './isInt';

/**
* Key: Position of digit (starts from 1).
* Value: Weight of digit at specified position.
*/

const weightOfDigits = {
1: 1,
2: 3,
3: 7,
4: 9,
5: 1,
6: 3,
7: 7,
8: 9,
9: 1,
10: 3,
11: 1,
};

/**
* PESEL is a national identification number used by Polish government.
* It is assigned to every Polish citzen and is unique.
*
* Reference: https://en.wikipedia.org/wiki/PESEL
*
* @param {string} str
* @return {boolean}
*/

export default function isPESEL(str) {
assertString(str);

if (str == null || str.length !== 11 || !isInt(str, { allow_leading_zeroes: true })) {
return false;
}

const digits = str.split('');
let sum = 0;

digits.forEach((digit, index) => {
if (index !== 10) {
sum += Number(digit) * weightOfDigits[index + 1];
}
});

const modulo = sum % 10;
const lastDigit = Number(str.charAt(str.length - 1));

return (modulo === 0 && lastDigit === 0) || (lastDigit === 10 - modulo);
}
41 changes: 41 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,28 @@ describe('Validators', () => {
});
});

it('should validate PESEL', () => {
test({
validator: 'isPESEL',
valid: [
'99012229019',
'09210215408',
'20313034701',
'86051575214',
],
invalid: [
'5',
'195',
'',
'12345678901',
'99212229019',
'09210215402',
'20313534701',
'86241579214',
],
});
});

it('should validate isIPRange', () => {
test({
validator: 'isIPRange',
Expand Down Expand Up @@ -4672,6 +4694,25 @@ describe('Validators', () => {

it('should validate identity cards', () => {
const fixtures = [
{
locale: 'PL',
valid: [
'99012229019',
'09210215408',
'20313034701',
'86051575214',
],
invalid: [
'5',
'195',
'',
'12345678901',
'99212229019',
'09210215402',
'20313534701',
'86241579214',
],
},
{
locale: 'ES',
valid: [
Expand Down