Skip to content

Commit 3292bc2

Browse files
authored
feat(evaluate): divide realms into core and contrib (#186)
BREAKING CHANGE: only json and map-set realms and still part of the package. apidom, minim and immutable are no longer part of npm distributions but live in contrib directory in the GitHub repo. Closes #65 Closes #33
1 parent d955bd8 commit 3292bc2

21 files changed

Lines changed: 715 additions & 592 deletions

File tree

CLAUDE.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
44

5+
## Environment Setup
6+
7+
Before running any commands, ensure you're using the correct Node.js version specified in `.nvmrc`:
8+
9+
```bash
10+
nvm use # Switch to Node.js version from .nvmrc
11+
```
12+
513
## Build Commands
614

715
```bash
@@ -26,14 +34,17 @@ This is an RFC 6901 JSON Pointer implementation providing parsing, validation, e
2634

2735
### Evaluation Realms
2836

29-
The realm system (`src/evaluate/realms/`) enables polymorphic evaluation across different data structures:
37+
The realm system enables polymorphic evaluation across different data structures.
3038

39+
**Published realms** (`src/evaluate/realms/`):
3140
- `json/` - Default realm for plain JS objects/arrays
3241
- `map-set/` - Supports Map and Set instances
42+
- `compose.js` - Composes multiple realms (order matters: specific before generic)
43+
44+
**Contrib realms** (`contrib/realms/`) - copy-paste examples, not published:
3345
- `minim/` - For minim element structures (API description tools)
3446
- `apidom/` - For ApiDOM structures (OpenAPI/AsyncAPI processing)
3547
- `immutable/` - For Immutable.js structures
36-
- `compose.js` - Composes multiple realms (order matters: specific before generic)
3748

3849
Custom realms extend `EvaluationRealm` base class implementing: `isArray`, `isObject`, `sizeOf`, `has`, `evaluate`.
3950

README.md

Lines changed: 5 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@
4444
- [Evaluation Realms](#evaluation-realms)
4545
- [JSON](#json-evaluation-realm)
4646
- [Map/Set](#mapset-evaluation-realm)
47-
- [Minim](#minim-evaluation-realm)
48-
- [ApiDOM](#apidom-evaluation-realm)
49-
- [Immutable.js](#immutablejs-evaluation-realm)
5047
- [Custom](#custom-evaluation-realms)
5148
- [Composing Realms](#composing-evaluation-realms)
5249
- [Evaluation Diagnostics](#evaluation-diagnostics)
@@ -373,7 +370,7 @@ An **evaluation realm** defines the rules for interpreting and navigating data s
373370
While JSON Pointer traditionally operates on JSON objects and arrays, evaluation realms allow the evaluation to work
374371
polymorphically with different data structures, such as [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map),
375372
[Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set), [Immutable.js](https://immutable-js.com/),
376-
or even custom representations like [ApiDOM](https://github.com/swagger-api/apidom).
373+
or even custom representations like [SpecLynx ApiDOM](https://github.com/speclynx/apidom).
377374
Realm can be specified via the `realm` option in the `evalute()` function.
378375

379376
###### JSON Evaluation Realm
@@ -418,93 +415,6 @@ const map = new Map([
418415
evaluate(map, '/a/1', { realm: new MapSetEvaluationRealm() }); // => 'c'
419416
```
420417

421-
###### Minim Evaluation Realm
422-
423-
The Minim Evaluation Realm extends JSON Pointer evaluation to support `minim` data structures,
424-
specifically `ObjectElement`, `ArrayElement`, and other element types from the [minim](https://github.com/refractproject/minim).
425-
426-
Minim is widely used in API description languages (e.g., OpenAPI, API Blueprint, AsyncAPI and other API Description processing tools)
427-
to represent structured API data. The Minim Evaluation Realm enables seamless JSON Pointer traversal for these structures.
428-
429-
Before using the Minim Evaluation Realm, you need to install the `minim` package:
430-
431-
```sh
432-
$ npm install --save minim
433-
```
434-
435-
```js
436-
import { ObjectElement } from 'minim';
437-
import { evaluate } from '@swaggerexpert/json-pointer';
438-
import MinimEvaluationRealm from '@swaggerexpert/json-pointer/evaluate/realms/minim';
439-
440-
const objectElement = new ObjectElement({
441-
a: ['b', 'c']
442-
});
443-
444-
evaluate(objectElement, '/a/1', { realm: new MinimEvaluationRealm() }); // => StringElement('c')
445-
```
446-
447-
###### ApiDOM Evaluation Realm
448-
449-
The [ApiDOM](https://github.com/swagger-api/apidom) Evaluation Realm is an integration layer that enables
450-
evaluation of JSON Pointer expressions on ApiDOM structures. It provides compatibility with ApiDOM [core](https://github.com/swagger-api/apidom/tree/main/packages/apidom-core) and namespace packages (`@swagger-api/apidom-ns-*`),
451-
allowing to traverse and query ApiDOM element instances.
452-
453-
Before using the ApiDOM Evaluation Realm, you need to install the `@swagger-api/apidom-core` package:
454-
455-
```sh
456-
$ npm install --save @swagger-api/apidom-core
457-
```
458-
459-
```js
460-
import { ObjectElement } from '@swagger-api/apidom-core';
461-
import { evaluate } from '@swaggerexpert/json-pointer';
462-
import ApiDOMEvaluationRealm from '@swaggerexpert/json-pointer/evaluate/realms/apidom';
463-
464-
const objectElement = new ObjectElement({
465-
a: ['b', 'c']
466-
});
467-
468-
evaluate(objectElement, '/a/1', { realm: new ApiDOMEvaluationRealm() }); // => StringElement('c')
469-
```
470-
471-
or using contextual evaluation:
472-
473-
```js
474-
import { ObjectElement } from '@swagger-api/apidom-core';
475-
import { evaluate } from '@swaggerexpert/json-pointer/evaluate/realms/apidom';
476-
477-
const objectElement = new ObjectElement({
478-
a: ['b', 'c']
479-
});
480-
481-
evaluate(objectElement, '/a/1'); // => StringElement('c')
482-
```
483-
484-
###### Immutable.js Evaluation Realm
485-
486-
The [Immutable.js](https://immutable-js.com/) Evaluation Realm is an integration layer that enables
487-
evaluation of JSON Pointer expressions on Immutable.js structures.
488-
489-
Before using the Immutable.js Evaluation Realm, you need to install the `immutable` package:
490-
491-
```sh
492-
$ npm install --save immutable
493-
```
494-
495-
```js
496-
import { fromJS } from 'immutable';
497-
import { evaluate } from '@swaggerexpert/json-pointer';
498-
import ImmutableEvaluationRealm from '@swaggerexpert/json-pointer/evaluate/realms/immutable';
499-
500-
const map = fromJS({
501-
a: ['b', 'c']
502-
});
503-
504-
evaluate(map, '/a/1', { realm: new ImmutableEvaluationRealm() }); // => 'c'
505-
```
506-
507-
508418
###### Custom Evaluation Realms
509419

510420
The evaluation is designed to support **custom evaluation realms**,
@@ -530,6 +440,10 @@ class CustomEvaluationRealm extends EvaluationRealm {
530440
evaluate({ a: 'b' }, '/a', { realm: new CustomEvaluationRealm() }); // => 'b'
531441
```
532442

443+
Reference implementations for [Minim](https://github.com/refractproject/minim), [SpecLynx ApiDOM](https://github.com/speclynx/apidom),
444+
and [Immutable.js](https://immutable-js.com/) are available in the [`contrib/realms/`](https://github.com/swaggerexpert/json-pointer/tree/main/contrib/realms) directory.
445+
These can be copied and adapted for your specific needs.
446+
533447
###### Composing Evaluation Realms
534448

535449
Evaluation realms can be composed to create complex evaluation scenarios,

contrib/realms/apidom/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# ApiDOM Evaluation Realm
2+
3+
Evaluation realm for [SpecLynx ApiDOM](https://github.com/speclynx/apidom) structures, used for OpenAPI and AsyncAPI processing.
4+
5+
## Requirements
6+
7+
```bash
8+
npm install @speclynx/apidom-datamodel
9+
```
10+
11+
## Usage
12+
13+
```js
14+
import { evaluate } from '@swaggerexpert/json-pointer';
15+
import { ObjectElement } from '@speclynx/apidom-datamodel';
16+
import ApiDOMEvaluationRealm from './index.js';
17+
18+
const element = new ObjectElement({
19+
foo: ['bar', 'baz'],
20+
'': 0,
21+
});
22+
23+
const realm = new ApiDOMEvaluationRealm();
24+
25+
evaluate(element, '/foo/0', { realm }); // => StringElement('bar')
26+
evaluate(element, '/', { realm }); // => NumberElement(0)
27+
```
28+
29+
## Features
30+
31+
- Supports `ObjectElement` and `ArrayElement` from ApiDOM
32+
- Validates array indices as unsigned 32-bit integers
33+
- Enforces unique member names in objects (as required by JSON Pointer spec)
34+
- Works with refracted OpenAPI/AsyncAPI documents
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { isObjectElement, isArrayElement } from '@swagger-api/apidom-core';
1+
import { isObjectElement, isArrayElement } from '@speclynx/apidom-datamodel';
22

3-
import EvaluationRealm from '../EvaluationRealm.js';
4-
import JSONPointerKeyError from '../../../errors/JSONPointerKeyError.js';
5-
import JSONPointerIndexError from '../../../errors/JSONPointerIndexError.js';
3+
import { EvaluationRealm, JSONPointerKeyError, JSONPointerIndexError } from '@swaggerexpert/json-pointer';
64

75
class ApiDOMEvaluationRealm extends EvaluationRealm {
86
name = 'apidom';

contrib/realms/immutable/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Immutable.js Evaluation Realm
2+
3+
Evaluation realm for [Immutable.js](https://immutable-js.com/) data structures.
4+
5+
## Requirements
6+
7+
```bash
8+
npm install immutable
9+
```
10+
11+
## Usage
12+
13+
```js
14+
import { evaluate } from '@swaggerexpert/json-pointer';
15+
import { Map, List } from 'immutable';
16+
import ImmutableEvaluationRealm from './index.js';
17+
18+
const structure = Map({
19+
foo: List(['bar', 'baz']),
20+
'': 0,
21+
});
22+
23+
const realm = new ImmutableEvaluationRealm();
24+
25+
evaluate(structure, '/foo/0', { realm }); // => 'bar'
26+
evaluate(structure, '/', { realm }); // => 0
27+
```
28+
29+
## Features
30+
31+
- Supports `Map` and `List` from Immutable.js
32+
- Seamless integration with immutable data structures
33+
- Can be composed with other realms for mixed data structures
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Immutable from 'immutable';
22

3-
import EvaluationRealm from '../EvaluationRealm.js';
3+
import { EvaluationRealm } from '@swaggerexpert/json-pointer';
44

55
const { List, Map } = Immutable;
66

contrib/realms/minim/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Minim Evaluation Realm
2+
3+
Evaluation realm for [Minim](https://github.com/refractproject/minim) element structures, commonly used in API description tools.
4+
5+
## Requirements
6+
7+
```bash
8+
npm install minim
9+
```
10+
11+
## Usage
12+
13+
```js
14+
import { evaluate } from '@swaggerexpert/json-pointer';
15+
import { ObjectElement } from 'minim';
16+
import MinimEvaluationRealm from './index.js';
17+
18+
const element = new ObjectElement({
19+
foo: ['bar', 'baz'],
20+
'': 0,
21+
});
22+
23+
const realm = new MinimEvaluationRealm();
24+
25+
evaluate(element, '/foo/0', { realm }); // => StringElement('bar')
26+
evaluate(element, '/', { realm }); // => NumberElement(0)
27+
```
28+
29+
## Features
30+
31+
- Supports `ObjectElement` and `ArrayElement` from minim
32+
- Validates array indices as unsigned 32-bit integers
33+
- Enforces unique member names in objects (as required by JSON Pointer spec)
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { ObjectElement, ArrayElement } from 'minim';
22

3-
import EvaluationRealm from '../EvaluationRealm.js';
4-
import JSONPointerKeyError from '../../../errors/JSONPointerKeyError.js';
5-
import JSONPointerIndexError from '../../../errors/JSONPointerIndexError.js';
3+
import { EvaluationRealm, JSONPointerKeyError, JSONPointerIndexError } from '@swaggerexpert/json-pointer';
64

75
class MinimEvaluationRealm extends EvaluationRealm {
86
name = 'minim';

0 commit comments

Comments
 (0)