Skip to content

Commit

Permalink
Merge pull request #250 from mrmlnc/ISSUE-247_input_data_validation
Browse files Browse the repository at this point in the history
ISSUE-247: improve input data validation
  • Loading branch information
mrmlnc committed Jan 29, 2020
2 parents ce1bff9 + d5bd15f commit d16282c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 20 deletions.
27 changes: 17 additions & 10 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import * as fg from '.';
describe('Package', () => {
describe('.sync', () => {
it('should throw an error when input values can not pass validation', () => {
const message = 'Patterns must be a string (non empty) or an array of strings';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
assert.throws(() => fg.sync(null as any), /TypeError: Patterns must be a string or an array of strings/);
assert.throws(() => fg.sync(null as any), { message });
assert.throws(() => fg.sync(''), { message });
});

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

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

// eslint-disable-next-line @typescript-eslint/no-explicit-any
await assert.rejects(() => fg(null as any), { message });
await assert.rejects(() => fg(''), { message });
});

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

describe('.stream', () => {
it('should throw an error when input values can not pass validation', () => {
const message = 'Patterns must be a string (non empty) or an array of strings';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
assert.throws(() => fg.stream(null as any), /TypeError: Patterns must be a string or an array of strings/);
assert.throws(() => fg.stream(null as any), { message });
assert.throws(() => fg.stream(''), { message });
});

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

describe('.generateTasks', () => {
it('should throw an error when input values can not pass validation', () => {
const message = 'Patterns must be a string (non empty) or an array of strings';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
assert.throws(() => fg.generateTasks(null as any), /TypeError: Patterns must be a string or an array of strings/);
assert.throws(() => fg.generateTasks(null as any), { message });
assert.throws(() => fg.generateTasks(''), { message });
});

it('should return tasks', () => {
Expand Down
15 changes: 6 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,13 @@ function getWorks<T>(source: PatternInternal | PatternInternal[], _Provider: new
return tasks.map(provider.read, provider);
}

function assertPatternsInput(source: unknown): void | never {
if (([] as unknown[]).concat(source).every(isString)) {
return;
}

throw new TypeError('Patterns must be a string or an array of strings');
}
function assertPatternsInput(input: unknown): void | never {
const source = ([] as unknown[]).concat(input);
const isValidSource = source.every((item) => utils.string.isString(item) && !utils.string.isEmpty(item));

function isString(source: unknown): source is string {
return typeof source === 'string';
if (!isValidSource) {
throw new TypeError('Patterns must be a string (non empty) or an array of strings');
}
}

export = FastGlob;
4 changes: 3 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import * as fs from './fs';
import * as path from './path';
import * as pattern from './pattern';
import * as stream from './stream';
import * as string from './string';

export {
array,
errno,
fs,
path,
pattern,
stream
stream,
string
};
33 changes: 33 additions & 0 deletions src/utils/string.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as assert from 'assert';

import * as util from './string';

describe('Utils → String', () => {
describe('.isString', () => {
it('should return true', () => {
const actual = util.isString('');

assert.ok(actual);
});

it('should return false', () => {
const actual = util.isString(undefined as unknown as string);

assert.ok(!actual);
});
});

describe('.isEmpty', () => {
it('should return true', () => {
const actual = util.isEmpty('');

assert.ok(actual);
});

it('should return false', () => {
const actual = util.isEmpty('string');

assert.ok(!actual);
});
});
});
7 changes: 7 additions & 0 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function isString(input: unknown): input is string {
return typeof input === 'string';
}

export function isEmpty(input: string): boolean {
return input === '';
}

0 comments on commit d16282c

Please sign in to comment.