Skip to content

Commit 3542fe0

Browse files
committed
test(validation): add test suite for json-pointer validation
1 parent f76d3ec commit 3542fe0

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/test/json-pointer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import parse from '../parse/index.js';
22

33
const testJSONPointer = (jsonPointer) => {
4+
if (typeof jsonPointer !== 'string') return false;
5+
46
try {
57
const parseResult = parse(jsonPointer);
68
return parseResult.result.success;

test/test/json-pointer.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { assert } from 'chai';
2+
import { testJSONPointer } from '../../src/index.js';
3+
4+
describe('testJSONPointer', function () {
5+
context('valid JSON Pointers', function () {
6+
it('should return true for valid JSON Pointers (rfc 6901)', function () {
7+
const validPointers = [
8+
'', // whole document
9+
'/foo', // valid key
10+
'/foo/0', // valid array index
11+
'/', // empty key (valid but rare)
12+
'/a~1b', // escaped `/`
13+
'/c%d', // special character
14+
'/e^f', // special character
15+
'/g|h', // special character
16+
'/i\\j', // escape sequence
17+
'/k"l', // quotation mark inside key
18+
'/ ', // space as key
19+
'/m~0n', // escaped `~`
20+
'/foo//bar', // valid: double `/` means empty segment
21+
'/foo/01', // valid: JSON Pointer allows leading zeroes in path segments
22+
'/-', // valid: `-` can be used in array operations
23+
'/foo/bar/', // valid: trailing slash is allowed
24+
'/foo[0]', // valid: brackets are allowed in keys
25+
'/foo?bar', // valid: `?` can be part of a key
26+
'/#hashtag', // valid: `#` can be part of a key
27+
'/foo bar', // valid: spaces are allowed in keys
28+
];
29+
30+
validPointers.forEach((pointer) => {
31+
assert.isTrue(testJSONPointer(pointer), `failed for pointer: ${pointer}`);
32+
});
33+
});
34+
});
35+
36+
context('invalid JSON Pointers', function () {
37+
it('should return false for malformed JSON Pointers', function () {
38+
const invalidPointers = [
39+
'/foo/~', // unescaped `~` at the end
40+
'/foo/~2bar', // invalid escape `~2`,
41+
'foo', // valid relative JSON Pointer
42+
'foo/bar', // valid relative JSON Pointer
43+
];
44+
45+
invalidPointers.forEach((pointer) => {
46+
assert.isFalse(testJSONPointer(pointer), `failed for pointer: ${pointer}`);
47+
});
48+
});
49+
});
50+
});

0 commit comments

Comments
 (0)