rkok / runtyping

Generate runtypes from static types & JSON schema.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

runtyping

Generate runtypes from static types & JSON schema.

Instructions

Install

npm install runtypes
npm install -D runtyping

Use from the command line

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

# This config represents a list of files to create.
- targetFile: src/runtypes.ts # The file to create
  sourceTypes:
    - file: src/types.ts # The file where your type lives
      type: Foo # The type you want to convert to a runtype

    - file: json/my-json-schema.json # You can even use JSON schema files!!
      type: [ExampleType, AnotherExampleType]
  1. Then run: npx runtyping

Use from a script

Basic example:

import { Generator } from 'runtyping'

const generator = new Generator({
  targetFile: 'src/runtypes.ts',
})

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

You can also pass a custom tsconfig file:

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

...or a custom ts-morph project (for the internal compiler):

(see generate.ts for the defaults)

import { Project } from 'ts-morph'

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

Thanks

Inspired by a gist by skurfuerst.

About

Generate runtypes from static types & JSON schema.

License:MIT License


Languages

Language:TypeScript 94.2%Language:JavaScript 5.4%Language:Shell 0.4%