Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fast-typescript-to-jsonschema

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-typescript-to-jsonschema

fast-typescript-to-jsonschema generates JSON Schema files from your Typescript sources.

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
decreased by-90%
Maintainers
1
Weekly downloads
 
Created
Source

fast-typescript-to-jsonschema

npm version Test codecov

生成typescript类型的jsonschema数据

特性

  • 编译Typescript文件以获取完整的类型信息
  • 将所需的属性、继承、注释、属性初始值转换为jsonschema

使用

  • 1.安装依赖
yarn add fast-typescript-to-jsonschema -D
  • 2.创建type.ts文件,内容如下:
interface ITest {
  attr1: string;
  attr2: number;
  attr3?: boolean;
}
  • 3.创建test.js文件,内容如下:
const { default: genTypeSchema } = require('fast-typescript-to-jsonschema');
const path = require('path');

// 目标文件
const file = path.resolve(__dirname, './type.ts');

// 生成数据
genTypeSchema.genJsonDataFormFile(file);

// 获得当前文件对应的所有jsonschema数据
const json = genTypeSchema.genJsonData();

// 获得具体的某个type的jsonschema数据
const jsonSchema = genTypeSchema.getJsonSchema(file, 'ITest');

// 返回结果
console.log(jsonSchema); 
  • 4.执行脚本
node ./test.js

jsonSchema返回结果如下:

{
  "additionalProperties": false,
  "properties": {
    "attr1": {
      "type": "string",
    },
    "attr2": {
      "type": "number",
    },
    "attr3": {
      "type": "boolean",
    },
  },
  "required": [
    "attr1",
    "attr2",
  ],
  "type": "object",
}

注释

示例1

interface Interface_1 {
  attr: string;
}

结果:

{
  "additionalProperties": false,
  "properties": {
    "attr": {
      "type": "string",
    },
  },
  "required": [
    "attr",
  ],
  "type": "object",
}

示例2

interface Interface_4 {
  attr: string[];
}

结果:

{
  "additionalProperties": false,
  "properties": {
    "attr": {
      "items": {
        "type": "string",
      },
      "type": "array",
    },
  },
  "required": [
    "attr",
  ],
  "type": "object",
}

更多支持的类型解析请看,目录如下:

贡献

我们非常欢迎您的贡献,您可以通过以下方式与我们共建。

Keywords

FAQs

Package last updated on 21 Mar 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc