tiny-jsonschema
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

tiny-jsonschema

A succinct json-schema language, simplify the json-schema definition.

This project is modified from easy-json-schema

install

npm install tiny-jsonschema

Usage

const {toJsonSchema} = require('tiny-jsonschema');
// or
// import {toJsonSchema} from 'tiny-jsonschema';
const jsonSchema = toJsonSchema(json);
console.log(jsonSchema);

Examples

A simple example

Input:

{
  "id": "string",
  "*name": "string",
  "*email": "string",
  "arr": [
    {
      "site": "string",
      "url": "string"
    }
  ]
}

Output:

{
  "type": "object",
  "required": ["name", "email"],
  "properties": {
    "id": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "email": {
      "type": "string"
    },
    "arr": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [],
        "properties": {
          "site": {
            "type": "string"
          },
          "url": {
            "type": "string"
          }
        }
      }
    }
  }
}

A more complex example

Input:

{
  "*id": "string",
  "*name": {
    "$type": "string",
    "enum": ["tom", "jay"],
    "minLength": 1,
    "maxLength": 10
  },
  "*images": [
    {
      "*id": "number",
      "names": {
        "$type": "array",
        "title": "Images Collections.",
        "items": {
          "*id": "string",
          "*name": "string"
        }
      }
    }
  ],
  "abc": {
    "type": "string",
    "a": {
      "x": "string",
      "y": {
        "$type": "number",
        "minimum": 400000,
        "maximum": 900000
      }
    }
  }
}

Output:

{
  "type": "object",
  "required": ["id", "name", "images"],
  "properties": {
    "id": {
      "type": "string"
    },
    "name": {
      "type": "string",
      "enum": ["tom", "jay"],
      "minLength": 1,
      "maxLength": 10
    },
    "images": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["id"],
        "properties": {
          "id": {
            "type": "number"
          },
          "names": {
            "type": "array",
            "title": "Images Collections.",
            "items": {
              "type": "object",
              "required": ["id", "name"],
              "properties": {
                "id": {
                  "type": "string"
                },
                "name": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    },
    "abc": {
      "type": "object",
      "required": [],
      "properties": {
        "type": {
          "type": "string"
        },
        "a": {
          "type": "object",
          "required": [],
          "properties": {
            "x": {
              "type": "string"
            },
            "y": {
              "type": "number",
              "minimum": 400000,
              "maximum": 900000
            }
          }
        }
      }
    }
  }
}

LICENSE

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i tiny-jsonschema

Weekly Downloads

0

Version

0.1.2

License

MIT

Unpacked Size

16.8 kB

Total Files

15

Last publish

Collaborators

  • towyuan