1.9.0 • Published 3 years ago

schematized v1.9.0

Weekly downloads
121
License
MIT
Repository
github
Last release
3 years ago

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

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

:dart: 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
maxPropertiesYesYes
minPropertiesYesYes

Array

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

3 years ago

1.8.9

3 years ago

1.8.8

3 years ago

1.8.7

3 years ago

1.8.6

3 years ago

1.8.5

4 years ago

1.8.4

4 years ago

1.8.2

4 years ago

1.8.3

4 years ago

1.8.1

4 years ago

1.8.0

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.2.0

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.0.3

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago