Skip to content

Commit 99988fe

Browse files
committed
refactor: update multiform-validator to version 2.2.7
- Updated the CDN links in the README.md file to use the latest version (2.2.7) of multiform-validator. - Updated the version number in the package.json file to 2.2.7. - Refactored the type definitions and error messages in the validateEmail.ts file. - Updated the module code generation in the tsconfig.json file to use CommonJS instead of ES6. - Added new test cases for invalid PNG, JPEG, and GIF images in the tests folder. - Generated fake PNG, JPEG, and GIF images for testing purposes.
1 parent 94d1e47 commit 99988fe

File tree

14 files changed

+157
-18
lines changed

14 files changed

+157
-18
lines changed

.yarn/install-state.gz

-156 Bytes
Binary file not shown.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ Feel free to find bugs and report them to me. Your feedback is highly appreciate
2020
jsDelivr:
2121

2222
```bash
23-
https://cdn.jsdelivr.net/npm/[email protected].3/+esm
23+
https://cdn.jsdelivr.net/npm/[email protected].7/+esm
2424
```
2525

2626
```html
2727
<script type="module">
28-
import multiform-validator from "https://cdn.jsdelivr.net/npm/[email protected].3/+esm"
28+
import multiform-validator from "https://cdn.jsdelivr.net/npm/[email protected].7/+esm"
2929
</script>
3030
```
3131

@@ -71,7 +71,7 @@ using esm:
7171

7272
```html
7373
<script type="module">
74-
import multiformValidator from "https://cdn.jsdelivr.net/npm/[email protected].3/+esm";
74+
import multiformValidator from "https://cdn.jsdelivr.net/npm/[email protected].7/+esm";
7575
const emailResult = multiformValidator.isEmail("123456");
7676
const cpfResult = multiformValidator.cpfIsValid("123456");
7777

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "multiform-validator",
3-
"version": "2.2.6",
3+
"version": "2.2.7",
44
"description": "Javascript library made to validate, several form fields, such as: email, images, phone, password, cpf etc.",
55
"main": "./dist/cjs/index.cjs",
66
"module": "./dist/esm/index.mjs",

src/isValidImage/validateGif.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
export default function validateGif(fileBuffer: Buffer): boolean {
2-
const isGif: boolean =
3-
fileBuffer[0] == 0x47 &&
4-
fileBuffer[1] == 0x49 &&
5-
fileBuffer[2] == 0x46 &&
6-
fileBuffer[3] == 0x38;
2+
const isGifSignature: boolean =
3+
fileBuffer[0] === 0x47 && // 'G'
4+
fileBuffer[1] === 0x49 && // 'I'
5+
fileBuffer[2] === 0x46 && // 'F'
6+
fileBuffer[3] === 0x38 && // '8'
7+
(fileBuffer[4] === 0x39 || fileBuffer[4] === 0x37) && // '9' ou '7'
8+
fileBuffer[5] === 0x61; // 'a'
79

8-
return isGif;
10+
if (isGifSignature) {
11+
const fileSize: number = fileBuffer.length;
12+
// The GIF ends with 0x00 0x3B (end marker)
13+
if (
14+
fileBuffer[fileSize - 2] === 0x00 &&
15+
fileBuffer[fileSize - 1] === 0x3b
16+
) {
17+
return true;
18+
}
19+
return false; // Corrupted or invalid GIF file
20+
}
21+
22+
return isGifSignature;
923
}

src/isValidImage/validateIco.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export default function validateIco(fileBuffer: Buffer): boolean {
22
const isIco: boolean =
3-
fileBuffer[0] === 0x00 && fileBuffer[1] === 0x00 && fileBuffer[2] === 0x01;
3+
fileBuffer[0] === 0x00 &&
4+
fileBuffer[1] === 0x00 &&
5+
fileBuffer[2] === 0x01 &&
6+
fileBuffer[3] === 0x00;
47

58
return isIco;
69
}

src/isValidImage/validateJpeg.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
export default function validateJpeg(fileBuffer: Buffer): boolean {
2-
const isJpeg: boolean =
2+
const jpegSignature: boolean =
33
fileBuffer[0] === 0xff && fileBuffer[1] === 0xd8 && fileBuffer[2] === 0xff;
44

5-
return isJpeg;
5+
if (jpegSignature) {
6+
const fileSize: number = fileBuffer.length;
7+
// Valid JPEG ends with marker 0xFF, 0xD9
8+
9+
// Check if the last two bytes are 0xFF, 0xD9
10+
if (
11+
fileBuffer[fileSize - 2] !== 0xff ||
12+
fileBuffer[fileSize - 1] !== 0xd9
13+
) {
14+
return false;
15+
}
16+
17+
return true;
18+
}
19+
20+
return jpegSignature;
621
}

src/isValidImage/validatePng.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
export default function validatePng(fileBuffer: Buffer): boolean {
2-
const isPng: boolean =
2+
const pngSignature: boolean =
33
fileBuffer[0] === 0x89 &&
44
fileBuffer[1] === 0x50 &&
55
fileBuffer[2] === 0x4e &&
6-
fileBuffer[3] === 0x47;
6+
fileBuffer[3] === 0x47 &&
7+
fileBuffer[4] === 0x0d &&
8+
fileBuffer[5] === 0x0a &&
9+
fileBuffer[6] === 0x1a &&
10+
fileBuffer[7] === 0x0a;
711

8-
return isPng;
12+
if (pngSignature) {
13+
const fileSize: number = fileBuffer.length;
14+
// A valid PNG structure has a specific byte sequence at the end
15+
const pngEndSignature: number[] = [
16+
0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
17+
];
18+
for (let i: number = 0; i < pngEndSignature.length; i++) {
19+
if (fileBuffer[fileSize - 8 + i] !== pngEndSignature[i]) {
20+
return false; // PNG file is corrupt or invalid
21+
}
22+
}
23+
}
24+
25+
return pngSignature;
926
}
103 Bytes
Loading
108 Bytes
Loading
106 Bytes
Loading

0 commit comments

Comments
 (0)