Skip to content

Commit fe0aa1a

Browse files
authored
docs(json-pointer): provide documentation for modern version of the package (#5049)
1 parent 4b3daaa commit fe0aa1a

File tree

1 file changed

+136
-5
lines changed

1 file changed

+136
-5
lines changed

packages/apidom-json-pointer/README.md

Lines changed: 136 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ You can install this package via [npm CLI](https://docs.npmjs.com/cli) by runnin
1313
## Modern API
1414

1515
This is the recommended API for use in new projects. It is fully compliant with [RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901) and supports all aspects of JSON Pointer.
16-
Uses [@swaggerexpert/json-pointer](https://www.npmjs.com/package/@swaggerexpert/json-pointer) under the hood and fully reflects its API.
16+
Uses [@swaggerexpert/json-pointer](https://www.npmjs.com/package/@swaggerexpert/json-pointer) under the hood and fully reflects its API. For additional options and details, refer to the `@swaggerexpert/json-pointer` [documentation](https://www.npmjs.com/package/@swaggerexpert/json-pointer#usage).
1717

1818
Evaluation is contextual to [ApiDOM realm](https://github.com/swaggerexpert/json-pointer?tab=readme-ov-file#apidom-evaluation-realm) - meaning `evaluate` function
1919
expects only ApiDOM as the first argument.
@@ -22,7 +22,138 @@ expects only ApiDOM as the first argument.
2222
import { evaluate } from '@swagger-api/apidom-json-pointer/modern';
2323
```
2424

25+
### Evaluating
26+
27+
```js
28+
import { ObjectElement } from '@swagger-api/apidom-core';
29+
import { evaluate } from '@swagger-api/apidom-json-pointer/modern';
30+
31+
const apidom = new ObjectElement({ a: { b: 'c' } });
32+
const result = evaluate(apidom, '/a/b');
33+
// => StringElement('c')
34+
```
35+
36+
### Parsing
37+
38+
Parses JSON Pointer into a list of tokens, which can be accessed through the `tree` property of the parse result.
39+
40+
```js
41+
import { parse } from '@swagger-api/apidom-json-pointer/modern';
42+
43+
const parseResult = parse('/a/b');
44+
// =>
45+
// {
46+
// result: {
47+
// success: true,
48+
// state: 101,
49+
// stateName: 'MATCH',
50+
// length: 4,
51+
// matched: 4,
52+
// maxMatched: 4,
53+
// maxTreeDepth: 8,
54+
// nodeHits: 31
55+
// },
56+
// tree: [ 'a', 'b' ],
57+
// stats: undefined,
58+
// trace: undefined
59+
// }
60+
```
61+
62+
### Compiling
63+
64+
Compiles a list of tokens into JSON Pointer.
65+
66+
```js
67+
import { compile } from '@swagger-api/apidom-json-pointer/modern';
68+
69+
const jsonPointer = compile(['a', 'b']); // => '/a/b'
70+
```
71+
72+
### Escaping
73+
74+
Escapes/unescapes tokens of JSON Pointer.
75+
76+
```js
77+
import { escape, unescape } from '@swagger-api/apidom-json-pointer/modern';
2578

79+
escape('~a/'); // => '~0a~1'
80+
unescape('~0a~1'); // => '~a/'
81+
```
82+
83+
### Transforming URI to JSON Pointer
84+
85+
Handles case of [URI Fragment Identifier Representation](https://datatracker.ietf.org/doc/html/rfc6901#section-6).
86+
87+
```js
88+
import { URIFragmentIdentifier } from '@swagger-api/apidom-json-pointer/modern';
89+
90+
URIFragmentIdentifier.fromURIReference('https://example.com/path/#/a/b'); // => '/a/b'
91+
```
92+
93+
### Validating
94+
95+
Validates a JSON Pointer and its tokens.
96+
97+
```js
98+
import {
99+
testJSONPointer,
100+
testReferenceToken,
101+
testArrayLocation,
102+
testArrayIndex,
103+
testArrayDash,
104+
} from '@swagger-api/apidom-json-pointer/modern';
105+
106+
testJSONPointer('/a/b'); // => true
107+
testReferenceToken('a'); // => true
108+
testArrayLocation('0'); // => true
109+
testArrayLocation('-'); // => true
110+
testArrayIndex('0'); // => true
111+
testArrayDash('-'); // => true
112+
```
113+
114+
### Invalid JSON Pointers
115+
116+
`JSONPointerError` is the base class for all JSON Pointer errors.
117+
118+
```js
119+
import { JSONPointerError } from '@swagger-api/apidom-json-pointer/modern';
120+
```
121+
122+
If an invalid list of tokens is supplied to `compile` function, `JSONPointerCompileError` is thrown.
123+
124+
```js
125+
import { JSONPointerCompileError } from '@swagger-api/apidom-json-pointer/modern';
126+
```
127+
128+
If an invalid JSON Pointer is supplied to `evaluate` function, `JSONPointerEvaluateError` is thrown.
129+
130+
```js
131+
import { JSONPointerEvaluateError } from '@swagger-api/apidom-json-pointer/modern';
132+
```
133+
134+
If a valid JSON Pointer is supplied to `evaluate` function and the pointer cannot be evaluated against ApiDOM fragment because it is not an object or an array, `JSONPointerTypeError` is thrown.
135+
136+
```js
137+
import { JSONPointerTypeError } from '@swagger-api/apidom-json-pointer/modern';
138+
```
139+
140+
If a valid JSON Pointer is supplied to `evaluate` function and the pointer cannot be evaluated against ApiDOM fragment because the key does not exist in the object, `JSONPointerKeyError` is thrown.
141+
142+
```js
143+
import { JSONPointerKeyError } from '@swagger-api/apidom-json-pointer/modern';
144+
```
145+
146+
If a valid JSON Pointer is supplied to `evaluate` function and the pointer cannot be evaluated against ApiDOM fragment because the index does not exist in the array, `JSONPointerIndexError` is thrown.
147+
148+
```js
149+
import { JSONPointerIndexError } from '@swagger-api/apidom-json-pointer/modern';
150+
```
151+
152+
If an error occurs in `parse` function, `JSONPointerParseError` is thrown.
153+
154+
```js
155+
import { JSONPointerParseError } from '@swagger-api/apidom-json-pointer/modern';
156+
```
26157

27158
## Legacy API
28159

@@ -52,7 +183,7 @@ const result = evaluate('/a/b', apidom);
52183

53184
### Parsing
54185

55-
Parses JSON Pointer into list of tokens.
186+
Parses JSON Pointer into a list of tokens.
56187

57188
```js
58189
import { parse } from '@swagger-api/apidom-json-pointer';
@@ -62,7 +193,7 @@ const tokens = parse('/a/b'); // => ['a', 'b']
62193

63194
### Compiling
64195

65-
Compiles list of tokens into JSON Pointer.
196+
Compiles a list of tokens into JSON Pointer.
66197

67198
```js
68199
import { compile } from '@swagger-api/apidom-json-pointer';
@@ -93,14 +224,14 @@ uriToPointer('https://example.com/path/#/a/b'); // => '/a/b'
93224

94225
### Invalid JSON Pointers
95226

96-
If invalid JSON Pointer is supplied to `parse` or `evaluate` functions, `InvalidJsonPointerError`
227+
If an invalid JSON Pointer is supplied to `parse` or `evaluate` functions, `InvalidJsonPointerError`
97228
is thrown.
98229

99230
```js
100231
import { InvalidJsonPointerError } from '@swagger-api/apidom-json-pointer';
101232
```
102233

103-
If valid JSON Pointer is supplied to `evaluate` function and the pointer cannot be evaluated against
234+
If a valid JSON Pointer is supplied to `evaluate` function and the pointer cannot be evaluated against
104235
ApiDOM fragment, `EvaluationJsonPointerError` is thrown.
105236

106237
```js

0 commit comments

Comments
 (0)