Skip to content

CINEPASS/json-schema-deref-sync

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-schema-deref-sync

Dereference JSON pointers in a JSON schemas with their true resolved values. Basically a lighter, synchronous version of json-schema-deref but omits web references and custom loaders.

Installation

npm install json-schema-deref-sync

Overview

Let's say you have the following JSON Schema:

{
  "description": "Just some JSON schema.",
  "title": "Basic Widget",
  "type": "object",
  "definitions": {
    "id": {
      "description": "unique identifier",
      "type": "string",
      "minLength": 1,
      "readOnly": true
    }
  },
  "properties": {
  "id": {
    "$ref": "#/definitions/id"
  },
  "foo": {
    "$ref": "http://www.mysite.com/myschema.json#/definitions/foo"
  },
  "bar": {
    "$ref": "bar.json"
  }
}

Sometimes you just want that schema to be fully expanded, with $ref's being their (true) resolved values:

{
  "description": "Just some JSON schema.",
  "title": "Basic Widget",
  "type": "object",
  "definitions": {
    "id": {
      "description": "unique identifier",
      "type": "string",
      "minLength": 1,
      "readOnly": true
    }
  },
  "properties": {
    "id": {
      "description": "unique identifier",
      "type": "string",
      "minLength": 1,
      "readOnly": true
    },
    "foo": {
      "description": "foo property",
      "readOnly": true,
      "type": "number"
    },
    "bar": {
      "description": "bar property",
      "type": "boolean"
    }
  }
}

This utility lets you do that:

var deref = require('json-schema-deref-sync');
var myschema = require('schema.json');

var fullSchema = deref(myschema);

API

deref(schema, options)

Dereferences $ref's in json schema to actual resolved values. Supports local, and file refs.

If circular references are found it returns an instance of Error.

Parameters:

schema

The input JSON schema

options
  • baseFolder - the base folder to get relative path files from. Default is process.cwd()

  • keepLocalRefs - Whether local $refs like "$ref": "#/definitions/foo" should not dereferenced. Passing true will pull all definitions to the root of the resulting schema. Default is false

deref.getRefPathValue(schema, refPath)

Gets the "local" ref value given the path.

schema - the (root) json schema to search

refPath - string ref path to get within the schema. Ex. #/definitions/id

var localValue = deref.getRefPathValue(myschema, '#/definitions/foo');
console.dir(localValue);

About

Synchronous json schema dereference utility

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%