Skip to content

Commit ca19ee2

Browse files
committed
Auto-generated commit
1 parent df7e737 commit ca19ee2

8 files changed

Lines changed: 76 additions & 52 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ A total of 10 issues were closed in this release:
5151

5252
<details>
5353

54+
- [`83652c1`](https://github.com/stdlib-js/stdlib/commit/83652c1fc6effe086806e0fc9cefcf2a7d96ff82) - **refactor:** return `undefined` for nonexistent document locations [(#16676)](https://github.com/stdlib-js/stdlib/pull/16676) _(by Gururaj Gurram, Athan Reines)_
5455
- [`d00c253`](https://github.com/stdlib-js/stdlib/commit/d00c25388d3c73d6fe3a272540e6686a4a86bb19) - **refactor:** add error handling for invalid escape sequences [(#16677)](https://github.com/stdlib-js/stdlib/pull/16677) _(by Gururaj Gurram)_
5556
- [`4ce7668`](https://github.com/stdlib-js/stdlib/commit/4ce7668d098bf469feb7d67026e2480fc26de84a) - **docs:** update throws annotations [(#16597)](https://github.com/stdlib-js/stdlib/pull/16597) _(by Philipp Burckhardt, Athan Reines)_
5657
- [`a6aa6d9`](https://github.com/stdlib-js/stdlib/commit/a6aa6d97df92d15c0262526367502ebd9d367036) - **feat:** add `utils/json-pointer/has` [(#16246)](https://github.com/stdlib-js/stdlib/pull/16246) _(by Gururaj Gurram, Athan Reines)_

‎json-pointer/resolve/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ out = resolve( obj, '/foo' );
4949

5050
out = resolve( obj, '' );
5151
// returns { 'target': null, 'key': null, 'value': { 'foo': 'bar' } }
52+
53+
out = resolve( obj, '/missing' );
54+
// returns undefined
5255
```
5356

5457
</section>
@@ -60,6 +63,7 @@ out = resolve( obj, '' );
6063
## Notes
6164

6265
- Per [RFC 6901][rfc-6901], an empty string refers to the root of the document.
66+
- If a pointer references a nonexistent location in the document, the function returns `undefined`.
6367
- The `-` token on the last segment of a pointer targeting an array resolves to the array's length (i.e., past-the-end index).
6468
- Tokens `__proto__`, `constructor`, and `prototype` are rejected to prevent prototype pollution.
6569
- The returned `target` is the parent container (object or array) holding the referenced value. For root pointers, `target` and `key` are both `null`.
@@ -101,6 +105,9 @@ out = resolve( doc, '/m~0n' );
101105

102106
out = resolve( doc, '' );
103107
// returns { 'target': null, 'key': null, 'value': { 'foo': [ 'bar', 'baz' ], '': 0, 'a/b': 1, 'm~n': 8 } }
108+
109+
out = resolve( doc, '/missing' );
110+
// returns undefined
104111
```
105112

106113
</section>

‎json-pointer/resolve/docs/repl.txt‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
Per RFC 6901, an empty string refers to the root of the document.
77

8+
If a pointer references a nonexistent location in the document, the
9+
function returns `undefined`.
10+
811
The `-` token on the last segment of a pointer targeting an array resolves
912
to the array's length (i.e., past-the-end index).
1013

@@ -21,8 +24,9 @@
2124

2225
Returns
2326
-------
24-
out: Object
25-
Result object.
27+
out: Object|void
28+
Result object or undefined if pointer does not resolve to an existing
29+
location.
2630

2731
out.target: Object|Array|null
2832
Parent container. Null for root pointer.
@@ -41,6 +45,8 @@
4145
{ 'target': { 'foo': 'bar' }, 'key': 'foo', 'value': 'bar' }
4246
> out = {{alias}}( { 'foo': 'bar' }, '' )
4347
{ 'target': null, 'key': null, 'value': { 'foo': 'bar' } }
48+
> out = {{alias}}( { 'foo': 'bar' }, '/missing' )
49+
undefined
4450

4551
See Also
4652
--------

‎json-pointer/resolve/docs/types/index.d.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@ interface ResolveResult {
4545
*
4646
* - Implements resolution per RFC 6901.
4747
* - An empty string pointer refers to the root of the document.
48+
* - If a pointer references a nonexistent location in the document, the function returns `undefined`.
4849
* - The `-` token on the last segment of a pointer targeting an array resolves to the array's length (i.e., past-the-end index).
4950
* - Tokens `__proto__`, `constructor`, and `prototype` are rejected to prevent prototype pollution.
5051
*
5152
* @param document - JSON document
5253
* @param pointer - JSON Pointer string
53-
* @throws pointer must resolve to an existing location in the document
54+
* @throws first argument must be an object or an array
55+
* @throws second argument must be a string
56+
* @throws pointer must be an empty string or begin with `/`
5457
* @throws tokens must not be prototype pollution vectors
55-
* @throws array index must be within bounds
56-
* @returns result object
58+
* @returns result
5759
*
5860
* @example
5961
* var out = resolve( { 'foo': [ 'bar', 'baz' ] }, '/foo/0' );
@@ -66,8 +68,12 @@ interface ResolveResult {
6668
* @example
6769
* var out = resolve( { 'foo': 'bar' }, '' );
6870
* // returns { 'target': null, 'key': null, 'value': { 'foo': 'bar' } }
71+
*
72+
* @example
73+
* var out = resolve( { 'foo': 'bar' }, '/missing' );
74+
* // returns undefined
6975
*/
70-
declare function resolve( document: Record<string, any> | Array<any>, pointer: string ): ResolveResult;
76+
declare function resolve( document: Record<string, any> | Array<any>, pointer: string ): ResolveResult | void;
7177

7278

7379
// EXPORTS //

‎json-pointer/resolve/docs/types/test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import resolve = require( './index' );
2121

2222
// TESTS //
2323

24-
// The function returns a resolve result object...
24+
// The function returns a result object or `undefined`...
2525
{
26-
resolve( { 'foo': 'bar' }, '/foo' ); // $ExpectType ResolveResult
27-
resolve( [ 1, 2, 3 ], '/0' ); // $ExpectType ResolveResult
28-
resolve( { 'foo': 'bar' }, '' ); // $ExpectType ResolveResult
26+
resolve( { 'foo': 'bar' }, '/foo' ); // $ExpectType void | ResolveResult
27+
resolve( [ 1, 2, 3 ], '/0' ); // $ExpectType void | ResolveResult
28+
resolve( { 'foo': 'bar' }, '' ); // $ExpectType void | ResolveResult
2929
}
3030

3131
// The compiler throws an error if the function is provided a first argument which is not an object or array...

‎json-pointer/resolve/examples/index.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ console.log( out );
5050
out = resolve( doc, '' );
5151
console.log( out );
5252
// => { 'target': null, 'key': null, 'value': { ... } }
53+
54+
out = resolve( doc, '/missing' );
55+
console.log( out );
56+
// => undefined

‎json-pointer/resolve/lib/main.js‎

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ var resolveToken = require( './../../../json-pointer/base/resolve-token' );
3838
*
3939
* - Implements resolution per RFC 6901.
4040
* - An empty string pointer refers to the root of the document.
41+
* - If a pointer references a nonexistent location in the document, the function returns `undefined`.
4142
* - The `-` token on the last segment of a pointer targeting an array resolves to the array's length (i.e., past-the-end index).
4243
* - Tokens `__proto__`, `constructor`, and `prototype` are rejected to prevent prototype pollution.
4344
*
4445
* @param {(Object|Array)} document - JSON document
4546
* @param {string} pointer - JSON Pointer string
46-
* @throws {TypeError} first argument must be an object or array
47+
* @throws {TypeError} first argument must be an object or an array
4748
* @throws {TypeError} second argument must be a string
48-
* @throws {Error} pointer must resolve to an existing location in the document
49+
* @throws {Error} pointer must be an empty string or begin with `/`
4950
* @throws {Error} tokens must not be prototype pollution vectors
50-
* @throws {RangeError} array index must be within bounds
51-
* @returns {Object} result object
51+
* @returns {(Object|void)} result object or undefined
5252
*
5353
* @example
5454
* var out = resolve( { 'foo': [ 'bar', 'baz' ] }, '/foo/0' );
@@ -61,6 +61,10 @@ var resolveToken = require( './../../../json-pointer/base/resolve-token' );
6161
* @example
6262
* var out = resolve( { 'foo': 'bar' }, '' );
6363
* // returns { 'target': null, 'key': null, 'value': { 'foo': 'bar' } }
64+
*
65+
* @example
66+
* var out = resolve( { 'foo': 'bar' }, '/missing' );
67+
* // returns undefined
6468
*/
6569
function resolve( document, pointer ) {
6670
var current;
@@ -76,6 +80,13 @@ function resolve( document, pointer ) {
7680
}
7781
tokens = parse( pointer );
7882

83+
// Validate all tokens upfront for security to guarantee deterministic rejection:
84+
for ( i = 0; i < tokens.length; i++ ) {
85+
if ( !isSafeToken( tokens[ i ] ) ) {
86+
throw new Error( format( 'invalid argument. Pointer contains an unsafe token. Value: `%s`.', tokens[ i ] ) );
87+
}
88+
}
89+
7990
// An empty pointer refers to the root:
8091
if ( tokens.length === 0 ) {
8192
return {
@@ -91,42 +102,42 @@ function resolve( document, pointer ) {
91102
// Traverse all tokens except the last to find the target:
92103
for ( i = 0; i < last; i++ ) {
93104
token = tokens[ i ];
94-
if ( !isSafeToken( token ) ) {
95-
throw new Error( format( 'invalid argument. Pointer contains an unsafe token. Value: `%s`.', token ) );
96-
}
97105
if ( isArray( current ) ) {
98106
idx = resolveToken( current, token );
99107
if ( idx >= current.length ) {
100-
throw new RangeError( format( 'invalid argument. Array index out of bounds. Index: `%d`. Length: `%d`.', idx, current.length ) );
108+
// Array index is out-of-bounds...
109+
return;
101110
}
102111
current = current[ idx ];
103112
} else if ( isPlainObject( current ) ) {
104113
if ( !hasOwnProp( current, token ) ) {
105-
throw new Error( format( 'invalid argument. Pointer references a nonexistent property. Value: `%s`.', token ) );
114+
// Pointer references a nonexistent property...
115+
return;
106116
}
107117
current = current[ token ];
108118
} else {
109-
throw new TypeError( format( 'invalid argument. Pointer references a non-traversable value. Value: `%s`.', token ) );
119+
// Pointer references a non-traversable value...
120+
return;
110121
}
111122
}
112123

113124
// Handle the last token to determine the final key and value:
114125
token = tokens[ last ];
115-
if ( !isSafeToken( token ) ) {
116-
throw new Error( format( 'invalid argument. Pointer contains an unsafe token. Value: `%s`.', token ) );
117-
}
118126
if ( isArray( current ) ) {
119127
key = resolveToken( current, token );
120128
if ( token !== '-' && key >= current.length ) {
121-
throw new RangeError( format( 'invalid argument. Array index out of bounds. Index: `%d`. Length: `%d`.', key, current.length ) );
129+
// Array index is out-of-bounds...
130+
return;
122131
}
123132
} else if ( isPlainObject( current ) ) {
124133
if ( !hasOwnProp( current, token ) ) {
125-
throw new Error( format( 'invalid argument. Pointer references a nonexistent property. Value: `%s`.', token ) );
134+
// Pointer references a nonexistent property...
135+
return;
126136
}
127137
key = token;
128138
} else {
129-
throw new TypeError( format( 'invalid argument. Pointer references a non-traversable value. Value: `%s`.', token ) );
139+
// Pointer references a non-traversable value...
140+
return;
130141
}
131142
return {
132143
'target': current,

‎json-pointer/resolve/test/test.js‎

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,10 @@ tape( 'the function throws an error for unsafe tokens', function test( t ) {
262262
'/constructor',
263263
'/prototype',
264264
'/foo/__proto__',
265-
'/foo/constructor'
265+
'/foo/constructor',
266+
'/missing/__proto__',
267+
'/missing/constructor',
268+
'/a/b/c/prototype'
266269
];
267270
for ( i = 0; i < values.length; i++ ) {
268271
t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] );
@@ -276,32 +279,27 @@ tape( 'the function throws an error for unsafe tokens', function test( t ) {
276279
}
277280
});
278281

279-
tape( 'the function throws an error for nonexistent object properties', function test( t ) {
282+
tape( 'the function returns undefined for nonexistent object properties', function test( t ) {
280283
var doc;
281284

282285
doc = {
283286
'foo': 'bar'
284287
};
285-
t.throws( badValue, Error, 'throws an error when provided /missing' );
288+
t.strictEqual( resolve( doc, '/missing' ), void 0, 'returns expected value' );
289+
t.strictEqual( resolve( doc, '/missing/nested' ), void 0, 'returns expected value' );
286290
t.end();
287-
288-
function badValue() {
289-
resolve( doc, '/missing' );
290-
}
291291
});
292292

293-
tape( 'the function throws an error for out-of-bounds array indices', function test( t ) {
293+
tape( 'the function returns undefined for out-of-bounds array indices', function test( t ) {
294294
var doc;
295295

296296
doc = {
297297
'foo': [ 1, 2, 3 ]
298298
};
299-
t.throws( badValue, RangeError, 'throws an error when provided /foo/5' );
299+
t.strictEqual( resolve( doc, '/foo/3' ), void 0, 'returns expected value' );
300+
t.strictEqual( resolve( doc, '/foo/5' ), void 0, 'returns expected value' );
301+
t.strictEqual( resolve( doc, '/foo/5/bar' ), void 0, 'returns expected value' );
300302
t.end();
301-
302-
function badValue() {
303-
resolve( doc, '/foo/5' );
304-
}
305303
});
306304

307305
tape( 'the function throws an error for invalid array indices', function test( t ) {
@@ -330,25 +328,16 @@ tape( 'the function throws an error for invalid array indices', function test( t
330328
}
331329
});
332330

333-
tape( 'the function throws an error when traversing into a primitive', function test( t ) {
331+
tape( 'the function returns undefined when attempting to traverse into a primitive', function test( t ) {
334332
var doc;
335-
336333
doc = {
337334
'foo': 'bar'
338335
};
339-
t.throws( badValue1, TypeError, 'throws an error when traversing into a string' );
340-
336+
t.strictEqual( resolve( doc, '/foo/baz' ), void 0, 'returns expected value' );
341337
doc = {
342338
'foo': 42
343339
};
344-
t.throws( badValue2, TypeError, 'throws an error when traversing into a number' );
345-
t.end();
346-
347-
function badValue1() {
348-
resolve( doc, '/foo/baz' );
349-
}
340+
t.strictEqual( resolve( doc, '/foo/bar' ), void 0, 'returns expected value' );
350341

351-
function badValue2() {
352-
resolve( doc, '/foo/bar' );
353-
}
342+
t.end();
354343
});

0 commit comments

Comments
 (0)