New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

agnostic-deref

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

agnostic-deref

Synchronous JSON schema dereference utility that supports any file format

  • 0.4.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

Agnostic Dereference

Dereference JSON pointers in a JSON schemas with their true resolved values. A custom parsing function can be specified to support dereferencing of any file format. This has been forked from json-schema-deref.

Installation

npm install agnostic-deref

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('agnostic-deref');
var myschema = require('schema.json');

var fullSchema = deref(myschema);

Agnostic Deference also allows refs of any format to be parsed via the options.parser function. This is useful if you specify your schemas in a non-JSON format, such as YAML:

# schema.yaml
---
  description: User
  title: User
  type: object

  properties:
    subschema:
      $ref: subschema.yaml

# subschema.yaml
---
  description: "Some subschema"
  properties:
    foo:
      type: string
    bar:
      type: object
const yaml = require('js-yaml');
const deref = require('agnostic-deref');
const fs = require('fs');

const schema = yaml.safeLoad(fs.readFileSync('schema.yaml'));

const fullSchema = deref(schema, { parser: yaml.safeLoad });

API Reference

deref(schema, options) ⇒ Object | Error

Derefs $ref's in JSON Schema to actual resolved values. Supports local, and file refs.

Kind: global function Returns: Object | Error - the deref schema oran instance of Error if error.

ParamTypeDescription
schemaObjectThe JSON schema
optionsObjectoptions
options.baseFolderStringthe base folder to get relative path files from. Default is process.cwd()
options.failOnMissingBooleanBy default missing / unresolved refs will be left as is with their ref value intact. If set to true we will error out on first missing ref that we cannot resolve. Default: false.
options.parserFunctionThe function invoked to parse the contents of files located at each $ref. This defaults to JSON.parse.

Keywords

FAQs

Package last updated on 23 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc