Skip to content

Commit 1fbc2f3

Browse files
helloyou2012blakeembrey
authored andcommitted
Add noValidate support to path function (#178)
1 parent b5156c8 commit 1fbc2f3

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ const toPathRegexp = pathToRegexp.compile('/user/:id(\\d+)')
192192
toPathRegexp({ id: 123 }) //=> "/user/123"
193193
toPathRegexp({ id: '123' }) //=> "/user/123"
194194
toPathRegexp({ id: 'abc' }) //=> Throws `TypeError`.
195+
toPathRegexp({ id: 'abc' }, { noValidate: true }) //=> "/user/abc"
195196
```
196197

197198
**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.

index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ declare namespace pathToRegexp {
7373
* Function for encoding input strings for output.
7474
*/
7575
encode?: (value: string, token: Key) => string;
76+
/**
77+
* When `true` the function can produce an invalid (unmatched) path. (default: `false`)
78+
*/
79+
noValidate?: boolean;
7680
}
7781

7882
export type Token = string | Key;

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ function tokensToFunction (tokens) {
137137
return function (data, options) {
138138
var path = ''
139139
var encode = (options && options.encode) || encodeURIComponent
140+
var noValidate = (options && options.noValidate) || false
140141

141142
for (var i = 0; i < tokens.length; i++) {
142143
var token = tokens[i]
@@ -163,7 +164,7 @@ function tokensToFunction (tokens) {
163164
for (var j = 0; j < value.length; j++) {
164165
segment = encode(value[j], token)
165166

166-
if (!matches[i].test(segment)) {
167+
if (!noValidate && !matches[i].test(segment)) {
167168
throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '"')
168169
}
169170

@@ -176,7 +177,7 @@ function tokensToFunction (tokens) {
176177
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
177178
segment = encode(String(value), token)
178179

179-
if (!matches[i].test(segment)) {
180+
if (!noValidate && !matches[i].test(segment)) {
180181
throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but got "' + segment + '"')
181182
}
182183

test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,7 @@ var TESTS: Test[] = [
10511051
],
10521052
[
10531053
[{ test: 'abc' }, null],
1054+
[{ test: 'abc' }, '/abc', { noValidate: true }],
10541055
[{ test: '123' }, '/123']
10551056
]
10561057
],
@@ -1121,7 +1122,9 @@ var TESTS: Test[] = [
11211122
],
11221123
[
11231124
[{ route: '' }, null],
1125+
[{ route: '' }, '/', { noValidate: true }],
11241126
[{ route: '123' }, null],
1127+
[{ route: '123' }, '/123', { noValidate: true }],
11251128
[{ route: 'abc' }, '/abc']
11261129
]
11271130
],
@@ -1146,6 +1149,7 @@ var TESTS: Test[] = [
11461149
[
11471150
[{ route: 'this' }, '/this'],
11481151
[{ route: 'foo' }, null],
1152+
[{ route: 'foo' }, '/foo', { noValidate: true }],
11491153
[{ route: 'that' }, '/that']
11501154
]
11511155
],
@@ -1175,7 +1179,9 @@ var TESTS: Test[] = [
11751179
[{ path: ['abc', 'xyz'] }, '/abc/xyz'],
11761180
[{ path: ['xyz', 'abc', 'xyz'] }, '/xyz/abc/xyz'],
11771181
[{ path: 'abc123' }, null],
1178-
[{ path: 'abcxyz' }, null]
1182+
[{ path: 'abc123' }, '/abc123', { noValidate: true }],
1183+
[{ path: 'abcxyz' }, null],
1184+
[{ path: 'abcxyz' }, '/abcxyz', { noValidate: true }],
11791185
]
11801186
],
11811187

0 commit comments

Comments
 (0)