Skip to content

Locrian24/json-schema-to-typescript

 
 

Repository files navigation

json-schema-to-typescript-with-deps Build Status npm mit node

Compile JSON Schema to TypeScript typings.

Note: This is a fork of json-schema-to-typescript whose intention is to support the use dependencies within JSON schemas. Right now the fork supports them when used in combination with oneOf and may not actually support other use-cases currently.

Example

Check out the live demo.

Input:

{
  "title": "Example Schema",
  "type": "object",
  "properties": {
    "occupation": {
      "enum": ["farmer", "other"]
    },
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
    "age": {
      "description": "Age in years",
      "type": "integer",
      "minimum": 0
    },
    "hairColor": {
      "enum": ["black", "brown", "blue"],
      "type": "string"
    }
  },
  "dependencies": {
    "occupation": {
      "oneOf": [
        {
          "properties": {
            "occupation": {
              "enum": ["farmer"]
            }
          },
          {
            "required": ["jobDescription"],
            "properties": {
              "occupation": {
                "enum": ["other"]
              },
              "jobDescription": {
                "type": "string"
              }
            }
          }
        }
      ]
    }
  },
  "additionalProperties": false,
  "required": ["firstName", "lastName", "occupation"]
}

Output:

export type ExampleSchema = ExampleSchema1 & {
  firstName: string;
  lastName: string;
  occupation: "farmer" | "other"
  /**
   * Age in years
   */
  age?: number;
  hairColor?: "black" | "brown" | "blue";
}

export type ExampleSchema1 = 
  | {
      occupation?: "farmer"
    }
  | {
      occupation?: "other",
      jobDescription: string
    }

Installation

npm install json-schema-to-typescript-with-deps

Usage

json-schema-to-typescript is easy to use via the CLI, or programmatically.

CLI

First make the CLI available using one of the following options:

# install locally, then use `npx json2ts`
npm install json-schema-to-typescript-with-deps

# or install globally, then use `json2ts`
npm install json-schema-to-typescript-with-deps --global

# or install to npm cache, then use `npx --package=json-schema-to-typescript-with-deps json2ts`
# (you don't need to run an install command first)

Then, use the CLI to convert JSON files to TypeScript typings:

cat foo.json | json2ts > foo.d.ts
# or
json2ts foo.json > foo.d.ts
# or
json2ts foo.yaml foo.d.ts
# or
json2ts --input foo.json --output foo.d.ts
# or
json2ts -i foo.json -o foo.d.ts
# or (quote globs so that your shell doesn't expand them)
json2ts -i 'schemas/**/*.json'
# or
json2ts -i schemas/ -o types/

You can pass any of the options described below (including style options) as CLI flags. Boolean values can be set to false using the no- prefix.

# generate code for definitions that aren't referenced
json2ts -i foo.json -o foo.d.ts --unreachableDefinitions
# use single quotes and disable trailing semicolons
json2ts -i foo.json -o foo.d.ts --style.singleQuote --no-style.semi

API

See the original repo for more usage and API examples.

About

Compile JSON Schema to TypeScript type declarations

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 99.9%
  • JavaScript 0.1%