What is json-schema-resolver?
The json-schema-resolver npm package is designed to resolve JSON schema references, allowing you to dereference $ref pointers within your JSON schemas. This is particularly useful for validating complex JSON structures that reference other schemas.
What are json-schema-resolver's main functionalities?
Resolving JSON Schema References
This feature allows you to resolve $ref pointers within a JSON schema. The code sample demonstrates how to use the json-schema-resolver package to dereference a schema that includes a reference to another schema.
const resolver = require('json-schema-resolver');
const schema = {
"$id": "http://example.com/root.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": {
"$ref": "http://example.com/name.json"
}
}
};
const resolvedSchema = resolver.resolve(schema);
console.log(JSON.stringify(resolvedSchema, null, 2));
Other packages similar to json-schema-resolver
json-schema-ref-parser
json-schema-ref-parser is a powerful library for parsing, resolving, and dereferencing JSON schema $ref pointers. It offers similar functionality to json-schema-resolver but includes additional features like bundling and dereferencing schemas with circular references.
json-schema-deref-sync
json-schema-deref-sync is a synchronous library for dereferencing JSON schema $ref pointers. It is similar to json-schema-resolver but operates synchronously, making it suitable for use cases where asynchronous operations are not desired.
json-schema-deref
json-schema-deref is another library for dereferencing JSON schema $ref pointers. It provides similar functionality to json-schema-resolver but focuses on simplicity and ease of use.
json-schema-resolver

Resolve all $refs
in your JSON schema!
This module will resolve the $ref
keyword against the externalSchemas
you will provide.
By resolving the $ref
keyword, means that you get back a single BIG inlined JSON schema that does not rely on any external schema.
If a reference is missing, it will not throw any error.
Install
npm install json-schema-resolver
This plugin support Node.js >= 10
Usage: resolve one schema against external schemas
The $ref
string is going to be modified to point to a local reference URI: #/definitions/<generated key>
.
Moreover, the definitions
keyword will be decorated with the external schemas to get only one JSON schema resolved as output.
By default the <generated key>
has the def-${index}
format.
You can customize it by passing a buildLocalReference
function as follows:
const RefResolver = require('json-schema-resolver')
const ref = RefResolver({
clone: true,
buildLocalReference (json, baseUri, fragment, i) {
return `def-${i}`
}
})
const inputSchema = {
$id: 'http://example.com/SimplePerson',
type: 'object',
properties: {
name: { type: 'string' },
address: { $ref: 'relativeAddress#' },
houses: { type: 'array', items: { $ref: 'relativeAddress#' } }
}
}
const addresSchema = {
$id: 'relativeAddress',
type: 'object',
properties: {
zip: { type: 'string' },
city: { type: 'string' }
}
}
const singleSchema = ref.resolve(inputSchema, { externalSchemas: [addresSchema] })
singleSchema
will be like:
{
"$id": "http://example.com/SimplePerson",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"address": {
"$ref": "#/definitions/def-0"
},
"houses": {
"type": "array",
"items": {
"$ref": "#/definitions/def-0"
}
}
},
"definitions": {
"def-0": {
"$id": "relativeAddress",
"type": "object",
"properties": {
"zip": {
"type": "string"
},
"city": {
"type": "string"
}
}
}
}
}
Usage: resolve multiple schemas against external shared schemas
When you have multiple schemas to resolve against a collection of shared schema you need to use this
module with little changes.
This is needed to have all the same definitions path (#/definitions/<generated key>
) across all the
root schemas
const ref = RefResolver({
clone: true,
applicationUri: 'my-application.org',
externalSchemas: [addresSchema]
})
const inputSchema = {
$id: 'http://example.com/SimplePerson',
type: 'object',
properties: {
name: { type: 'string' },
address: { $ref: 'relativeAddress#' },
houses: { type: 'array', items: { $ref: 'relativeAddress#' } }
}
}
const singleSchema = ref.resolve(inputSchema)
const anotherResolvedSchema = ref.resolve(input_2_Schema)
const sharedDefinitions = ref.definitions()
Debug
To debug this module, simply set:
export DEBUG=json-schema-resolver
License
Licensed under MIT.