Skip to content

Commit 57cc14e

Browse files
MarcholioMarkus
andauthored
feat(isIdentityCard): add finnish locale (#1838)
Co-authored-by: Markus <[email protected]>
1 parent 2201869 commit 57cc14e

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Validator | Description
115115
**isHexColor(str)** | check if the string is a hexadecimal color.
116116
**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)`).
117117
**isIBAN(str)** | check if a string is a IBAN (International Bank Account Number).
118-
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`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.<br/><br/>Defaults to 'any'.
118+
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['LK', 'PL', 'ES', 'FI', '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'.
119119
**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.
120120
**isIn(str, values)** | check if the string is in a array of allowed values.
121121
**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).

src/lib/isIdentityCard.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ const validators = {
6363

6464
return sanitized.endsWith(controlDigits[number % 23]);
6565
},
66+
FI: (str) => {
67+
// https://dvv.fi/en/personal-identity-code#:~:text=control%20character%20for%20a-,personal,-identity%20code%20calculated
68+
assertString(str);
69+
70+
if (str.length !== 11) {
71+
return false;
72+
}
73+
74+
if (!str.match(/^\d{6}[\-A\+]\d{3}[0-9ABCDEFHJKLMNPRSTUVWXY]{1}$/)) {
75+
return false;
76+
}
77+
78+
const checkDigits = '0123456789ABCDEFHJKLMNPRSTUVWXY';
79+
80+
const idAsNumber = (parseInt(str.slice(0, 6), 10) * 1000) + parseInt(str.slice(7, 10), 10);
81+
const remainder = idAsNumber % 31;
82+
const checkDigit = checkDigits[remainder];
83+
84+
return checkDigit === str.slice(10, 11);
85+
},
6686
IN: (str) => {
6787
const DNI = /^[1-9]\d{3}\s?\d{4}\s?\d{4}$/;
6888

test/validators.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4874,6 +4874,20 @@ describe('Validators', () => {
48744874
'Z1234567C',
48754875
],
48764876
},
4877+
{
4878+
locale: 'FI',
4879+
valid: [
4880+
'131052-308T', // People born in 1900s
4881+
'131052A308T', // People born in 2000s
4882+
'131052+308T', // People born in 1800s
4883+
'131052-313Y',
4884+
],
4885+
invalid: [
4886+
'131052308T',
4887+
'131052-308T ',
4888+
'131052-308A',
4889+
],
4890+
},
48774891
{
48784892
locale: 'IN',
48794893
valid: [

0 commit comments

Comments
 (0)