Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

schematized

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

schematized

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

  • 1.5.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Schematized

NPM

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.



:rocket: Quickstart

  1. Add dependency
yarn add schematized
  1. Basic usage : See output
import { SchemaBuilder } from 'schematized' // Typescript & ES6+
const { 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 response',
  description: 'User data from server.'
})

Schema from the single example

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

Schema from the two examples

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

Schema from the two examples and the schema

{
  "$schema": "http://json-schema.org/schema#",
  "title": "/user response",
  "description": "User data from server.",
  "type": "object",
  "properties": {
    "token": {
      "type": "string",
      "maxLength": 39,
      "minLength": 32
    },
    "role": {
      "type": "array",
      "items": {
        "type": "string",
        "maxLength": 9,
        "minLength": 5
      }
    }
  },
  "required": [
    "role",
    "token"
  ]
}

📖 API

MethodDefinitionParameter
.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

✅ Supported Schema Features

Visit the official JSON Schema site for specification details.

Types

TypeSupported
StringYes
NumberYes
IntegerNever
ObjectYes
ArrayYes
TupleNot yet
BooleanYes
NullYes

Typeless

ConstraintaddSchema()addObject()
titleYesNever
descriptionYesNever
$commentYesNever
defaultNot yetNot yet
examplesNot yetNot yet
enumNot yetNot yet
constNot yetNot yet
anyOfYesYes
allOfNot yetNot yet
oneOfNot yetNot yet
notNot yetNot yet
if/then/elseNot yetNot yet

String

ConstraintaddSchema()addObject()
maxLengthYesYes
minLengthYesYes
formatYesYes
patternNot yetNot yet
contentMediaTypeNot yetNot yet
contentEncodingNot yetNot yet
Supported String formats
FormatSupported?
date-timeYes
dateYes
timeYes
emailYes
hostnameNot yet
idn-hostnameNot yet
ipv4Yes
ipv6Yes
uriYes
uri-referenceNot yet
urlYes
uuidYes
iriNot yet
iri-referenceNot yet
uri-templateNot yet
json-pointerNot yet
relative-json-pointerNot yet
regexNot yet

Number

ConstraintaddSchema()addObject()
maximumYesYes
minimumYesYes
exclusiveMaximumNot yetNot yet
exclusiveMinimumNot yetNot yet
multipleNot yetNot yet

Object

ConstraintaddSchema()addObject()
propertyPatternsYesYes
additionalPropertiesYesYes
requiredYesYes
maxPropertiesNot yetNot yet
minPropertiesNot yetNot yet

Array

ConstraintaddSchema()addObject()
maxItemsNot yetNot yet
minItemsNot yetNot yet
uniqueItemsNot yetNot yet

Keywords

FAQs

Package last updated on 22 Jun 2020

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