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