Skip to content

Commit d5bd15f

Browse files
committed
feat: improve input data validation
Adds a check of the input value for an empty string.
1 parent ce1bff9 commit d5bd15f

File tree

5 files changed

+66
-20
lines changed

5 files changed

+66
-20
lines changed

src/index.spec.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import * as fg from '.';
77
describe('Package', () => {
88
describe('.sync', () => {
99
it('should throw an error when input values can not pass validation', () => {
10+
const message = 'Patterns must be a string (non empty) or an array of strings';
11+
1012
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11-
assert.throws(() => fg.sync(null as any), /TypeError: Patterns must be a string or an array of strings/);
13+
assert.throws(() => fg.sync(null as any), { message });
14+
assert.throws(() => fg.sync(''), { message });
1215
});
1316

1417
it('should returns entries', () => {
@@ -49,13 +52,11 @@ describe('Package', () => {
4952

5053
describe('.async', () => {
5154
it('should throw an error when input values can not pass validation', async () => {
52-
try {
53-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
54-
await fg(null as any);
55-
throw new Error('An unexpected error was found.');
56-
} catch (error) {
57-
assert.strictEqual((error as Error).toString(), 'TypeError: Patterns must be a string or an array of strings');
58-
}
55+
const message = 'Patterns must be a string (non empty) or an array of strings';
56+
57+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
58+
await assert.rejects(() => fg(null as any), { message });
59+
await assert.rejects(() => fg(''), { message });
5960
});
6061

6162
it('should returns entries', async () => {
@@ -96,8 +97,11 @@ describe('Package', () => {
9697

9798
describe('.stream', () => {
9899
it('should throw an error when input values can not pass validation', () => {
100+
const message = 'Patterns must be a string (non empty) or an array of strings';
101+
99102
// eslint-disable-next-line @typescript-eslint/no-explicit-any
100-
assert.throws(() => fg.stream(null as any), /TypeError: Patterns must be a string or an array of strings/);
103+
assert.throws(() => fg.stream(null as any), { message });
104+
assert.throws(() => fg.stream(''), { message });
101105
});
102106

103107
it('should returns entries', (done) => {
@@ -152,8 +156,11 @@ describe('Package', () => {
152156

153157
describe('.generateTasks', () => {
154158
it('should throw an error when input values can not pass validation', () => {
159+
const message = 'Patterns must be a string (non empty) or an array of strings';
160+
155161
// eslint-disable-next-line @typescript-eslint/no-explicit-any
156-
assert.throws(() => fg.generateTasks(null as any), /TypeError: Patterns must be a string or an array of strings/);
162+
assert.throws(() => fg.generateTasks(null as any), { message });
163+
assert.throws(() => fg.generateTasks(''), { message });
157164
});
158165

159166
it('should return tasks', () => {

src/index.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,13 @@ function getWorks<T>(source: PatternInternal | PatternInternal[], _Provider: new
8989
return tasks.map(provider.read, provider);
9090
}
9191

92-
function assertPatternsInput(source: unknown): void | never {
93-
if (([] as unknown[]).concat(source).every(isString)) {
94-
return;
95-
}
96-
97-
throw new TypeError('Patterns must be a string or an array of strings');
98-
}
92+
function assertPatternsInput(input: unknown): void | never {
93+
const source = ([] as unknown[]).concat(input);
94+
const isValidSource = source.every((item) => utils.string.isString(item) && !utils.string.isEmpty(item));
9995

100-
function isString(source: unknown): source is string {
101-
return typeof source === 'string';
96+
if (!isValidSource) {
97+
throw new TypeError('Patterns must be a string (non empty) or an array of strings');
98+
}
10299
}
103100

104101
export = FastGlob;

src/utils/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import * as fs from './fs';
44
import * as path from './path';
55
import * as pattern from './pattern';
66
import * as stream from './stream';
7+
import * as string from './string';
78

89
export {
910
array,
1011
errno,
1112
fs,
1213
path,
1314
pattern,
14-
stream
15+
stream,
16+
string
1517
};

src/utils/string.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as assert from 'assert';
2+
3+
import * as util from './string';
4+
5+
describe('Utils → String', () => {
6+
describe('.isString', () => {
7+
it('should return true', () => {
8+
const actual = util.isString('');
9+
10+
assert.ok(actual);
11+
});
12+
13+
it('should return false', () => {
14+
const actual = util.isString(undefined as unknown as string);
15+
16+
assert.ok(!actual);
17+
});
18+
});
19+
20+
describe('.isEmpty', () => {
21+
it('should return true', () => {
22+
const actual = util.isEmpty('');
23+
24+
assert.ok(actual);
25+
});
26+
27+
it('should return false', () => {
28+
const actual = util.isEmpty('string');
29+
30+
assert.ok(!actual);
31+
});
32+
});
33+
});

src/utils/string.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function isString(input: unknown): input is string {
2+
return typeof input === 'string';
3+
}
4+
5+
export function isEmpty(input: string): boolean {
6+
return input === '';
7+
}

0 commit comments

Comments
 (0)