npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-schema-generator

v2.0.6

Published

JSON schema generator based on draft-v4.

Downloads

66,657

Readme

json-schema-generator

JSON schema generated based on draft-v4 of the specification. Note that the full spec if not yet supported. The compiler will be enhanced to support as much as possible. More specifically, there's no support for $ref nodes or special nodes like location (lat, long), etc. These features will be added in future releases or you can always fork and make it better :-)

Install (GIT)

git clone https://github.com/krg7880/json-schema-generator
cd json-schema-generator
npm install .

Install as cli (NPM)

Run on the command line:

npm install -g json-schema-generator

Then use (for example):

#### JSON PATH
json-schema-generator path/to/input.json -o path/to/output.json
#### JSON URL
json-schema-generator https://sample.com/path/to/input.json --jsondir ./source/backup -o ./path/to/dir/
#### JSON STDIN | STDOUT
cat input.json | json-schema-generator > output.json

Install as lib (NPM)

Run on the command line:

npm install json-schema-generator --save-dev

Then, in your project:

var jsonSchemaGenerator = require('json-schema-generator'),
    obj = { some: { object: true } },
    schemaObj;

schemaObj = jsonSchemaGenerator(json);

Cli usage

Usage: json-schema-generator [<target>|--url <url>|--file <file>|--stdin]

If <target> is specified, it is interpreted as follows: a protocol (like http://) 
means url; anything else is treated as path to a local file. 
If no input file is specified and stdin is provided, stdin is used.

Options:
  --stdin          Use stdin as input.                                              
  --url            Remote json document to use as input.                            
  --file           Local json document to use as input.                             
  --schemadir, -o  Directory (or file, if ending with .json) where the schema will
                   be stored.                                                       
  --jsondir        Directory (or file, if ending with .json) where the source
                   document is copied to. Useful with --url.                        
  --pretty         Whether to use pretty json format. Use --no-pretty for false.
                                                                     [default: true]
  --force, -f      If a destination file already exists, overwrite it.              
  --help, -h       Show this help text.                                             

Example JSON

{
    "title": "fresh fruit schema v1",
    "type": "object",
    "required": ["skin", "colors", "taste"],
    "properties": {
        "colors": {
            "type": "array",
            "minItems": 1,
            "uniqueItems": true,
            "items": {
                "type": "string"
            }
        },
        "skin": {
            "type": "string"
        },
        "taste": {
            "type": "number",
            "minimum": 5
        }
    }
}

Example Output

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "description": "",
  "type": "object",
  "properties": {
    "title": {
      "type": "string",
      "minLength": 1
    },
    "type": {
      "type": "string",
      "minLength": 1
    },
    "required": {
      "type": "array",
      "items": {
        "required": [
          
        ],
        "properties": {
          
        }
      }
    },
    "properties": {
      "type": "object",
      "properties": {
        "colors": {
          "type": "object",
          "properties": {
            "type": {
              "type": "string",
              "minLength": 1
            },
            "minItems": {
              "type": "number"
            },
            "uniqueItems": {
              "type": "boolean"
            },
            "items": {
              "type": "object",
              "properties": {
                "type": {
                  "type": "string",
                  "minLength": 1
                }
              },
              "required": [
                "type"
              ]
            }
          },
          "required": [
            "type",
            "minItems",
            "uniqueItems",
            "items"
          ]
        },
        "skin": {
          "type": "object",
          "properties": {
            "type": {
              "type": "string",
              "minLength": 1
            }
          },
          "required": [
            "type"
          ]
        },
        "taste": {
          "type": "object",
          "properties": {
            "type": {
              "type": "string",
              "minLength": 1
            },
            "minimum": {
              "type": "number"
            }
          },
          "required": [
            "type",
            "minimum"
          ]
        }
      },
      "required": [
        "colors",
        "skin",
        "taste"
      ]
    }
  },
  "required": [
    "title",
    "type",
    "required",
    "properties"
  ]
}

Background

I created this schema generator to validate JSON responses from APIs. As the JSON API is enhanced and nodes are added or removed from the response, the schema is regenerated and validated against the newly deployed API.

Tests

To run tests, including fetching documents via HTTP, we've added node-stubby-server-cli to help with serving mock data. The ports for the stub server is defined under test/helpers/stubby-cli, in the event the default port is in use, you can change them there.

npm install -g stubby

Install mocha globally (as cli) and run

npm test

Validating Documents

JSON documents can be validated against schemas using chai-json-schema. See the tests under test for example usage.

Contributors

Thanks to those who have contributed. These kind folks are listed below: