Skip to content

Commit

Permalink
Add error on bad input values
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Sep 12, 2024
1 parent c827fce commit 8f09549
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ function pathToRegexp(path, keys, options) {
return new RegExp(path.join('|'), flags);
}

if (typeof path !== 'string') {
throw new TypeError('path must be a string, array of strings, or regular expression');
}

path = path.replace(
/\\.|(\/)?(\.)?:(\w+)(\(.*?\))?(\*)?(\?)?|[.*]|\/\(/g,
function (match, slash, format, key, capture, star, optional, offset) {
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ var pathToRegExp = require('./');
var assert = require('assert');

describe('path-to-regexp', function () {
it('should throw on invalid input', function () {
assert.throws(function () {
pathToRegExp(function () {});
}, /path must be a string, array of strings, or regular expression/);
});

describe('strings', function () {
it('should match simple paths', function () {
var params = [];
Expand Down

0 comments on commit 8f09549

Please sign in to comment.