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

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/multiform-validator@2.2.3/+esm
23+
https://cdn.jsdelivr.net/npm/multiform-validator@2.2.7/+esm
2424
```
2525

2626
```html
2727
<script type="module">
28-
import multiform-validator from "https://cdn.jsdelivr.net/npm/multiform-validator@2.2.3/+esm"
28+
import multiform-validator from "https://cdn.jsdelivr.net/npm/multiform-validator@2.2.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/multiform-validator@2.2.3/+esm";
74+
import multiformValidator from "https://cdn.jsdelivr.net/npm/multiform-validator@2.2.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
106 Bytes
Loading

tests/generateFakeImage.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const fs = require('fs');
2+
3+
// Assinatura PNG: [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]
4+
const pngSignature = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
5+
const fakePNG = new Uint8Array([...pngSignature, ...new Array(100).fill(0)]); // 100 bytes extras vazios
6+
fs.writeFileSync('fake.png', fakePNG);
7+
8+
// Assinatura GIF: [0x47, 0x49, 0x46, 0x38, 0x37, 0x61] (GIF89a) ou [0x47, 0x49, 0x46, 0x38, 0x37, 0x61] (GIF87a)
9+
const gifSignature = [0x47, 0x49, 0x46, 0x38, 0x37, 0x61];
10+
const fakeGIF = new Uint8Array([...gifSignature, ...new Array(100).fill(0)]); // 100 bytes extras vazios
11+
fs.writeFileSync('fake87a.gif', fakeGIF);
12+
13+
// Assinatura GIF89a: [0x47, 0x49, 0x46, 0x38, 0x39, 0x61]
14+
const gif89aSignature = [0x47, 0x49, 0x46, 0x38, 0x39, 0x61];
15+
const fakeGIF89a = new Uint8Array([...gif89aSignature, ...new Array(100).fill(0)]); // 100 bytes extras vazios
16+
fs.writeFileSync('fake89a.gif', fakeGIF89a);
17+
18+
// Assinatura JPEG: [0xFF, 0xD8, 0xFF]
19+
const jpegSignature = [0xFF, 0xD8, 0xFF];
20+
const fakeJPEG = new Uint8Array([...jpegSignature, ...new Array(100).fill(0)]); // 100 bytes extras vazios
21+
fs.writeFileSync('fake.jpeg', fakeJPEG);

tests/src/isValidImage.test.ts

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ describe("isValidImage", () => {
1010
fileBuffer5: Buffer,
1111
fileBuffer6: Buffer,
1212
fileBuffer7: Buffer,
13-
fileBuffer8: Buffer;
13+
fileBuffer8: Buffer,
14+
fileBuffer9: Buffer,
15+
fileBuffer10: Buffer,
16+
fileBuffer11: Buffer,
17+
fileBuffer12: Buffer;
1418

1519
beforeAll(() => {
1620
fileBuffer1 = fs.readFileSync(
@@ -93,6 +97,47 @@ describe("isValidImage", () => {
9397
"valid.gif",
9498
),
9599
); // Valid GIF image
100+
fileBuffer9 = fs.readFileSync(
101+
path.join(
102+
__dirname,
103+
"..",
104+
"assets",
105+
"isValidImage",
106+
"invalid",
107+
"fake.png",
108+
),
109+
); // Invalid Png image
110+
fileBuffer10 = fs.readFileSync(
111+
path.join(
112+
__dirname,
113+
"..",
114+
"assets",
115+
"isValidImage",
116+
"invalid",
117+
"fake.jpeg",
118+
),
119+
); // Invalid JPEG image
120+
fileBuffer11 = fs.readFileSync(
121+
path.join(
122+
__dirname,
123+
"..",
124+
"assets",
125+
"isValidImage",
126+
"invalid",
127+
"fake87a.gif",
128+
),
129+
); // Invalid GIF 87a image
130+
131+
fileBuffer12 = fs.readFileSync(
132+
path.join(
133+
__dirname,
134+
"..",
135+
"assets",
136+
"isValidImage",
137+
"invalid",
138+
"fake89a.gif",
139+
),
140+
); // Invalid GIF 89a image
96141
});
97142

98143
it("should return false for an empty buffer", () => {
@@ -202,5 +247,29 @@ describe("isValidImage", () => {
202247
const result = isValidImage(fileBuffer8, { exclude: ["ico"] });
203248
expect(result).toBe(true);
204249
});
250+
251+
it("should return false for an invalid PNG image with headers corrects", () => {
252+
const result = isValidImage(fileBuffer9, { exclude: ["gif", "ico", "jpeg"]});
253+
expect(result).toBe(false);
254+
});
255+
256+
it("should return false if validations break in png image", () => {
257+
expect(isValidImage("null" as any)).toBe(false);
258+
});
259+
260+
it("should return false for an invalid JPEG image with headers corrects", () => {
261+
const result = isValidImage(fileBuffer10, { exclude: ["gif", "ico", "png"]});
262+
expect(result).toBe(false);
263+
});
264+
265+
it("should return false for an invalid GIF image with headers corrects", () => {
266+
const result = isValidImage(fileBuffer11, { exclude: ["jpeg", "ico", "png"]});
267+
expect(result).toBe(false);
268+
});
269+
270+
it("should return false for an invalid GIF image with headers corrects", () => {
271+
const result = isValidImage(fileBuffer12, { exclude: ["jpeg", "ico", "png"]});
272+
expect(result).toBe(false);
273+
});
205274
});
206275
export default isValidImage;

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
2626

2727
/* Modules */
28-
"module": "ES6" /* Specify what module code is generated. */,
28+
"module": "CommonJS" /* Specify what module code is generated. */,
2929
// "rootDir": "./src", /* Specify the root folder within your source files. */
3030
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
3131
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */

0 commit comments

Comments
 (0)