From 52fa6a86c8472d27b35637d07529b215262ae1f9 Mon Sep 17 00:00:00 2001 From: Nimantha Cooray <54179737+nimanthadilz@users.noreply.github.com> Date: Sat, 30 Oct 2021 12:01:29 +0530 Subject: [PATCH] feat(isIdentityCard): add 'LK' (Sri Lanka) locale (#1786) * Implement isIdentityCard LK(Sri Lanka) locale * Add tests for isIdentityCard LK locale * Update README.md for the isIdentityCard LK locale * Change let to const Co-authored-by: Sarhan Aissi Co-authored-by: Sarhan Aissi --- README.md | 2 +- src/lib/isIdentityCard.js | 8 ++++++++ test/validators.js | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 192d79fb7..bcf4493c7 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,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).

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.

`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.

Defaults to 'any'. +**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`locale` is one of `['LK', '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.

Defaults to 'any'. **isIMEI(str [, options]))** | check if the string is a valid IMEI number. Imei should be of format `###############` or `##-######-######-#`.

`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.

`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). diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index 8ea15e262..b004803b8 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -160,6 +160,14 @@ const validators = { } return str[12] === ((11 - (sum % 11)) % 10).toString(); }, + LK: (str) => { + const old_nic = /^[1-9]\d{8}[vx]$/i; + const new_nic = /^[1-9]\d{11}$/i; + + if (str.length === 10 && old_nic.test(str)) return true; + else if (str.length === 12 && new_nic.test(str)) return true; + return false; + }, 'he-IL': (str) => { const DNI = /^\d{9}$/; diff --git a/test/validators.js b/test/validators.js index 3e5c7ce75..8cae6ac01 100644 --- a/test/validators.js +++ b/test/validators.js @@ -4792,6 +4792,27 @@ describe('Validators', () => { it('should validate identity cards', () => { const fixtures = [ + { + locale: 'LK', + valid: [ + '722222222v', + '722222222V', + '993151225x', + '993151225X', + '188888388x', + '935632124V', + '199931512253', + '200023125632', + ], + invalid: [ + '023125648V', + '023345621v', + '021354211X', + '055321231x', + '02135465462', + '199931512253X', + ], + }, { locale: 'PL', valid: [