Gathering detailed insights and metrics for @stoplight/json-ref-resolver
Gathering detailed insights and metrics for @stoplight/json-ref-resolver
Gathering detailed insights and metrics for @stoplight/json-ref-resolver
Gathering detailed insights and metrics for @stoplight/json-ref-resolver
[Deprecated] Recursively resolve JSON pointers and remote authorities.
npm install @stoplight/json-ref-resolver
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
38 Stars
118 Commits
9 Forks
20 Watching
6 Branches
65 Contributors
Updated on 29 Oct 2024
TypeScript (99.98%)
JavaScript (0.02%)
Cumulative downloads
Total Downloads
Last day
-6.3%
126,405
Compared to previous day
Last week
3.4%
717,172
Compared to previous week
Last month
7.3%
2,928,368
Compared to previous month
Last year
63.2%
27,121,409
Compared to previous year
Follow $ref
values in JSON Schema, OpenAPI (formerly known as Swagger), and any other objects with $ref
values inside of them.
http://
, file://
, mongo://
, custom://
... etc, or use one of ours.Supported in modern browsers and node.
1yarn add @stoplight/json-ref-resolver
All relevant types and options can be found in src/types.ts.
1// Import the Resolver class. 2import { Resolver } from "@stoplight/json-ref-resolver"; 3 4/** 5 * Create a Resolver instance. Resolve can be called on this instance multiple times to take advantage of caching. 6 * 7 * @param globalOpts {IResolverOpts} [{}] 8 * 9 * These options are used on every resolve call for this resolver instance. 10 * 11 * See `IResolverOpts` interface defined in [src/types.ts](src/types.ts) for available options. 12 * 13 * @return IResolver 14 */ 15const resolver = new Resolver(globalOpts); 16 17/** 18 * Resolve the passed in object, replacing all references. 19 20 * @param resolveOpts {any} - The object to resolve. 21 22 * @param resolveOpts {IResolveOpts} [{}] 23 * 24 * These options override any globalOpts specified on the resolver instance, and only apply during this resolve call. 25 * 26 * See `IResolveOpts` interface defined in [src/types.ts](src/types.ts) for available options. 27 * 28 * @return IResolveResult - see [src/types.ts](src/types.ts) for interface definition. 29 */ 30const resolved = await resolver.resolve(sourceObj, resolveOpts);
By default, only inline references will be dereferenced.
1import { Resolver } from "@stoplight/json-ref-resolver"; 2 3const resolver = new Resolver(); 4const resolved = await resolver.resolve({ 5 user: { 6 $ref: "#/models/user" 7 }, 8 models: { 9 user: { 10 name: "john" 11 } 12 } 13}); 14 15// ==> result is the original object, with local refs resolved and replaced 16expect(resolved.result).toEqual({ 17 user: { 18 name: "john" 19 }, 20 models: { 21 user: { 22 name: "john" 23 } 24 } 25});
This will dereference the minimal number of references needed for the given target, and return the target.
In the example below, the address reference (https://slow-website.com/definitions#/address
) will NOT be dereferenced, since
it is not needed to resolve the #/user
jsonPointer target we have specified. However, #/models/user/card
IS dereferenced since
it is needed in order to full dereference the #/user
property.
1import { Resolver } from "@stoplight/json-ref-resolver"; 2 3const resolver = new Resolver(); 4const resolved = await resolver.resolve( 5 { 6 user: { 7 $ref: "#/models/user" 8 }, 9 address: { 10 $ref: "https://slow-website.com/definitions#/address" 11 }, 12 models: { 13 user: { 14 name: "john", 15 card: { 16 $ref: "#/models/card" 17 } 18 }, 19 card: { 20 type: "visa" 21 } 22 } 23 }, 24 { 25 jsonPointer: "#/user" 26 } 27); 28 29// ==> result is the target object, with refs resolved and replaced 30expect(resolved.result).toEqual({ 31 name: "john", 32 card: { 33 type: "visa" 34 } 35});
By default only inline references (those that point to values inside of the original object) are dereferenced.
In order to dereference remote URIs (file, http, etc) you must provide resolvers for each URI scheme.
Resolvers are keyed by scheme, receive the URI to fetch, and must return the fetched data.
1import { Resolver } from "@stoplight/json-ref-resolver";
2
3// some example http library
4import * as axios from "axios";
5
6// if we're in node, we create a file resolver with fs
7import * as fs from "fs";
8
9// create our resolver instance
10const resolver = new Resolver({
11 // resolvers can do anything, so long as they define an async read function that resolves to a value
12 resolvers: {
13 // this resolver will be invoked for refs with the https protocol
14 https: {
15 async resolve(ref: uri.URI) {
16 return axios({
17 method: "get",
18 url: String(ref)
19 });
20 }
21 },
22
23 // this resolver will be invoked for refs with the file protocol
24 file: {
25 async resolve(ref: uri.URI) {
26 return fs.read(String(ref));
27 }
28 }
29 }
30});
31
32const resolved = await resolver.resolve({
33 definitions: {
34 someOASFile: {
35 $ref: "./main.oas2.yml#/definitions/user"
36 },
37 someMarkdownFile: {
38 $ref: "https://foo.com/intro.md"
39 }
40 }
41});
42
43// ==> result is the original object, with refs resolved and replaced
44expect(resolved.result).toEqual({
45 definitions: {
46 someOASFile: {
47 // ... the data located in the relative file `./main.oas2.yml` and inner json path `#/definitions/user`
48 },
49 someMarkdownFile: {
50 // ... the data located at the url `https://foo.com/intro.md`
51 }
52 }
53});
If there are relative remote references (for example, a relative file path ../model.json
), then the location of the source
data must be specified via the baseUri
option. Relative references will be dereferenced against this baseUri.
1import { Resolver } from "@stoplight/json-ref-resolver";
2import * as fs from "fs";
3import * as URI from "urijs";
4
5const resolver = new Resolver({
6 readers: {
7 file: {
8 async read(ref: uri.URI) {
9 return fs.read(String(ref));
10 }
11 }
12 }
13});
14
15const sourcePath = "/specs/api.json";
16const sourceData = fs.readSync(sourcePath);
17// sourceData === {
18// user: {
19// $ref: "../models/user.json"
20// }
21// }
22
23const resolved = await resolver.resolve(sourceData, {
24 // Indicate where the `sourceData` being resolved lives, so that relative remote references can be fetched and resolved.
25 baseUri: new URI(sourcePath)
26});
27
28expect(resolved.result).toEqual({
29 user: {
30 // ... the user object defined in `../models/user.json`
31 }
32});
In the above example, the user $ref
will resolve to /models/user.json
, because ../models/user.json
is resolved against the baseUri
of the current document (which was indicated at /specs/api.json
). Relative references will not work if the source document has no baseUri set.
This is a simplistic example of a reader. You can create your own, but we have built some readers which you might find useful.
If you are interested in contributing to Spectral itself, check out our contributing docs to get started.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 14/20 approved changesets -- score normalized to 7
Reason
project is archived
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
78 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More