0.1.2 • Published 2 years ago

tiny-jsonschema v0.1.2

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

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