zimbabwean-id-number-validator 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +88 -34
  2. package/constants.js +88 -63
  3. package/index.js +13 -4
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -1,40 +1,21 @@
1
1
  # Zimbabwean ID Number Validator
2
2
 
3
3
  The ID numbers issued in Zimbabwe consist of either 11 or 12 alphanumeric characters. Each ID contains one alphabetic letter, while the remaining characters are numeric digits. Various systems may capture ID numbers in different formats, with the most common formats including:
4
- - 082047823Q29
5
- - 08-2047823-Q-29
6
- - 08-2047823Q29
7
4
 
8
- | ID | Description |
9
- |------|---------------------------------------------------------------------------|
10
- | 08 | District Code |
11
- | 2047823 | 6 or 7 digit sequence|
12
- | Q | Alphabetic letter |
13
- | 29 | District Code |
14
- -----------------------------------------------------------------------------------
5
+ - 082047823Q29 (No hyphens)
6
+ - 08-2047823Q29 (Hyphen after district code)
7
+ - 08-2047823-Q-29 (Full hyphenation)
15
8
 
16
- This package uses regular expressions to validate the input against the established patterns. It also includes a lookup of District Codes to ensure the correctness of the ID number.
17
- The three possible responses are as follows:
18
- ```javascript
19
- {
20
- isIdNumberValid: false,
21
- description: "id number must be in one of the formats: (082047823Q29), (08-2047823Q29), or (08-2047823-Q-29)"
22
- },
23
-
24
- {
25
- isIdNumberValid: false,
26
- description: "id number is invalid"
27
- },
28
-
29
- {
30
- isIdNumberValid: true,
31
- description: "id number is valid"
32
- }
9
+ | ID | Description |
10
+ | ------- | --------------------- |
11
+ | 08 | District Code |
12
+ | 2047823 | 6 or 7 digit sequence |
13
+ | Q | Alphabetic letter |
14
+ | 29 | District Code |
33
15
 
34
- ```
35
-
36
- District Codes used in the validation procedures were acquired from the following [source](https://ntjwg.uwazi.io/entity/seif769joed?file=15512639038111am2iqd21an.pdf&page=9).
16
+ ---
37
17
 
18
+ This package uses regular expressions to validate the input against the established patterns. It also includes a lookup of District Codes to ensure the correctness of the ID number. District Codes used in the validation procedures were acquired from the following [source](https://ntjwg.uwazi.io/entity/seif769joed?file=15512639038111am2iqd21an.pdf&page=9).
38
19
 
39
20
  ## Usage
40
21
 
@@ -47,15 +28,88 @@ npm install zimbabwean-id-number-validator
47
28
  ### Usage In ES6
48
29
 
49
30
  ```javascript
50
- import validateIdNumber from 'zimbabwean-id-number-validator'
51
- const validationResults = validateIdNumber("082047823Q21")
31
+ import validateIdNumber from "zimbabwean-id-number-validator";
32
+ const validationResults = validateIdNumber("082047823Q21");
52
33
  ```
53
34
 
54
35
  ### Usage in CommonJS
55
36
 
56
37
  ```javascript
57
- const validateIdNumber = require('zimbabwean-id-number-validator');
58
- const validationResults = validateIdNumber("082047823Q21")
38
+ const validateIdNumber = require("zimbabwean-id-number-validator");
39
+ const validationResults = validateIdNumber("082047823Q21");
40
+ ```
41
+
42
+ ### Advanced Usage
43
+
44
+ ```javascript
45
+ import { validateIdNumber } from "zimbabwean-id-number-validator";
46
+
47
+ // Validate with specific format
48
+ const result = validateIdNumber("08-2047823-Q-29", "FULL_HYPHENS");
49
+
50
+ // Available format options:
51
+ // NO_HYPHENS - 082047823Q29
52
+ // HYPHEN_AFTER_DISTRICT - 08-2047823Q29
53
+ // FULL_HYPHENS - 08-2047823-Q-29
54
+ ```
55
+
56
+ ## Validation Results
57
+
58
+ The validator provides detailed response objects
59
+
60
+ ### Successful Validation
61
+
62
+ ```javascript
63
+ {
64
+ isValid: true,
65
+ description: "id number is valid"
66
+ }
67
+ ```
68
+
69
+ ### Format-Specific Errors
70
+
71
+ When using NO_HYPHENS
72
+
73
+ ```javascript
74
+ {
75
+ isValid: false,
76
+ description: "id number must be in the format: 082047823Q29"
77
+ }
59
78
  ```
60
79
 
80
+ When using HYPHEN_AFTER_DISTRICT
61
81
 
82
+ ```javascript
83
+ {
84
+ isValid: false,
85
+ description: "id number must be in the format: 08-2047823Q29"
86
+ }
87
+ ```
88
+
89
+ When using FULL_HYPHENS
90
+
91
+ ```javascript
92
+ {
93
+ isValid: false,
94
+ description: "id number must be in the format: 08-2047823-Q-29"
95
+ }
96
+ ```
97
+
98
+ ### Content Validation Errors
99
+ ```javascript
100
+ {
101
+ isValid: false,
102
+ description: "id number is invalid"
103
+ }
104
+ ```
105
+
106
+ ### Automatic Format Detection
107
+
108
+ When no specific format is specified, the validator provides general guidance:
109
+
110
+ ```javascript
111
+ {
112
+ isValid: false,
113
+ description: "id number must be in one of the formats: 082047823Q29, 08-2047823Q29, or 08-2047823-Q-29"
114
+ }
115
+ ```
package/constants.js CHANGED
@@ -8,7 +8,7 @@ const validLetters = {
8
8
  6: "F",
9
9
  7: "G",
10
10
  8: "H",
11
- 9: "J",
11
+ 9: "J",
12
12
  10: "K",
13
13
  11: "L",
14
14
  12: "M",
@@ -24,69 +24,94 @@ const validLetters = {
24
24
  22: "Y",
25
25
  }
26
26
 
27
- const validDistrictCodes = [
28
- "08",
29
- "63",
30
- "07",
31
- "44",
32
- "13",
33
- "42",
34
- "75",
35
- "50",
36
- "34",
37
- "05",
38
- "71",
39
- "15",
40
- "45",
41
- "11",
42
- "61",
43
- "68",
44
- "18",
45
- "25",
46
- "80",
47
- "43",
48
- "49",
49
- "47",
50
- "48",
51
- "59",
52
- "85",
53
- "32",
54
- "38",
55
- "24",
56
- "37",
57
- "70",
58
- "86",
59
- "04",
60
- "14",
61
- "13",
62
- "27",
63
- "22",
64
- "54",
65
- "83",
66
- "06",
67
- "35",
68
- "79",
69
- "41",
70
- "53",
71
- "73",
72
- "84",
73
- "02",
74
- "56",
75
- "28",
76
- "21",
77
- "39",
78
- "19",
79
- "77",
80
- "26",
81
- "23",
82
- "29",
83
- "58",
84
- "03",
85
- "66",
86
- "67",
87
- ]
27
+ const zimDistricts = {
28
+ "Foreigner": "00",
29
+ "Beitbridge": "02",
30
+ "Mberengwa": "03",
31
+ "Bikita": "04",
32
+ "Bindura": "05",
33
+ "Binga": "06",
34
+ "Buhera": "07",
35
+ "Bulawayo": "08",
36
+ "Muzarabani": "11",
37
+ "Chipinge": "13",
38
+ "Chiredzi": "14",
39
+ "Mazowe": "15",
40
+ "Filabusi": "17",
41
+ "Chikomba": "18",
42
+ "Umzingwane": "19",
43
+ "Insiza": "21",
44
+ "Masvingo": "22",
45
+ "Gokwe South": "23",
46
+ "Kadoma": "24",
47
+ "Goromonzi": "25",
48
+ "Gokwe North": "26",
49
+ "Gutu": "27",
50
+ "Gwanda": "28",
51
+ "Gweru": "29",
52
+ "Chiredzii": "31",
53
+ "Chegutu": "32",
54
+ "Nyanga": "34",
55
+ "Bubi": "35",
56
+ "Kariba": "37",
57
+ "Hurungwe": "38",
58
+ "Matobo": "39",
59
+ "Lupane": "41",
60
+ "Makoni": "42",
61
+ "Marondera": "43",
62
+ "Chimanimani": "44",
63
+ "Mt Darwin": "45",
64
+ "Murehwa": "47",
65
+ "Mutoko": "48",
66
+ "Mudzi": "49",
67
+ "Mutasa": "50",
68
+ "Nkayi": "53",
69
+ "Mwenezi": "54",
70
+ "Bulilimamangwe": "56",
71
+ "Kwekwe": "58",
72
+ "Seke": "59",
73
+ "Rushinga": "61",
74
+ "Harare": "63",
75
+ "Beatrice": "65",
76
+ "Shurugwi": "66",
77
+ "Zvishavane": "67",
78
+ "Shamva": "68",
79
+ "Makonde": "70",
80
+ "Guruve": "71",
81
+ "Tsholotsho": "73",
82
+ "Mutare": "75",
83
+ "Mount Darwin": "76",
84
+ "Chirumanzu": "77",
85
+ "Hwange": "79",
86
+ "Hwedza": "80",
87
+ "Zaka": "83",
88
+ "Umgusa": "84",
89
+ "Uzumba Maramba Pfungwe": "85",
90
+ "Zvimba": "86"
91
+ }
92
+
93
+ const validDistrictCodes = Object.values(zimDistricts)
94
+
95
+ const defaultRegexFormat = /^\d{2}-?\d{6,7}-?[A-HJ-NP-Z]-?\d{2}$/
96
+ const defaultValidationError = "id number must be in one of the formats: (082047823Q29), (08-2047823Q29), or (08-2047823-Q-29)"
97
+
98
+ const formatOptions = {
99
+ "NO_HYPHENS": /^\d{2}\d{6,7}[A-HJ-NP-Z]\d{2}$/,
100
+ "HYPHEN_AFTER_DISTRICT": /^\d{2}-\d{6,7}[A-HJ-NP-Z]\d{2}$/,
101
+ "FULL_HYPHENS": /^\d{2}-\d{6,7}-[A-HJ-NP-Z]-\d{2}$/,
102
+ }
103
+
104
+ const validationErrors = {
105
+ "NO_HYPHENS": "id number must be in the format: (082047823Q29)",
106
+ "HYPHEN_AFTER_DISTRICT": "id number must be in the format: (08-2047823Q29)",
107
+ "FULL_HYPHENS": "id number must be in the format: (08-2047823-Q-29)"
108
+ }
88
109
 
89
110
  module.exports = {
90
111
  validLetters,
91
- validDistrictCodes
112
+ validDistrictCodes,
113
+ defaultRegexFormat,
114
+ defaultValidationError,
115
+ formatOptions,
116
+ validationErrors
92
117
  }
package/index.js CHANGED
@@ -1,12 +1,21 @@
1
- const { validLetters, validDistrictCodes } = require("./constants")
1
+ const {
2
+ validLetters,
3
+ validDistrictCodes,
4
+ defaultRegexFormat,
5
+ defaultValidationError,
6
+ formatOptions,
7
+ validationErrors
8
+ } = require("./constants.js")
2
9
 
3
- const idNumberRegex = /^\d{2}-?\d{6,7}-?[A-HJ-NP-Z]-?\d{2}$/;
10
+ const validateIdNumber = (idNumber, formatOption) => {
11
+
12
+ // if no formatOption is provided, use the default regex format
13
+ const idNumberRegex = formatOptions[formatOption] || defaultRegexFormat
4
14
 
5
- const validateIdNumber = (idNumber) => {
6
15
  // initialize id validation results
7
16
  const idValidationResults = {
8
17
  isIdNumberValid: false,
9
- description: "id number must be in one of the formats: (082047823Q29), (08-2047823Q29), or (08-2047823-Q-29)"
18
+ description: validationErrors[formatOption] || defaultValidationError
10
19
  }
11
20
 
12
21
  // check if the id number is formatted correctly
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zimbabwean-id-number-validator",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Zimbabwean national id number validator",
5
5
  "main": "index.js",
6
6
  "scripts": {