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

schematized

v1.9.0

Published

Turn objects into JSON schemas! The more examples you provide, the better your schema will be.

Downloads

82

Readme

Schematized

npm version Build-Test-Publish semantic-release XO code style

Turn objects into JSON schemas! The more examples you provide, the better your schema will be.

A Node port of the Python module GenSON but with more inferred constraints.

Example use case: Generate JSON schemas using your API tests, then use the schemas to validate. To keep up to date, Write a test that compares your current schema with the generated schema. Then when your API changes, just update the tests with the newly generated schemas and move on with your day.



:rocket: Quick start

  1. Add dependency
yarn add schematized
  1. Basic usage : See output
import SchemaBuilder from 'schematized' // Typescript & ESM
const { default: SchemaBuilder } = require('schematized') // CommonJS

const builder = new SchemaBuilder()

// Consume JSON
builder.addObject({
  token: '74aea1a53d68b77e4f1f55fa90a7eb81',
  role: ['Basic'],
})

// Produce JSON Schemas!
const schema = builder.toPrettySchema()
  1. Improve the schema with examples : See output
...

builder.addObject({
  token: 'Bearer 6498d9afc96d1d8d881a2b7ded4f9290',
  role: [
    'Admin',
    'Basic',
    'Publisher'
  ]
})
  1. Improve the schema with existing schemas : See output
...

builder.addSchema({
  title: '/user server response',
  description: '/user server response'
})

Schema from the single example

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "token": {
      "type": "string",
      "maxLength": 32,
      "minLength": 32
    },
    "role": {
      "type": "array",
      "items": {
        "type": "string",
        "maxLength": 5,
        "minLength": 5
      }
    }
  },
  "required": [
    "role",
    "token"
  ],
  "additionalProperties": false,
  "maxProperties": 2,
  "minProperties": 2
}

Schema from the two examples

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "token": {
      "type": "string",
      "maxLength": 39,
      "minLength": 32
    },
    "role": {
      "type": "array",
      "items": {
        "type": "string",
        "maxLength": 9,
        "minLength": 5
      }
    }
  },
  "required": [
    "role",
    "token"
  ],
  "additionalProperties": false,
  "maxProperties": 2,
  "minProperties": 2
}

Schema from the two examples and the schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "/user server response",
  "description": "/user server response",
  "type": "object",
  "properties": {
    "token": {
      "type": "string",
      "maxLength": 39,
      "minLength": 32
    },
    "role": {
      "type": "array",
      "items": {
        "type": "string",
        "maxLength": 9,
        "minLength": 5
      }
    }
  },
  "required": [
    "role",
    "token"
  ],
  "additionalProperties": false,
  "maxProperties": 2,
  "minProperties": 2
}

:books: API

| Method | Definition | Parameter | | --------------------- | ----------------------------------------------- | ----------------- | | .addObject($object) | Add an example to improve the generated schema. | Valid JSON object | | .addSchema($schema) | Add schemas to improve the generated schema. | Valid JSON Schema | | .toSchema() | Generate the schema. | None | | .toPrettySchema() | Generate the schema and pretty print. | None |

:dart: Supported Schema Features

Visit the official JSON Schema site for specification details.

Types

| Type | Supported | | ------- | --------- | | String | Yes | | Number | Yes | | Integer | Never | | Object | Yes | | Array | Yes | | Tuple | Not yet | | Boolean | Yes | | Null | Yes |

Typeless

| Constraint | addSchema() | addObject() | | ------------ | ----------- | ----------- | | title | Yes | Never | | description | Yes | Never | | $comment | Yes | Never | | default | Not yet | Not yet | | examples | Not yet | Not yet | | enum | Not yet | Not yet | | const | Not yet | Not yet | | anyOf | Yes | Yes | | allOf | Not yet | Not yet | | oneOf | Not yet | Not yet | | not | Not yet | Not yet | | if/then/else | Not yet | Not yet |

String

| Constraint | addSchema() | addObject() | | ---------------- | ----------- | ----------- | | maxLength | Yes | Yes | | minLength | Yes | Yes | | format | Yes | Yes | | pattern | Not yet | Not yet | | contentMediaType | Not yet | Not yet | | contentEncoding | Not yet | Not yet |

Supported String formats

| Format | Supported? | | --------------------- | ---------- | | date-time | Yes | | date | Yes | | time | Yes | | email | Yes | | hostname | Not yet | | idn-hostname | Not yet | | ipv4 | Yes | | ipv6 | Yes | | uri | Yes | | uri-reference | Not yet | | url | Yes | | uuid | Yes | | iri | Not yet | | iri-reference | Not yet | | uri-template | Not yet | | json-pointer | Not yet | | relative-json-pointer | Not yet | | regex | Not yet |

Number

| Constraint | addSchema() | addObject() | | ---------------- | ----------- | ----------- | | maximum | Yes | Yes | | minimum | Yes | Yes | | exclusiveMaximum | Not yet | Not yet | | exclusiveMinimum | Not yet | Not yet | | multiple | Not yet | Not yet |

Object

| Constraint | addSchema() | addObject() | | -------------------- | ----------- | ----------- | | propertyPatterns | Yes | Yes | | additionalProperties | Yes | Yes | | required | Yes | Yes | | maxProperties | Yes | Yes | | minProperties | Yes | Yes |

Array

| Constraint | addSchema() | addObject() | | ----------- | ----------- | ----------- | | maxItems | Not yet | Not yet | | minItems | Not yet | Not yet | | uniqueItems | Not yet | Not yet |