Skip to content

Commit

Permalink
Rename noValidate option to validate
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Aug 31, 2019
1 parent bef800d commit 36344dc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 + '"')
}

Expand All @@ -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 + '"')
}

Expand Down
14 changes: 7 additions & 7 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ var TESTS: Test[] = [
],
[
[{ test: 'abc' }, null],
[{ test: 'abc' }, '/abc', { noValidate: true }],
[{ test: 'abc' }, '/abc', { validate: false }],
[{ test: '123' }, '/123']
]
],
Expand Down Expand Up @@ -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']
]
],
Expand All @@ -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']
]
],
Expand Down Expand Up @@ -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 }],
]
],

Expand Down Expand Up @@ -2702,7 +2702,7 @@ var TESTS: Test[] = [
[{ test: 'ABC' }, '/ABC']
]
],

]

/**
Expand Down

0 comments on commit 36344dc

Please sign in to comment.