# zodtyping

> Generate zod types from static types & JSON schema.

Latest version **1.2.0** (published 2022-07-15) · MIT license · 0 weekly downloads

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

## Install

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

Provides the command `zodtyping`.

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2022-07-15 |
| First published | 2022-06-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 131.2 MB |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | John Wright |
| Maintainers | johngeorgewright |

## Links

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

## Dependencies (8)

- [tslib](https://npm.io/package/tslib.md) 2.4.0
- [yargs](https://npm.io/package/yargs.md) 17.5.1
- [js-yaml](https://npm.io/package/js-yaml.md) 4.1.0
- [ts-morph](https://npm.io/package/ts-morph.md) 15.1.0
- [@johngw/array](https://npm.io/package/@johngw/array.md) 3.3.0
- [@johngw/error](https://npm.io/package/@johngw/error.md) 2.2.0
- [@johngw/iterator](https://npm.io/package/@johngw/iterator.md) 1.2.0
- [json-schema-to-typescript](https://npm.io/package/json-schema-to-typescript.md) 11.0.1

## Recent versions

- 1.2.0 (latest) — 2022-07-15
- 1.1.0 — 2022-06-06
- 1.0.0 — 2022-06-06

## README

# zodtyping

Generate [zod](https://github.com/colinhacks/zod) from static types & JSON schema.

## Instructions

### Install

```
npm install zod
npm install -D zodtyping
```

### Use from the command line

1. Create a file, in the root of your project, called "zodtyping.yml".

   ```yaml
   # zodtyping.yml

   targetFile: src/other-zod.ts # The file to create
   zodtypeFormat: {type}Zt # Optional: use a custom name format for the created zod type
   typeFormat: {type}Type  # Optional: use a custom name format for the created type
   sourceTypes:
     exportStaticType: true # Optional: export static types as well (true by default)
     file: src/types.ts # The file where your type lives
     type: Foo # The type you want to convert to a zod type
   ```

   You can also specify a list of target files, if you want to create more than one:

   ```yaml
   # zodtyping.yml

   - targetFile: src/other-zod.ts
     sourceTypes:
       file: src/types.ts
       type: Foo

   - targetFile: src/zod.ts
     sourceTypes:
       # Source types can also be a list
       - file: src/types.ts
         type: Foo

       - file: json/my-json-schema.json # You can even use JSON schema files!!
         type: [ExampleType, AnotherExampleType] # You may use an array of types
   ```

1. Then run: `npx zodtyping`

### Use from a script

Basic example:

```ts
import { Generator } from 'zodtyping'

const generator = new Generator({
  targetFile: 'src/zod.ts',
  // optional: runtypeFormat / typeFormat (see above)
})

generator
  .generate([
    { file: 'src/types.ts', type: 'Foo' },
    { file: 'json/my-json-schema.json', type: 'ExampleType' },
  ])
  .then((file) => file.save())
```

#### Passing a custom tsconfig file

```ts
const generator = new Generator({
  targetFile: 'src/zod.ts',
  tsConfigFile: '/path/to/tsconfig.json',
})
```

#### Passing a custom ts-morph project (for the internal compiler)

(see [generate.ts](src/generate.ts) for the defaults)

```ts
import { Project } from 'ts-morph'

const generator = new Generator({
  targetFile: 'src/zod.ts',
  project: new Project({
    // ...
  }),
})
```

## Contributing

1. Install [Yarn](https://yarnpkg.com/)
1. Link your workspace `yarn`
1. If using VSCode, be sure to accept the workspace TypeScript version
1. If using another editor, install the [Yarn SDK](https://yarnpkg.com/getting-started/editor-sdks) for it (and commit with your PR)

## Thanks

Inspired by a [gist](https://gist.github.com/skurfuerst/a07ab23c3e40a45f2268f7700ceeceaf) by [skurfuerst](https://gist.github.com/skurfuerst).

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