diff --git a/src/random.ts b/src/random.ts index c2d8ecb2bb4..dd58c3ce821 100644 --- a/src/random.ts +++ b/src/random.ts @@ -479,11 +479,17 @@ export class Random { 'y', 'z', ]; - if (options) { - if (options.bannedChars) { - charsArray = arrayRemove(charsArray, options.bannedChars); - } + + if (options.bannedChars) { + charsArray = arrayRemove(charsArray, options.bannedChars); } + + if (charsArray.length === 0) { + throw new Error( + 'Unable to generate string, because all possible characters are banned.' + ); + } + for (let i = 0; i < count; i++) { wholeString += this.faker.random.arrayElement(charsArray); } diff --git a/test/random.spec.ts b/test/random.spec.ts index 07b2a0f5b6b..f43cc5003d2 100644 --- a/test/random.spec.ts +++ b/test/random.spec.ts @@ -210,7 +210,7 @@ describe('random', () => { expect(alphaText).match(/[b-oq-z]/); }); - it.todo('should throw if all possible characters being banned', () => { + it('should throw if all possible characters being banned', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz0123456789'.split(''); expect(() => faker.random.alphaNumeric(5, {