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

@jsonhero/schema-generator

v0.2.2

Published

Generate a draft-07 JSON schema definitions from an example JSON document

Downloads

6

Readme

Schema Generator

Generate a draft-07 JSON schema definitions from an example JSON document

Coverage lines

Features

  • Generate a draft-07 JSON schema definitions from an example JSON document
  • Easy to use
  • Supports string formats (with more to come)
  • Can generate either a strict or liberal schema

Usage

Install Schema Generator

$ npm install --save @jsonhero/schema-generator

generateSchema takes either a JSON object/array or JSON stringified and returns a valid draft-07 JSON Schema

const { generateSchema } = require("@jsonhero/schema-generator");

const exampleJSON = {
  id: 1234,
  name: "Eric",
  image: {
    url: "https://foo.com/image.png",
  },
  email: "[email protected]",
  homepage: "http://github.com/ericallam",
  createdAt: "2021-12-17T12:55:58.141Z",
  tags: ["foo", "bar"],
  isAdmin: true,
};

const schema = generateSchema(exampleJSON);

// Now validate the provided JSON using the generated schema using ajv
const Ajv = require("ajv");
const ajv = new Ajv();
const addFormats = require("ajv-formats");
addFormats(ajv);

const validate = ajv.compile(schema);
const valid = validate(exampleJSON); // valid == true

The Schema generated in the above example

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://jsonhero.io/schemas/root.json",
  "type": "object",
  "properties": {
    "id": {
      "type": "number"
    },
    "name": {
      "type": "string"
    },
    "image": {
      "type": "object",
      "properties": {
        "url": {
          "type": "string",
          "format": "uri"
        }
      },
      "required": [],
      "additionalProperties": true
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "homepage": {
      "type": "string",
      "format": "uri"
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "friends": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "type": "number"
          },
          "name": {
            "type": "string"
          }
        },
        "required": [],
        "additionalProperties": true
      }
    },
    "isAdmin": {
      "type": "boolean"
    }
  },
  "required": [],
  "additionalProperties": true
}

By default all properties are optional and additionalProperties are allowed. You can set the strict option to true if you'd like a more restrictive schema

const { generateSchema } = require("@jsonhero/schema-generator");

const exampleJSON = {
  id: 1234,
  name: "Eric",
  image: {
    url: "https://foo.com/image.png",
  },
  email: "[email protected]",
  homepage: "http://github.com/ericallam",
  createdAt: "2021-12-17T12:55:58.141Z",
  tags: ["foo", "bar"],
  isAdmin: true,
};

const schema = generateSchema(exampleJSON, { strict: true });

// Now validate the provided JSON using the generated schema using ajv
const Ajv = require("ajv");
const ajv = new Ajv();
const addFormats = require("ajv-formats");
addFormats(ajv);

const validate = ajv.compile(schema);
const valid = validate(exampleJSON); // valid == true

The Schema generated in the above example

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://jsonhero.io/schemas/root.json",
  "type": "object",
  "properties": {
    "id": {
      "type": "number"
    },
    "name": {
      "type": "string"
    },
    "image": {
      "type": "object",
      "properties": {
        "url": {
          "type": "string",
          "format": "image"
        }
      },
      "required": ["url"],
      "additionalProperties": false
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "homepage": {
      "type": "string",
      "format": "uri"
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "friends": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "type": "number"
          },
          "name": {
            "type": "string"
          }
        },
        "required": ["id", "name"],
        "additionalProperties": false
      }
    },
    "isAdmin": {
      "type": "boolean"
    }
  },
  "required": [
    "id",
    "name",
    "image",
    "email",
    "homepage",
    "createdAt",
    "tags",
    "friends",
    "isAdmin"
  ],
  "additionalProperties": false
}

Generate Schema currently supports a few string formats

  • Strings that are URLs will have the uri format
  • Strings that are Date/Times will have the date-time format
  • Strings that are emails will have the email format

If you are using ajv to use the generated schema, make sure to use the ajv-formats package

const addFormats = require("ajv-formats");
addFormats(ajv);

You can also set the baseURI of the schema (i.e. the top level $id property)

const { generateSchema } = require("@jsonhero/schema-generator");

const exampleJSON = {
  foo: "bar",
};

const schema = generateSchema(exampleJSON, {
  baseURI: "https://example.com/schemas/root.json",
});
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://example.com/schemas/root.json",
  "type": "object",
  "properties": {
    "foo": {
      "type": "string"
    }
  },
  "required": [],
  "additionalProperties": true
}

Roadmap

  • [ ] Support more draft versions of JSON Schema
  • [ ] Support JSON Type Definitions
  • [ ] Support enums and constants
  • [ ] Support more string formats (regex, ipv4, ipv6, hostname, JSONPointer, time, date, uuid)
  • [ ] Add examples