Skip to content

Commit

Permalink
Make the argument validation stricter
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
sindresorhus committed Jun 12, 2019
1 parent 6836892 commit ab8ab11
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
8 changes: 4 additions & 4 deletions index.js
Expand Up @@ -38,16 +38,16 @@ const allowedTypes = [
];

module.exports = ({length, type, characters}) => {
if (!Number.isFinite(length)) {
throw new TypeError('Expected a finite number');
if (!(length >= 0 && Number.isFinite(length))) {
throw new TypeError('Expected a `length` to be a non-negative finite number');
}

if (type !== undefined && characters !== undefined) {
throw new TypeError('Expected either type or characters');
throw new TypeError('Expected either `type` or `characters`');
}

if (characters !== undefined && typeof characters !== 'string') {
throw new TypeError('Expected characters to be string');
throw new TypeError('Expected `characters` to be string');
}

if (!allowedTypes.includes(type)) {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -33,11 +33,11 @@
"hex"
],
"dependencies": {
"type-fest": "^0.4.1"
"type-fest": "^0.5.2"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"ava": "^2.1.0",
"tsd": "^0.7.3",
"xo": "^0.24.0"
}
}
4 changes: 4 additions & 0 deletions test.js
Expand Up @@ -41,6 +41,10 @@ test('argument errors', t => {
cryptoRandomString({length: Infinity});
});

t.throws(() => {
cryptoRandomString({length: -1});
});

t.throws(() => {
cryptoRandomString({length: 0, type: 'hex', characters: '1234'});
});
Expand Down

0 comments on commit ab8ab11

Please sign in to comment.