GithubHelp home page GithubHelp logo

eomm / json-schema-resolver Goto Github PK

View Code? Open in Web Editor NEW
7.0 4.0 2.0 26 KB

Resolve all your JSON Schema $refs to relative path

License: MIT License

JavaScript 100.00%
json-schema-resolver json-schema definitions ref fastify

json-schema-resolver's Introduction

json-schema-resolver

CI js-standard-style

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, // Clone the input schema without changing it. Default: false,
  buildLocalReference (json, baseUri, fragment, i) {
    // the `json` that is being resolved
    // the `baseUri` object of the schema. Its values is the parse result from https://www.npmjs.com/package/uri-js
    // the `fragment` is the `$ref` string when the `$ref` is a relative reference
    // the `i` is a local counter to generate a unique key
    return `def-${i}` // default value
  }
})

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', // Note: prefer always absolute URI like: http://mysite.com
  type: 'object',
  properties: {
    zip: { type: 'string' },
    city: { type: 'string' }
  }
}

const singleSchema = ref.resolve(inputSchema, { externalSchemas: [addresSchema] })
// inputSchema is untouched thanks to clone:true

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, // Clone the input schema without changing it. Default: false
  applicationUri: 'my-application.org', // You need to provide an unique URI to resolve relative `$id`s
  externalSchemas: [addresSchema] // The schemas provided at the creation of the resolver, will be used evvery time `.resolve` will be called
})

const inputSchema = {
  $id: 'http://example.com/SimplePerson',
  type: 'object',
  properties: {
    name: { type: 'string' },
    address: { $ref: 'relativeAddress#' },
    houses: { type: 'array', items: { $ref: 'relativeAddress#' } }
  }
}

// the resolved schema DOES NOT have definitions added
const singleSchema = ref.resolve(inputSchema)
const anotherResolvedSchema = ref.resolve(input_2_Schema) // resolve schemas within the same externalSchemas

// to get the definition you need only to call:
const sharedDefinitions = ref.definitions()

Debug

To debug this module, simply set:

export DEBUG=json-schema-resolver

License

Licensed under MIT.

json-schema-resolver's People

Contributors

eomm avatar robcresswell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

iosonotan fhueser

json-schema-resolver's Issues

Local references in external schemas are not resolved.

    const resolvedSchema = resolver.resolve(
        {
            "$id": "test",
            "title": "Test",
            "type": "object",
            "properties": {
                "TestString": { "$ref": "DefSchema#/$defs/Username" }
            }                      
        }, 
        {
            externalSchemas: [
                {
                    "$id": "DefSchema",
                    "$defs": {
                        "Username": { "$ref": "#/$defs/string:10" },
                        "string:10": {
                            "type": "string",
                            "maxLength": 10
                        }
                    }
                }
            ]
        }
    )

Produces the following incorrect reference:

{
  "$id": "test",
  "definitions": {
     "def-0": {
      "$defs": {
        "Username": { "$ref": "#/$defs/string:10" }
      }
    }
  }
}

As opposed to the expected:

{
  "$id": "test",
  "definitions": {
     "def-0": {
      "$defs": {
        "Username": { "$ref": "#/definitions/def-0/$defs/string:10" }
      }
    }
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.