@hoth/typescript-to-json-schema
TypeScript icon, indicating that this package has built-in type declarations

2.1.2 • Public • Published

typescript-to-json-schema

Language npm package Build Status

TypeScript to JsonSchema Transpiler

Usage

Programmatic use

import {resolve} from 'path';
import {generateSchema} from '@hoth/typescript-to-json-schema';

const {schemas} = generateSchema([resolve('demo.ts')]);

Annotations

For example

company.ts

import { integer } from "@hoth/typescript-to-json-schema";

type employee = {

    /**
     * 雇员名字
     *
     * @maxLength 50
     * @minLength 1
     */
    name: string;

    /**
     * 雇员年龄
     *
     * @minimum 18
     */
    age: integer;
};

interface Department {

    /**
     * 是否开始
     */
    open: boolean | null;

    /**
     * 员工
     *
     * @maxItems 1000
     */
    employee: employee[]
}

export interface Company {

    /**
     * 部门
     *
     * @minItems 1
     */
    departments: Department[]
}

output

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "$id": "http://www.baidu.com/schemas/company.json",
      "$ref": "#/definitions/company",
      "definitions": {
        "department": {
          "type": "object",
          "properties": {
            "open": {
              "oneOf": [
                {
                  "type": "boolean"
                },
                {
                  "type": "null"
                }
              ],
              "description": "是否开始"
            },
            "employee": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/employee"
              },
              "maxItems": 1000,
              "description": "员工"
            }
          },
          "required": [
            "open",
            "employee"
          ]
        },
        "company": {
          "type": "object",
          "properties": {
            "departments": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/department"
              },
              "minItems": 1,
              "description": "部门"
            }
          },
          "required": [
            "departments"
          ]
        },
        "employee": {
          "type": "object",
          "properties": {
            "name": {
              "type": "string",
              "minLength": 1,
              "maxLength": 50,
              "description": "雇员名字"
            },
            "age": {
              "type": "integer",
              "minimum": 18,
              "description": "雇员年龄"
            }
          },
          "required": [
            "name",
            "age"
          ]
        }
      }
    }

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.1.22latest

Version History

VersionDownloads (Last 7 Days)Published
2.1.22
2.1.12
2.1.01
2.1.0-50
2.1.0-40
2.1.0-30
2.1.0-22
2.1.0-10
2.0.10
2.0.1-00
2.0.00
1.1.0-20
1.1.0-10
1.1.0-00
1.0.260
1.0.250
1.0.241
1.0.230
1.0.220
1.0.210
1.0.200
1.0.190
1.0.180
1.0.170
1.0.161
1.0.150
1.0.140
1.0.130
1.0.120
1.0.110
1.0.100
1.0.90
1.0.80
1.0.71
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i @hoth/typescript-to-json-schema

Weekly Downloads

7

Version

2.1.2

License

MIT

Unpacked Size

85 kB

Total Files

17

Last publish

Collaborators

  • yaochang
  • cxtom
  • moxiaobei
  • meixg
  • xxllxhdj