Skip to content

Commit

Permalink
feat: new file validation options and improve security validations pu…
Browse files Browse the repository at this point in the history
…blish 2.1.0v
  • Loading branch information
gabriel-logan committed May 23, 2024
1 parent 1d32b0a commit 995e236
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 51 deletions.
10 changes: 5 additions & 5 deletions packages/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ Feel free to find bugs and report them to me. Your feedback is highly appreciate

jsDelivr:
```bash
https://cdn.jsdelivr.net/npm/multiform-validator@2.0.6/dist/bundle.min.js
https://cdn.jsdelivr.net/npm/multiform-validator@2.1.0/dist/bundle.min.js
```
```html
<script src="https://cdn.jsdelivr.net/npm/multiform-validator@2.0.6/dist/bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/multiform-validator@2.1.0/dist/bundle.min.js"></script>
```

unpkg:
```bash
https://unpkg.com/multiform-validator@2.0.6/dist/bundle.js
https://unpkg.com/multiform-validator@2.1.0/dist/bundle.js
```
```html
<script src="https://unpkg.com/multiform-validator@2.0.6/dist/bundle.js"></script>
<script src="https://unpkg.com/multiform-validator@2.1.0/dist/bundle.js"></script>
```

### Example of use with CDN

```html
<script src="https://cdn.jsdelivr.net/npm/multiform-validator@2.0.6/dist/bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/multiform-validator@2.1.0/dist/bundle.min.js"></script>
<script>
const emailResult = isEmail('123456');
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "multiform-validator",
"version": "2.0.6",
"version": "2.1.0",
"description": "Javascript library made to validate, several form fields, such as: email, phone, password, cpf etc.",
"main": "./dist/index.js",
"types": "./types/index.d.ts",
"unpkg": "./dist/bundle.js",
"scripts": {
"test": "jest --coverage",
"test:file": "jest tests/src/isEmail.test --watch",
"test:file": "jest tests/src/isDecimal.test --watch",
"test:watch": "jest --watch",
"build:types": "tsc -p tsconfig.types.json",
"build:src": "tsc",
Expand Down
8 changes: 8 additions & 0 deletions packages/typescript/src/isDecimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
*/
function isDecimal(value: string | number): boolean {
let getValued: string | number = value;
if (typeof getValued === "number" && Number.isNaN(getValued)) {
throw new TypeError("Input value must not be NaN.");
}

if (typeof getValued === "number" && !isFinite(getValued)) {
throw new TypeError("Input value must not be Infinity, -Infinity or NaN.");
}

if (typeof getValued !== "string") {
if (typeof getValued === "number") {
if (Number.isInteger(getValued)) {
Expand Down
44 changes: 32 additions & 12 deletions packages/typescript/src/isValidVideo/validateMp4.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
export default function validateMp4(fileBuffer: Buffer): boolean {
const isMp4: boolean =
fileBuffer[0] === 0x00 &&
fileBuffer[1] === 0x00 &&
fileBuffer[2] === 0x00 &&
fileBuffer[3] === 0x20 &&
fileBuffer[4] === 0x66 &&
fileBuffer[5] === 0x74 &&
fileBuffer[6] === 0x79 &&
fileBuffer[7] === 0x70 &&
fileBuffer[8] === 0x6d &&
fileBuffer[9] === 0x70 &&
fileBuffer[10] === 0x34 &&
fileBuffer[11] === 0x32;
(fileBuffer[0] === 0x00 &&
fileBuffer[1] === 0x00 &&
fileBuffer[2] === 0x00 &&
fileBuffer[3] === 0x20 &&
fileBuffer[4] === 0x66 &&
fileBuffer[5] === 0x74 &&
fileBuffer[6] === 0x79 &&
fileBuffer[7] === 0x70 &&
fileBuffer[8] === 0x6d &&
fileBuffer[9] === 0x70 &&
fileBuffer[10] === 0x34 &&
fileBuffer[11] === 0x32) ||
(fileBuffer[0] === 0x00 &&
fileBuffer[1] === 0x00 &&
fileBuffer[2] === 0x00 &&
fileBuffer[3] === 0x18 &&
fileBuffer[4] === 0x66 &&
fileBuffer[5] === 0x74 &&
fileBuffer[6] === 0x79 &&
fileBuffer[7] === 0x70) ||
(fileBuffer[0] === 0x00 &&
fileBuffer[1] === 0x00 &&
fileBuffer[2] === 0x00 &&
fileBuffer[3] === 0x1c &&
fileBuffer[4] === 0x66 &&
fileBuffer[5] === 0x74 &&
fileBuffer[6] === 0x79 &&
fileBuffer[7] === 0x70 &&
fileBuffer[8] === 0x69 &&
fileBuffer[9] === 0x73 &&
fileBuffer[10] === 0x6f &&
fileBuffer[11] === 0x6d);

return isMp4;
}
42 changes: 39 additions & 3 deletions packages/typescript/tests/src/isDecimal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import isDecimal from '../../src/isDecimal';

describe('isDecimal', () => {
const errorToThrow = "Input value must be a string or a number."; // Mensagem de erro a ser lançada

it('should return true when the input is a valid decimal number', () => {
const result = isDecimal('123.45');
expect(result).toBe(true);
Expand All @@ -23,18 +25,52 @@ describe('isDecimal', () => {
});

it('should throw error when the input is an array', () => {
expect(() => isDecimal([] as any)).toThrow("Input value must be a string or a number.");
expect(() => isDecimal([] as any)).toThrow(errorToThrow);
});

it('should throw error when the input is an object', () => {
expect(() => isDecimal({} as any)).toThrow("Input value must be a string or a number.");
expect(() => isDecimal({} as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a boolean', () => {
expect(() => isDecimal(true as any)).toThrow("Input value must be a string or a number.");
expect(() => isDecimal(true as any)).toThrow(errorToThrow);
});

it('should throw error when the input is empty', () => {
expect(() => isDecimal('')).toThrow("Input value must not be an empty string.");
});

it('should throw error when the input is a whitespace', () => {
expect(() => isDecimal(' ')).toThrow("Input value must not be an empty string.");
});

it('should throw error when the input is a null', () => {
expect(() => isDecimal(null as any)).toThrow(errorToThrow);
});

it('should throw error when the input is undefined', () => {
expect(() => isDecimal(undefined as any)).toThrow(errorToThrow);
});

it('should throw error when the input is NaN', () => {
expect(() => isDecimal(NaN as any)).toThrow("Input value must not be NaN.");
});

it('should throw error when the input is Infinity', () => {
expect(() => isDecimal(Infinity as any)).toThrow("Input value must not be Infinity, -Infinity or NaN.");
});

it('should throw error when the input is -Infinity', () => {
expect(() => isDecimal(-Infinity as any)).toThrow("Input value must not be Infinity, -Infinity or NaN.");
});

it('should throw error when the input is a function', () => {
function func() {}

expect(() => isDecimal(func() as any) as any).toThrow(errorToThrow);
});

it('should throw error when the input is a symbol', () => {
expect(() => isDecimal(Symbol() as any) as any).toThrow(errorToThrow);
});
});
80 changes: 71 additions & 9 deletions packages/typescript/tests/src/isValidAudio.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,81 @@
import isValidAudio from '../../src/isValidAudio';
import path from 'path';
import * as fs from 'fs';

describe('isValidAudio', () => {
it('should return true for valid MP3 file', () => {
const fileBuffer = Buffer.from([0x49, 0x44, 0x33]);
expect(isValidAudio(fileBuffer)).toBe(true);
let fileBuffer1: Buffer, fileBuffer2: Buffer, fileBuffer3: Buffer, fileBuffer4: Buffer;

beforeAll(() => {
fileBuffer1 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidAudio', 'invalid', 'invalid.mp3')); // Invalid MP3 image
fileBuffer2 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidAudio', 'invalid', 'invalid.wav')); // Invalid WAV image
fileBuffer3 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidAudio', 'valid', 'valid.mp3')); // valid MP3 image
fileBuffer4 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidAudio', 'valid', 'valid.wav')); // valid WAV image
});

it('should return false for an empty buffer', () => {
const fileBuffer = Buffer.from([]);
const result = isValidAudio(fileBuffer);
expect(result).toBe(false);
});

it('should return false for an invalid MP3 audio', () => {
const result = isValidAudio(fileBuffer1);
expect(result).toBe(false);
});

it('should return false for an invalid WAV audio', () => {
const result = isValidAudio(fileBuffer2);
expect(result).toBe(false);
});

it('should return true for a valid MP3 audio', () => {
const result = isValidAudio(fileBuffer3);
expect(result).toBe(true);
});

it('should return true for a valid WAV audio', () => {
const result = isValidAudio(fileBuffer4);
expect(result).toBe(true);
});

it('should return false when excluding WAV files', () => {
const result = isValidAudio(fileBuffer4, { exclude: ['wav'] });
expect(result).toBe(false);
});

it('should return false when excluding MP3 files', () => {
const result = isValidAudio(fileBuffer3, { exclude: ['mp3'] });
expect(result).toBe(false);
});

it('should return true when excluding WAV files', () => {
const result = isValidAudio(fileBuffer3, { exclude: ['wav'] });
expect(result).toBe(true);
});

it('should return true when excluding MP3 files', () => {
const result = isValidAudio(fileBuffer4, { exclude: ['mp3'] });
expect(result).toBe(true);
});

it('should return false when excluding WAV files and MP3 files', () => {
const result = isValidAudio(fileBuffer4, { exclude: ['wav', 'mp3'] });
expect(result).toBe(false);
});

it('should return false when excluding WAV files and MP3 files', () => {
const result = isValidAudio(fileBuffer3, { exclude: ['wav', 'mp3'] });
expect(result).toBe(false);
});

it('should return true for valid WAV file', () => {
const fileBuffer = Buffer.from([0x52, 0x49, 0x46, 0x46]);
expect(isValidAudio(fileBuffer)).toBe(true);
it('should return false when excluding WAV files and MP3 files', () => {
const result = isValidAudio(fileBuffer2, { exclude: ['wav', 'mp3'] });
expect(result).toBe(false);
});

it('should return false for invalid file', () => {
const fileBuffer = Buffer.from([0x00, 0x00, 0x00, 0x00]);
expect(isValidAudio(fileBuffer)).toBe(false);
it('should return false when excluding WAV files and MP3 files', () => {
const result = isValidAudio(fileBuffer1, { exclude: ['wav', 'mp3'] });
expect(result).toBe(false);
});
});
export default isValidAudio;
115 changes: 95 additions & 20 deletions packages/typescript/tests/src/isValidVideo.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,106 @@
import isValidVideo from '../../src/isValidVideo';
import path from 'path';
import * as fs from 'fs';

describe('isValidVideo', () => {
it('should return true for valid MP4 file', () => {
const fileBuffer = Buffer.from([
0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70, 0x6d, 0x70, 0x34, 0x32
]);
expect(isValidVideo(fileBuffer)).toBe(true);
let fileBuffer1: Buffer, fileBuffer2: Buffer, fileBuffer3: Buffer, fileBuffer4: Buffer, fileBuffer5: Buffer, fileBuffer6: Buffer;

beforeAll(() => {
fileBuffer1 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidVideo', 'invalid', 'invalid.mkv')); // Invalid MKV image
fileBuffer2 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidVideo', 'invalid', 'invalid.mp4')); // Invalid MP4 image
fileBuffer3 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidVideo', 'invalid', 'invalid.mov')); // Invalid MOV image
fileBuffer4 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidVideo', 'valid', 'valid.mkv')); // valid MKV image
fileBuffer5 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidVideo', 'valid', 'valid.mp4')); // valid MP4 image
fileBuffer6 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidVideo', 'valid', 'valid.mov')); // valid MOV image
});

it('should return false for an empty buffer', () => {
const fileBuffer = Buffer.from([]);
const result = isValidVideo(fileBuffer);
expect(result).toBe(false);
});

it('should return false for an invalid MKV video', () => {
const result = isValidVideo(fileBuffer1);
expect(result).toBe(false);
});

it('should return false for an invalid MP4 video', () => {
const result = isValidVideo(fileBuffer2);
expect(result).toBe(false);
});

it('should return false for an invalid MOV video', () => {
const result = isValidVideo(fileBuffer3);
expect(result).toBe(false);
});

it('should return true for a valid MKV video', () => {
const result = isValidVideo(fileBuffer4);
expect(result).toBe(true);
});

it('should return true for a valid MP4 video', () => {
const result = isValidVideo(fileBuffer5);
expect(result).toBe(true);
});

it('should return true for a valid MOV video', () => {
const result = isValidVideo(fileBuffer6);
expect(result).toBe(true);
});

it('should return false when excluding MP4 files', () => {
const result = isValidVideo(fileBuffer5, { exclude: ['mp4'] });
expect(result).toBe(false);
});

it('should return false when excluding MOV files', () => {
const result = isValidVideo(fileBuffer6, { exclude: ['mov'] });
expect(result).toBe(false);
});

it('should return false when excluding MKV files', () => {
const result = isValidVideo(fileBuffer4, { exclude: ['mkv'] });
expect(result).toBe(false);
});

it('should return true when excluding MP4 files', () => {
const result = isValidVideo(fileBuffer6, { exclude: ['mp4'] });
expect(result).toBe(true);
});

it('should return true when excluding MOV files', () => {
const result = isValidVideo(fileBuffer4, { exclude: ['mov'] });
expect(result).toBe(true);
});

it('should return true when excluding MKV files', () => {
const result = isValidVideo(fileBuffer5, { exclude: ['mkv'] });
expect(result).toBe(true);
});

it('should return false when excluding MP4 files and MOV files', () => {
const result = isValidVideo(fileBuffer5, { exclude: ['mp4', 'mov'] });
expect(result).toBe(false);
});

it('should return true for valid MOV file', () => {
const fileBuffer = Buffer.from([
0x00, 0x00, 0x00, 0x14, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20
]);
expect(isValidVideo(fileBuffer)).toBe(true);
it('should return false when excluding MKV files and MP4 files', () => {
const result = isValidVideo(fileBuffer5, { exclude: ['mkv', 'mp4'] });
expect(result).toBe(false);
});

it('should return true for valid MKV file', () => {
const fileBuffer = Buffer.from([
0x1a, 0x45, 0xdf, 0xa3
]);
expect(isValidVideo(fileBuffer)).toBe(true);
it('should return false when excluding MKV files, MP4 files, and MOV files', () => {
const result = isValidVideo(fileBuffer5, { exclude: ['mkv', 'mp4', 'mov'] });
expect(result).toBe(false);
});

it('should return false for invalid file', () => {
const fileBuffer = Buffer.from([
0x00, 0x00, 0x00, 0x00
]);
expect(isValidVideo(fileBuffer)).toBe(false);
it('should return false when excluding all video files', () => {
const result1 = isValidVideo(fileBuffer4, { exclude: ['mkv', 'mp4', 'mov'] });
const result2 = isValidVideo(fileBuffer5, { exclude: ['mkv', 'mp4', 'mov'] });
const result3 = isValidVideo(fileBuffer6, { exclude: ['mkv', 'mp4', 'mov'] });
expect(result3).toBe(false);
expect(result2).toBe(false);
expect(result1).toBe(false);
});
});

0 comments on commit 995e236

Please sign in to comment.