Skip to content

Commit 7e2ee97

Browse files
authored
Merge pull request #4 from DevKevs/electoralRollValidation
feat: ✨ electoral roll register api call included
2 parents 0ae85bc + 8013deb commit 7e2ee97

3 files changed

Lines changed: 85 additions & 11 deletions

File tree

README.md

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@ This package offers a simple and efficient way to validate Dominican Republic ID
44

55
# Installation
66

7-
you can install the package using:
7+
You can install the package using:
88

99
```sh
1010
npm install do-validator
1111
```
1212

1313
# Use
1414

15-
```sh
16-
import { documentValidator } from 'do-validator';
15+
```ts
16+
import { documentValidator, electoralRollValidation } from 'do-validator';
17+
18+
const document = '10000000001';
1719

18-
const validation = documentValidator('12345678901');
20+
console.log(documentValidator(document));
1921

20-
console.log(validation);
22+
const validation = async () => console.log(await electoralRollValidation(document));
23+
24+
validation();
2125
```
2226
# API
23-
```sh
24-
documentValidator(document)
27+
```ts
28+
documentValidator(document) //Faster
2529
```
2630

2731
Validates a Dominican identity card number.
@@ -31,9 +35,29 @@ document (string): The identity card number to validate 11 characters without sp
3135

3236
### Return
3337
boolean: true if the ID number is valid, false otherwise.
38+
<hr>
3439

35-
# Example
36-
```sh
40+
```ts
41+
//validation by API Call
42+
await electoralRollValidation(document)
43+
```
44+
Validates a Dominican identity card number by an API Call in the Electoral roll.
45+
46+
### Parameters
47+
document (string): The identity card number to validate 11 characters without special characters.
48+
49+
### Return
50+
```ts
51+
{
52+
statusCode: number,
53+
valid: boolean,
54+
message: string
55+
}
56+
```
57+
58+
59+
# Examples
60+
```ts
3761
import { documentValidator } from 'do-validator';
3862

3963
const validId = 'YOUR_DOMINICAN_DOCUMENT';
@@ -42,6 +66,19 @@ const invalidId = '12345678901';
4266
console.log(documentValidator(validId)); // true
4367
console.log(documentValidator(invalidId)); // false
4468

69+
```
70+
71+
```ts
72+
import { electoralRollValidation } from 'do-validator';
73+
74+
const validId = 'YOUR_DOMINICAN_DOCUMENT';
75+
const invalidId = '12345678901';
76+
77+
const validation = async (document) => console.log(await electoralRollValidation(document));
78+
79+
validation(validId); // valid json response
80+
validation(invalidId) // invalid json response
81+
4582
```
4683
# Author
4784
DevKevs

src/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,36 @@ export function documentValidator(document: string): boolean {
2525
}
2626
}
2727

28+
/**
29+
* The function `electoralRollValidation` validates in a electoral roll register by
30+
* making an API call.
31+
* @param {string} document - The `document` parameter in the `electoralRollValidation` function is
32+
* expected to be a string representing an identification document number.
33+
* @returns The function `electoralRollValidation` returns the response from the API endpoint
34+
* after validating the provided
35+
* document number.
36+
*/
37+
export async function electoralRollValidation(document: string) {
38+
try {
39+
const documentId: string = removeHyphens(document);
40+
41+
if (documentId.length !== 11) {
42+
throw new Error('The provided document must have 11 characters.');
43+
}
44+
45+
const promise = await fetch(`https://api.digital.gob.do/v3/cedulas/${documentId}/validate`);
46+
const response = await promise.json();
47+
48+
return {
49+
statusCode: promise.status,
50+
valid: response.valid,
51+
message: response.message ?? 'The provided document is valid.'
52+
};
53+
} catch (error) {
54+
console.error(error)
55+
}
56+
}
57+
2858
function removeHyphens(documentId: string): string {
2959
if (documentId.includes('-')) {
3060
return documentId.replace(/-/g, '');

src/testlib.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
import { documentValidator } from ".";
1+
import { documentValidator, electoralRollValidation } from ".";
22

3-
console.log(documentValidator("12345678901"));
3+
const document = '10000000001';
4+
5+
//Locally validation (No API call needed)
6+
console.log(documentValidator(document));
7+
8+
//Server side, by API call validation
9+
const validation = async () => console.log(await electoralRollValidation(document));
10+
validation();

0 commit comments

Comments
 (0)