# jtdc

> JSON Type Definition to TypeScript compiler.

Latest version **1.0.0** (published 2021-07-13) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install jtdc
pnpm add jtdc
yarn add jtdc
bun add jtdc
```

Provides the command `jtdc`.

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-07-13 |
| First published | 2021-07-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 141.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Savva Mikhalevski |
| Maintainers | smikhalevski |

## Links

- npm: https://www.npmjs.com/package/jtdc
- Repository: https://github.com/smikhalevski/jtdc
- Homepage: https://github.com/smikhalevski/jtdc#readme
- Issues: https://github.com/smikhalevski/jtdc/issues
- npm.io page: https://npm.io/package/jtdc

## Dependencies (3)

- [glob](https://npm.io/package/glob.md) ^7.1.7
- [commander](https://npm.io/package/commander.md) ^8.0.0
- [@smikhalevski/codegen](https://npm.io/package/@smikhalevski/codegen.md) ^1.0.0

## Recent versions

- 1.0.0 (latest) — 2021-07-13

## README

# jtdc

[JSON Type Definition (RFC8927)](https://jsontypedef.com/) to TypeScript compiler.

- Compile enums, interface and types;
- Modify naming of enums, enum keys and values, interfaces, types, properties and any other rendered entities;
- Compile validator functions that produce an array of detected validation errors;
- Validators support recursive structures and shallow checks;
- Compile [type narrowing functions](https://www.typescriptlang.org/docs/handbook/2/narrowing.html) aka type guards;
- Create custom validator dialect and have explicit control over every aspect of code generation;
- CLI and programmatic usage;

[Full API documentation.](https://smikhalevski.github.io/jtdc/)

```shell
npm install --save-prod jtdc
```

## CLI usage

Let's assume you have user and account type definitions in separate files under `./src` folder:

```json5
// ./src/user.json

{
  "user": {
    "properties": {
      "email": {"type": "string"},
      "friends": {
        "elements": {"ref": "user"}
      }
    },
    "optionalProperties": {
      "name": {"type": "string"},
      "age": {"type": "int8"}
    }
  }
}
```

```json5
// ./src/account.json

{
  "account": {
    "properties": {
      "user": {"ref": "user"},
      "stats": {
        "properties": {
          "visitCount": {"type": "int32"}
        }
      }
    },
    "optionalProperties": {
      "roles": {
        "metadata": {
          "comment": "Default role is guest"
        },
        "elements": {"ref": "role"}
      }
    }
  },
  "role": {
    "enum": ["admin", "guest"]
  }
}
```

To compile these definitions to TypeScript use this command:

```sh
npx jtdc --rootDir ./src --includes '*.json' --outDir ./gen --typeGuards
```

The result would be output to `./gen` folder:

<details>
<summary><code>./gen/user.ts</code></summary>
<p>

```ts
import {_a, _i, _o, _O, _S, _s, Validator as _Validator} from 'jtdc/lib/jtd-dialect/runtime';

export interface User {
  email: string;
  friends: Array<User>;
  name?: string;
  age?: number;
}

const validateUser: _Validator = (a, b, c) => {
  let d, e, f, g, h;
  b = b || {};
  c = c || '';
  if (_o(a, b, c)) {
    _s(a.email, b, c + '/email');
    d = a.friends;
    e = c + '/friends';
    if (_a(d, b, e)) {
      for (f = 0; f < d.length; f++) {
        validateUser(d[f], b, e + _S + f);
      }
    }
    g = a.name;
    if (_O(g)) {
      _s(g, b, c + '/name');
    }
    h = a.age;
    if (_O(h)) {
      _i(h, b, c + '/age');
    }
  }
  return b.errors;
};
export {validateUser};
const isUser = (value: unknown): value is User => !validateUser(value, {shallow: true});
export {isUser};
```

</p>
</details>

<details>
<summary><code>./gen/account.ts</code></summary>
<p>

```ts
import {_a, _e, _i, _o, _O, _S, Validator as _Validator} from 'jtdc/lib/jtd-dialect/runtime';
import {User, validateUser} from './user.ts';

export interface Account {
  user: User;
  stats: { visitCount: number; };
  /**
   * Default role is guest
   */
  roles?: Array<Role>;
}

enum Role {ADMIN = 'admin', GUEST = 'guest',}

export {Role};
const validateAccount: _Validator = (a, b, c) => {
  let d, e, f, g, h;
  b = b || {};
  c = c || '';
  if (_o(a, b, c)) {
    validateUser(a.user, b, c + '/user');
    d = a.stats;
    e = c + '/stats';
    if (_o(d, b, e)) {
      _i(d.visitCount, b, e + '/visitCount');
    }
    f = a.roles;
    if (_O(f)) {
      g = c + '/roles';
      if (_a(f, b, g)) {
        for (h = 0; h < f.length; h++) {
          validateRole(f[h], b, g + _S + h);
        }
      }
    }
  }
  return b.errors;
};
export {validateAccount};

const isAccount = (value: unknown): value is Account => !validateAccount(value, {shallow: true});
export {isAccount};

const validateRole: _Validator = (a, b, c) => {
  b = b || {};
  _e(a, (validateRole.cache ||= {}).a ||= ['admin', 'guest'], b, c || '');
  return b.errors;
};
export {validateRole};

const isRole = (value: unknown): value is Role => !validateRole(value, {shallow: true});
export {isRole};
```

</p>
</details>

## Programmatic usage

[Full API documentation.](https://smikhalevski.github.io/jtdc/)

```ts
import {compileTsModules} from 'jtdc';
import userJson from './src/user.json';
import accountJson from './src/account.json';

compileTsModules({
  './user': userJson,
  './account': accountJson,
});
// → {'./user': 'import …', './account': 'import …'}
```

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