Skip to content

takes $ref strings and a root object, returns the resolved value of the reference

License

Notifications You must be signed in to change notification settings

json-schema-tools/reference-resolver

Repository files navigation

JSON Schema Reference Resolver

CircleCI branch npm GitHub release GitHub commits since latest release

Takes a $ref string and a root object, and returns the referenced value.

Works in browser & in node (file system refs ignored in browser).

Easily add support for your own protocols.

Getting Started

npm install @json-schema-tools/reference-resolver

import refRes from "@json-schema-tools/reference-resolver";

refRes.resolve("#/properties/foo", { properties: { foo: true } }); // returns true
refRes.resolve("https://foo.com/"); // returns what ever json foo.com returns
refRef.resolve("../my-object.json"); // you get teh idea

Adding custom protocol handlers

import referenceResolver from "@json-schema-tools/reference-resolver";
import JSONSchema from "@json-schema-tools/meta-schema";

referenceResolver.protocolHandlerMap.ipfs = (uri) => {
   const pretendItsFetchedFromIpfs = {
     title: "foo",
     type: "string",
   } as JSONSchema;
   return Promise.resolve(fetchedFromIpfs);
};

referenceResolver.protocolHandlerMap["customprotocol"] = (uri) => {
   return Promise.resolve({
     type: "string",
     title: uri.replace("customprotocol://", ""),
   });
};

referenceResolver.resolve("ipfs://80088008800880088008");
referenceResolver.resolve("customprotocol://foobar");

Contributing

How to contribute, build and release are outlined in CONTRIBUTING.md, BUILDING.md and RELEASING.md respectively. Commits in this repository follow the CONVENTIONAL_COMMITS.md specification.