diff --git a/Readme.md b/Readme.md index 0b2f47c..4808f95 100644 --- a/Readme.md +++ b/Readme.md @@ -193,7 +193,7 @@ const toPathRegexp = pathToRegexp.compile('/user/:id(\\d+)') toPathRegexp({ id: 123 }) //=> "/user/123" toPathRegexp({ id: '123' }) //=> "/user/123" toPathRegexp({ id: 'abc' }) //=> Throws `TypeError`. -toPathRegexp({ id: 'abc' }, { noValidate: true }) //=> "/user/abc" +toPathRegexp({ id: 'abc' }, { validate: true }) //=> "/user/abc" ``` **Note:** The generated function will throw on invalid input. It will do all necessary checks to ensure the generated path is valid. This method only works with strings. diff --git a/index.d.ts b/index.d.ts index a25edef..18b9614 100644 --- a/index.d.ts +++ b/index.d.ts @@ -81,9 +81,9 @@ declare namespace pathToRegexp { */ encode?: (value: string, token: Key) => string; /** - * When `true` the function can produce an invalid (unmatched) path. (default: `false`) + * When `false` the function can produce an invalid (unmatched) path. (default: `true`) */ - noValidate?: boolean; + validate?: boolean; } export type Token = string | Key; diff --git a/index.js b/index.js index 460c600..7426f4f 100644 --- a/index.js +++ b/index.js @@ -137,7 +137,7 @@ function tokensToFunction (tokens, options) { return function (data, options) { var path = '' var encode = (options && options.encode) || encodeURIComponent - var noValidate = (options && options.noValidate) || false + var validate = options ? options.validate !== false : true for (var i = 0; i < tokens.length; i++) { var token = tokens[i] @@ -164,7 +164,7 @@ function tokensToFunction (tokens, options) { for (var j = 0; j < value.length; j++) { segment = encode(value[j], token) - if (!noValidate && !matches[i].test(segment)) { + if (validate && !matches[i].test(segment)) { throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '"') } @@ -177,7 +177,7 @@ function tokensToFunction (tokens, options) { if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { segment = encode(String(value), token) - if (!noValidate && !matches[i].test(segment)) { + if (validate && !matches[i].test(segment)) { throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but got "' + segment + '"') } diff --git a/test.ts b/test.ts index 66aab8c..62784ce 100644 --- a/test.ts +++ b/test.ts @@ -1051,7 +1051,7 @@ var TESTS: Test[] = [ ], [ [{ test: 'abc' }, null], - [{ test: 'abc' }, '/abc', { noValidate: true }], + [{ test: 'abc' }, '/abc', { validate: false }], [{ test: '123' }, '/123'] ] ], @@ -1122,9 +1122,9 @@ var TESTS: Test[] = [ ], [ [{ route: '' }, null], - [{ route: '' }, '/', { noValidate: true }], + [{ route: '' }, '/', { validate: false }], [{ route: '123' }, null], - [{ route: '123' }, '/123', { noValidate: true }], + [{ route: '123' }, '/123', { validate: false }], [{ route: 'abc' }, '/abc'] ] ], @@ -1149,7 +1149,7 @@ var TESTS: Test[] = [ [ [{ route: 'this' }, '/this'], [{ route: 'foo' }, null], - [{ route: 'foo' }, '/foo', { noValidate: true }], + [{ route: 'foo' }, '/foo', { validate: false }], [{ route: 'that' }, '/that'] ] ], @@ -1179,9 +1179,9 @@ var TESTS: Test[] = [ [{ path: ['abc', 'xyz'] }, '/abc/xyz'], [{ path: ['xyz', 'abc', 'xyz'] }, '/xyz/abc/xyz'], [{ path: 'abc123' }, null], - [{ path: 'abc123' }, '/abc123', { noValidate: true }], + [{ path: 'abc123' }, '/abc123', { validate: false }], [{ path: 'abcxyz' }, null], - [{ path: 'abcxyz' }, '/abcxyz', { noValidate: true }], + [{ path: 'abcxyz' }, '/abcxyz', { validate: false }], ] ], @@ -2702,7 +2702,7 @@ var TESTS: Test[] = [ [{ test: 'ABC' }, '/ABC'] ] ], - + ] /**