[d24f17c] | 1 | # @swagger-api/apidom-json-pointer
|
---|
| 2 |
|
---|
| 3 | `apidom-json-pointer` is a package that evaluates [JSON Pointer](https://datatracker.ietf.org/doc/html/rfc6901) against ApiDOM.
|
---|
| 4 |
|
---|
| 5 | ## Installation
|
---|
| 6 |
|
---|
| 7 | You can install this package via [npm CLI](https://docs.npmjs.com/cli) by running the following command:
|
---|
| 8 |
|
---|
| 9 | ```sh
|
---|
| 10 | $ npm install @swagger-api/apidom-json-pointer
|
---|
| 11 | ```
|
---|
| 12 | ## Evaluating
|
---|
| 13 |
|
---|
| 14 | ```js
|
---|
| 15 | import { ObjectElement } from '@swagger-api/apidom-core';
|
---|
| 16 | import { evaluate } from '@swagger-api/apidom-json-pointer';
|
---|
| 17 |
|
---|
| 18 | const apidom = new ObjectElement({ a: { b: 'c' } });
|
---|
| 19 | const result = evaluate('/a/b', apidom);
|
---|
| 20 | // => StringElement('c')
|
---|
| 21 | ```
|
---|
| 22 |
|
---|
| 23 | ## Parsing
|
---|
| 24 |
|
---|
| 25 | Parses JSON Pointer into list of tokens.
|
---|
| 26 |
|
---|
| 27 | ```js
|
---|
| 28 | import { parse } from '@swagger-api/apidom-json-pointer';
|
---|
| 29 |
|
---|
| 30 | const tokens = parse('/a/b'); // => ['a', 'b']
|
---|
| 31 | ```
|
---|
| 32 |
|
---|
| 33 | ## Compiling
|
---|
| 34 |
|
---|
| 35 | Compiles list of tokens into JSON Pointer.
|
---|
| 36 |
|
---|
| 37 | ```js
|
---|
| 38 | import { compile } from '@swagger-api/apidom-json-pointer';
|
---|
| 39 |
|
---|
| 40 | const jsonPointer = compile(['a', 'b']); // => '/a/b'
|
---|
| 41 | ```
|
---|
| 42 |
|
---|
| 43 | ## Escaping
|
---|
| 44 |
|
---|
| 45 | Escapes/unescapes tokens of JSON Pointer.
|
---|
| 46 |
|
---|
| 47 | ```js
|
---|
| 48 | import { escape, unescape } from '@swagger-api/apidom-json-pointer';
|
---|
| 49 |
|
---|
| 50 | escape('~a/'); // => '~0a~1'
|
---|
| 51 | unescape('~0a~1'); // => '~a/'
|
---|
| 52 | ```
|
---|
| 53 |
|
---|
| 54 | ## Transforming URI to JSON Pointer
|
---|
| 55 |
|
---|
| 56 | Handles case of [URI Fragment Identifier Representation](https://datatracker.ietf.org/doc/html/rfc6901#section-6).
|
---|
| 57 |
|
---|
| 58 | ```js
|
---|
| 59 | import { uriToPointer } from '@swagger-api/apidom-json-pointer';
|
---|
| 60 |
|
---|
| 61 | uriToPointer('https://example.com/path/#/a/b'); // => '/a/b'
|
---|
| 62 | ```
|
---|
| 63 |
|
---|
| 64 | ## Invalid JSON Pointers
|
---|
| 65 |
|
---|
| 66 | If invalid JSON Pointer is supplied to `parse` or `evaluate` functions, `InvalidJsonPointerError`
|
---|
| 67 | is thrown.
|
---|
| 68 |
|
---|
| 69 | ```js
|
---|
| 70 | import { InvalidJsonPointerError } from '@swagger-api/apidom-json-pointer';
|
---|
| 71 | ```
|
---|
| 72 |
|
---|
| 73 | If valid JSON Pointer is supplied to `evaluate` function and the pointer cannot be evaluated against
|
---|
| 74 | ApiDOM fragment, `EvaluationJsonPointerError` is thrown.
|
---|
| 75 |
|
---|
| 76 | ```js
|
---|
| 77 | import { EvaluationJsonPointerError } from '@swagger-api/apidom-json-pointer';
|
---|
| 78 | ```
|
---|