# create-ts-json-schema

> generate json-schema from typescript source code and management it

Latest version **0.11.4** (published 2022-11-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install create-ts-json-schema
pnpm add create-ts-json-schema
yarn add create-ts-json-schema
bun add create-ts-json-schema
```

Provides the command `ctjs`.

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.11.4 |
| Published | 2022-11-16 |
| First published | 2022-10-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 29 |
| Unpacked size | 983.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | ByungJoon Lee |
| Maintainers | jooni |

## Links

- npm: https://www.npmjs.com/package/create-ts-json-schema
- Repository: https://github.com/imjuni/create-ts-json-schema
- Homepage: https://github.com/imjuni/create-ts-json-schema#readme
- Issues: https://github.com/imjuni/create-ts-json-schema/issues
- npm.io page: https://npm.io/package/create-ts-json-schema

## Dependencies (29)

- [ora](https://npm.io/package/ora.md) ^5.4.1
- [pino](https://npm.io/package/pino.md) ^8.6.1
- [chalk](https://npm.io/package/chalk.md) ^4.1.2
- [dayjs](https://npm.io/package/dayjs.md) ^1.11.6
- [tslib](https://npm.io/package/tslib.md) ^2.4.0
- [yargs](https://npm.io/package/yargs.md) ^17.6.0
- [mathjs](https://npm.io/package/mathjs.md) ^11.3.0
- [find-up](https://npm.io/package/find-up.md) ^5.0.0
- [fuse.js](https://npm.io/package/fuse.js.md) ^6.6.2
- [inquirer](https://npm.io/package/inquirer.md) ^8.2.4
- [minimist](https://npm.io/package/minimist.md) ^1.2.7
- [ts-morph](https://npm.io/package/ts-morph.md) ^16.0.0
- [url-join](https://npm.io/package/url-join.md) ^5.0.0
- [crypto-js](https://npm.io/package/crypto-js.md) ^4.1.1
- [fast-copy](https://npm.io/package/fast-copy.md) ^3.0.0
- [fast-glob](https://npm.io/package/fast-glob.md) ^3.2.12
- [type-fest](https://npm.io/package/type-fest.md) ^3.1.0
- [url-naong](https://npm.io/package/url-naong.md) ^1.0.0
- [my-easy-fp](https://npm.io/package/my-easy-fp.md) ^0.16.0
- [my-node-fp](https://npm.io/package/my-node-fp.md) ^0.8.1
- [pino-pretty](https://npm.io/package/pino-pretty.md) ^9.1.1
- [tsimportlib](https://npm.io/package/tsimportlib.md) 0.0.3
- [jsonc-parser](https://npm.io/package/jsonc-parser.md) ^3.2.0
- [my-only-either](https://npm.io/package/my-only-either.md) ^1.3.0
- [object-traversal](https://npm.io/package/object-traversal.md) ^1.0.1
- [fast-safe-stringify](https://npm.io/package/fast-safe-stringify.md) ^2.1.1
- [ts-json-schema-generator](https://npm.io/package/ts-json-schema-generator.md) ^1.1.2
- [inquirer-autocomplete-prompt](https://npm.io/package/inquirer-autocomplete-prompt.md) ^2.0.0
- [inquirer-ts-checkbox-plus-prompt](https://npm.io/package/inquirer-ts-checkbox-plus-prompt.md) ^1.0.1

## Recent versions

- 0.11.4 (latest) — 2022-11-16
- 0.11.3 — 2022-11-09
- 0.11.2 — 2022-11-02
- 0.11.1 — 2022-11-02
- 0.11.0 — 2022-11-01
- 0.10.2 — 2022-11-01
- 0.10.1 — 2022-10-31
- 0.10.0 — 2022-10-30
- 0.9.0 — 2022-10-30
- 0.8.2 — 2022-10-28
- 0.8.1 — 2022-10-28
- 0.8.0 — 2022-10-27
- 0.7.0 — 2022-10-25

## README

# create-ts-json-schema

[![Download Status](https://img.shields.io/npm/dw/create-ts-json-schema.svg)](https://npmcharts.com/compare/create-ts-json-schema?minimal=true) [![Github Star](https://img.shields.io/github/stars/imjuni/create-ts-json-schema.svg?style=popout)](https://github.com/imjuni/create-ts-json-schema) [![Github Issues](https://img.shields.io/github/issues-raw/imjuni/create-ts-json-schema.svg)](https://github.com/imjuni/create-ts-json-schema/issues) [![NPM version](https://img.shields.io/npm/v/create-ts-json-schema.svg)](https://www.npmjs.com/package/create-ts-json-schema) [![License](https://img.shields.io/npm/l/create-ts-json-schema.svg)](https://github.com/imjuni/create-ts-json-schema/blob/master/LICENSE)

automatically generate json-schema from TypeScript source code and management it!

## What is create-ts-json-schema?

create-ts-json-schema is tiny cli utility that automatically generate json-schema from TypeScript source code and management generated json-schema. If you use fastify.js that uses json-schema to validate request and reply. But managing a large number of json-schema takes a lot of effort. create-ts-json-schema help that work.

![demo](ctjs_demo.gif)

## installation

```bash
npm install create-ts-json-schema --save-dev
```

## Usage

```bash
# generate json-schema and create database (if exist)
ctjs add

# delete json-schema from database
ctjs del

# truncate database
ctjs truncate

# regenerate all json-schema in database
```

## Fastify.js

Example for fastify

```ts
import fastify from 'fastify';
import fs from 'fs';

const server = fastify();
const db = JSON.parse((await fs.promises.readFile('db.json')).toString());
Object.value(db).forEach((record) => server.addSchema(JSON.parse(record.schema)));

server.get(
  '/pokemon/:name',
  {
    schema: {
      tags: ['Pokemon'],
      summary: 'Pokemon detail information using by name',
      querystring: { $ref: 'IReqPokeDetailQuerystring' }, // You can access local reference for fastify schema store
      params: { $ref: 'IReqPokeDetailParams' }, // You can access local reference for fastify schema store
      response: {
        200: { $ref: 'IPokemonDto' }, // You can access local reference for fastify schema store
      },
    },
  },
  (req) => {
    /* your handler code */
  },
);
```

## command and option

### command

| command  | alias | description                                 |
| -------- | ----- | ------------------------------------------- |
| add      | a     | add or update json-schema to database file  |
| del      | d     | delete json-schema from database file       |
| refresh  | r     | regenerate all json-schema in database file |
| truncate | t     | reset database file                         |

### add option

| option     | alias | description                                                                     |
| ---------- | ----- | ------------------------------------------------------------------------------- |
| project    | p     | tsconfig.json path                                                              |
| config     | c     | configuration file path                                                         |
| output     | o     | database file path                                                              |
| skip-error |       | skip compile error on source file                                               |
| types      |       | TypeScript type of source code. You can use interface, type alias, enum, class. |
| files      |       | TypeScript source code file path                                                |
| format     |       | generated json-schema save format: json, string, base64                         |

### delete option

| option     | alias | description                                                                     |
| ---------- | ----- | ------------------------------------------------------------------------------- |
| project    | p     | tsconfig.json path                                                              |
| config     | c     | configuration file path                                                         |
| output     | o     | database file path                                                              |
| skip-error |       | skip compile error on source file                                               |
| types      |       | TypeScript type of source code. You can use interface, type alias, enum, class. |
| files      |       | TypeScript source code file path                                                |

### refresh option

| option     | alias | description                                             |
| ---------- | ----- | ------------------------------------------------------- |
| project    | p     | tsconfig.json path                                      |
| config     | c     | configuration file path                                 |
| output     | o     | database file path                                      |
| skip-error |       | skip compile error on source file                       |
| format     |       | generated json-schema save format: json, string, base64 |

### truncate option

| option  | alias | description             |
| ------- | ----- | ----------------------- |
| project | p     | tsconfig.json path      |
| config  | c     | configuration file path |
| output  | o     | database file path      |

---
_Source: https://npm.io/package/create-ts-json-schema · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
