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.8
  • 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

English | 简体中文

a tool generate json schema from typescript.

Feature

  • compile Typescript to get all type information
  • convert properties, extends, annotations, and initial values to jsonschema

Usage

  1. install
yarn add fast-typescript-to-jsonschema -D
  1. create type.ts
interface ITest {
  attr1: string;
  attr2: number;
  attr3?: boolean;
}
  1. create test.js

3.1. gen jsonschema from file.

const { default: genTypeSchema } = require('fast-typescript-to-jsonschema');
const path = require('path');

// target file
const file = path.resolve(__dirname, './type.ts');

// generate data
genTypeSchema.genJsonDataFormFile(file);

// get all jsonschema data of current file
const json = genTypeSchema.genJsonData();

// get jsonschema of specific type
const jsonSchema = genTypeSchema.getJsonSchema(file, 'ITest');

// result
console.log(jsonSchema); 

3.2. gen jsonschema from code.

const { default: genTypeSchema } = require('fast-typescript-to-jsonschema');

const code = `
interface ITest {
  attr1: string;
  attr2: number;
  attr3?: boolean;
}
`
// generate data
genTypeSchema.genJsonDataFromCode(code);

// get all jsonschema data of current file
const json = genTypeSchema.genJsonData();

// get jsonschema of specific type
const jsonSchema = genTypeSchema.getJsonSchema('ITest');

// result
console.log(jsonSchema); 
  1. execute script
node ./test.js

jsonSchema result:

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

see more examples at example.

Comments

Example1

interface Interface_1 {
  attr: string;
}

result:

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

Example2

interface Interface_4 {
  attr: string[];
}

result:

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

Read more supported types here

Contribution

Contributions are extremely welcomed by our team, you can contribute to this repository by several ways below.

Keywords

FAQs

Package last updated on 27 Feb 2023

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