Skip to content

Commit 36344dc

Browse files
committed
Rename noValidate option to validate
1 parent bef800d commit 36344dc

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

Readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ const toPathRegexp = pathToRegexp.compile('/user/:id(\\d+)')
193193
toPathRegexp({ id: 123 }) //=> "/user/123"
194194
toPathRegexp({ id: '123' }) //=> "/user/123"
195195
toPathRegexp({ id: 'abc' }) //=> Throws `TypeError`.
196-
toPathRegexp({ id: 'abc' }, { noValidate: true }) //=> "/user/abc"
196+
toPathRegexp({ id: 'abc' }, { validate: true }) //=> "/user/abc"
197197
```
198198

199199
**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

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ declare namespace pathToRegexp {
8181
*/
8282
encode?: (value: string, token: Key) => string;
8383
/**
84-
* When `true` the function can produce an invalid (unmatched) path. (default: `false`)
84+
* When `false` the function can produce an invalid (unmatched) path. (default: `true`)
8585
*/
86-
noValidate?: boolean;
86+
validate?: boolean;
8787
}
8888

8989
export type Token = string | Key;

index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function tokensToFunction (tokens, options) {
137137
return function (data, options) {
138138
var path = ''
139139
var encode = (options && options.encode) || encodeURIComponent
140-
var noValidate = (options && options.noValidate) || false
140+
var validate = options ? options.validate !== false : true
141141

142142
for (var i = 0; i < tokens.length; i++) {
143143
var token = tokens[i]
@@ -164,7 +164,7 @@ function tokensToFunction (tokens, options) {
164164
for (var j = 0; j < value.length; j++) {
165165
segment = encode(value[j], token)
166166

167-
if (!noValidate && !matches[i].test(segment)) {
167+
if (validate && !matches[i].test(segment)) {
168168
throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '"')
169169
}
170170

@@ -177,7 +177,7 @@ function tokensToFunction (tokens, options) {
177177
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
178178
segment = encode(String(value), token)
179179

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

test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ var TESTS: Test[] = [
10511051
],
10521052
[
10531053
[{ test: 'abc' }, null],
1054-
[{ test: 'abc' }, '/abc', { noValidate: true }],
1054+
[{ test: 'abc' }, '/abc', { validate: false }],
10551055
[{ test: '123' }, '/123']
10561056
]
10571057
],
@@ -1122,9 +1122,9 @@ var TESTS: Test[] = [
11221122
],
11231123
[
11241124
[{ route: '' }, null],
1125-
[{ route: '' }, '/', { noValidate: true }],
1125+
[{ route: '' }, '/', { validate: false }],
11261126
[{ route: '123' }, null],
1127-
[{ route: '123' }, '/123', { noValidate: true }],
1127+
[{ route: '123' }, '/123', { validate: false }],
11281128
[{ route: 'abc' }, '/abc']
11291129
]
11301130
],
@@ -1149,7 +1149,7 @@ var TESTS: Test[] = [
11491149
[
11501150
[{ route: 'this' }, '/this'],
11511151
[{ route: 'foo' }, null],
1152-
[{ route: 'foo' }, '/foo', { noValidate: true }],
1152+
[{ route: 'foo' }, '/foo', { validate: false }],
11531153
[{ route: 'that' }, '/that']
11541154
]
11551155
],
@@ -1179,9 +1179,9 @@ var TESTS: Test[] = [
11791179
[{ path: ['abc', 'xyz'] }, '/abc/xyz'],
11801180
[{ path: ['xyz', 'abc', 'xyz'] }, '/xyz/abc/xyz'],
11811181
[{ path: 'abc123' }, null],
1182-
[{ path: 'abc123' }, '/abc123', { noValidate: true }],
1182+
[{ path: 'abc123' }, '/abc123', { validate: false }],
11831183
[{ path: 'abcxyz' }, null],
1184-
[{ path: 'abcxyz' }, '/abcxyz', { noValidate: true }],
1184+
[{ path: 'abcxyz' }, '/abcxyz', { validate: false }],
11851185
]
11861186
],
11871187

@@ -2702,7 +2702,7 @@ var TESTS: Test[] = [
27022702
[{ test: 'ABC' }, '/ABC']
27032703
]
27042704
],
2705-
2705+
27062706
]
27072707

27082708
/**

0 commit comments

Comments
 (0)