JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 104609
  • Score
    100M100P100Q192026F
  • License MIT

Convert JS object to JSON Schema

Package Exports

  • to-json-schema

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (to-json-schema) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm version Build Status Coverage Status

to-json-schema

Converts javascript objects (and other types) to corresponding JSON schema

Install

npm install to-json-schema

Example usage

const toJsonSchema = require('to-json-schema');

const objToBeConverted = {
  name: 'David',
  rank: 7,
  born: '1990-04-05T15:09:56.704Z',
  luckyNumbers: [7, 77, 5]
};

const schema = toJsonSchema(objToBeConverted);

schema from above code will look like this:

{
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        },
        "rank": {
            "type": "integer"
        },
        "born": {
            "type": "string",
            "format": "date-time"
        },
        "luckyNumbers": {
            "type": "array",
            "items": {
                "type": "integer"
            }
        }
    }
}

toJsonSchema(value, options)

to-json-schema exports function that is able to convert most javascript values to JSON schema. Such a schema can be used to further validation of similar objects/values

  • value: required Any javascript value
  • options: documentation will be updated later