Socket
Socket
Sign inDemoInstall

json-schema-trainer

Package Overview
Dependencies
1
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    json-schema-trainer

Create JSON Schemas by training with objects


Version published
Maintainers
1
Install size
904 kB
Created

Readme

Source

json-schema-trainer

Create JSON Schemas through training with objects.

Features

  • Detect property type(s)
  • Set required properties
  • Set minimum number value
  • Set minimum array length
  • Detect string format (uri, email, hostname, ipv4, ipv6, date)
  • Create enum properties based on the number of options

Example

Feed the SchemaTrainer some objects:

var schemaTrainer = new SchemaTrainer

schemaTrainer.train({
	gender: 'male',
	name: 'Joe'
})

schemaTrainer.train({
	gender: 'female',
	name: 'Martha'
})

schemaTrainer.train({
	gender: 'male',
	name: 'Bob'
})

schemaTrainer.train({
	gender: 'female',
	name: 'Cindy'
})

console.log(schemaTrainer.toJS())

Call toJS() to return an object with the JSON Schema

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "gender": {
            "enum": ["male", "female"]
        },
        "name": {
            "type": "string"
        }
    },
    "required": ["gender", "name"]
}

Example schema generated from https://anapioficeandfire.com/api/characters/*

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "url": {
            "type": "string",
            "format": "uri"
        },
        "name": {
            "type": "string"
        },
        "gender": {
            "enum":[
                "Female",
                "Male"
            ]
        },
        "culture": {
            "type": "string"
        },
        "born": {
            "type": "string"
        },
        "died": {
            "type": "string"
        },
        "titles": {
            "type": "array",
            "items": {
                "type": "string"
            }
        },
        "aliases": {
            "type": "array",
            "items": {
                "type": "string"
            }
        },
        "father": {
            "type": "string"
        },
        "mother": {
            "type": "string"
        },
        "spouse": {
            "type": "string"
        },
        "allegiances": {
            "type": "array",
            "items": {
                "type": "string",
                "format": "uri"
            }
        },
        "books": {
            "type": "array",
            "items": {
                "type": "string",
                "format": "uri"
            }
        },
        "povBooks": {
            "type": "array",
            "items": {
                "type": "string",
                "format": "uri"
            }
        },
        "tvSeries": {
            "type": "array",
            "items": {
                "type": "string"
            }
        },
        "playedBy": {
            "type": "array",
            "items": {
                "type": "string"
            }
        }
    },
    "required":[
        "url",
        "name",
        "gender",
        "culture",
        "born",
        "died",
        "titles",
        "aliases",
        "father",
        "mother",
        "spouse",
        "allegiances",
        "books",
        "povBooks",
        "tvSeries",
        "playedBy"
    ]
}

FAQs

Last updated on 20 Oct 2016

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc