All Projects → bcherny → Json Schema To Typescript

bcherny / Json Schema To Typescript

Compile JSONSchema to TypeScript type declarations

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Json Schema To Typescript

Jesse
jesse (JSon Schema Erlang) is an implementation of a JSON Schema validator for Erlang.
Stars: ✭ 92 (-95.2%)
Mutual labels:  json-schema
Go Jsonschema Generator
json-schemas generator based on Go types
Stars: ✭ 110 (-94.26%)
Mutual labels:  json-schema
Typedload
Python library to load dynamically typed data into statically typed data structures
Stars: ✭ 120 (-93.74%)
Mutual labels:  json-schema
Ajv I18n
Internationalised error messages for Ajv JSON-Schema validator
Stars: ✭ 98 (-94.89%)
Mutual labels:  json-schema
Beerjson
Development repository for the BeerJSON format standard
Stars: ✭ 109 (-94.31%)
Mutual labels:  json-schema
Liform
PHP library to render Symfony Forms to JSON Schema
Stars: ✭ 113 (-94.11%)
Mutual labels:  json-schema
Understanding Json Schema
A website aiming to provide more accessible documentation for JSON schema.
Stars: ✭ 1,268 (-33.85%)
Mutual labels:  json-schema
Xsd2json
Translate XML Schema into equivalent JSON Schema
Stars: ✭ 124 (-93.53%)
Mutual labels:  json-schema
Schema Registry
Confluent Schema Registry for Kafka
Stars: ✭ 1,647 (-14.08%)
Mutual labels:  json-schema
Class Validator Jsonschema
Convert class-validator-decorated classes into JSON schema
Stars: ✭ 118 (-93.84%)
Mutual labels:  json-schema
Laravel Json Schema
Create all your migrations and models from one JSON schema file.
Stars: ✭ 101 (-94.73%)
Mutual labels:  json-schema
React Jsonschema Form
A React component for building Web forms from JSON Schema.
Stars: ✭ 10,870 (+467.03%)
Mutual labels:  json-schema
Hypothesis Jsonschema
Tools to generate test data from JSON schemata with Hypothesis
Stars: ✭ 112 (-94.16%)
Mutual labels:  json-schema
React Jsonschema Form Material Ui
📜 React - Material UI components for building Web forms from JSON Schema.
Stars: ✭ 95 (-95.04%)
Mutual labels:  json-schema
Lurker
📖 The ultimate tool for documenting and testing APIs in Rails
Stars: ✭ 120 (-93.74%)
Mutual labels:  json-schema
Graphql To Json Schema
GraphQL Schema to JSON Schema
Stars: ✭ 87 (-95.46%)
Mutual labels:  json-schema
Jsonschema
JSON Schema validation
Stars: ✭ 1,575 (-17.84%)
Mutual labels:  json-schema
React Json Schema
Configure and build views using JSON schemas mapped to React components
Stars: ✭ 131 (-93.17%)
Mutual labels:  json-schema
Liformbundle
Symfony Bundle to render Symfony Forms to JSON Schema
Stars: ✭ 124 (-93.53%)
Mutual labels:  json-schema
Npoint
JSON storage bins with schema validation
Stars: ✭ 116 (-93.95%)
Mutual labels:  json-schema

json-schema-to-typescript Build Status npm mit

Compile json schema to typescript typings

Example

Input:

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

Output:

export interface ExampleSchema {
  firstName: string;
  lastName: string;
  /**
   * Age in years
   */
  age?: number;
  hairColor?: "black" | "brown" | "blue";
}

Installation

# Using Yarn:
yarn add json-schema-to-typescript

# Or, using NPM:
npm install json-schema-to-typescript --save

Usage

import { compile, compileFromFile } from 'json-schema-to-typescript'

// compile from file
compileFromFile('foo.json')
  .then(ts => fs.writeFileSync('foo.d.ts', ts))

// or, compile a JS object
let mySchema = {
  properties: [...]
}
compile(mySchema, 'MySchema')
  .then(ts => ...)

See server demo and browser demo for full examples.

Options

compileFromFile and compile accept options as their last argument (all keys are optional):

key type default description
bannerComment string "/* tslint:disable */\n/**\n* This file was automatically generated by json-schema-to-typescript.\n* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,\n* and run json-schema-to-typescript to regenerate this file.\n*/" Disclaimer comment prepended to the top of each generated file
cwd string process.cwd() Root directory for resolving $refs
declareExternallyReferenced boolean true Declare external schemas referenced via $ref?
enableConstEnums boolean true Prepend enums with const?
format boolean true Format code? Set this to false to improve performance.
ignoreMinAndMaxItems boolean false Ignore maxItems and minItems for array types, preventing tuples being generated.
style object { bracketSpacing: false, printWidth: 120, semi: true, singleQuote: false, tabWidth: 2, trailingComma: 'none', useTabs: false } A Prettier configuration
unknownAny boolean true Use unknown instead of any where possible
unreachableDefinitions boolean false Generates code for definitions that aren't referenced by the schema.
strictIndexSignatures boolean false Append all index signatures with | undefined so that they are strictly typed.
$refOptions object {} $RefParser Options, used when resolving $refs

CLI

A CLI utility is provided with this package.

cat foo.json | json2ts > foo.d.ts
# or
json2ts foo.json > foo.d.ts
# or
json2ts foo.json 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 above (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

Tests

npm test

Features

  • title => interface
  • Primitive types:
    • array
    • homogeneous array
    • boolean
    • integer
    • number
    • null
    • object
    • string
    • homogeneous enum
    • heterogeneous enum
  • Non/extensible interfaces
  • Custom JSON-schema extensions
  • Nested properties
  • Schema definitions
  • Schema references
  • Local (filesystem) schema references
  • External (network) schema references
  • Add support for running in browser
  • default interface name
  • infer unnamed interface name from filename
  • allOf ("intersection")
  • anyOf ("union")
  • oneOf (treated like anyOf)
  • maxItems (eg)
  • minItems (eg)
  • additionalProperties of type
  • patternProperties (partial support)
  • extends
  • required properties on objects (eg)
  • validateRequired (eg)
  • literal objects in enum (eg)
  • referencing schema by id (eg)
  • custom typescript types via tsType

Custom schema properties:

  • tsType: Overrides the type that's generated from the schema. Useful for forcing a type to any or when using non-standard JSON schema extensions (eg).
  • tsEnumNames: Overrides the names used for the elements in an enum. Can also be used to create string enums (eg).

Not expressible in TypeScript:

FAQ

JSON-Schema-to-TypeScript is crashing on my giant file. What can I do?

Prettier is known to run slowly on really big files. To skip formatting and improve performance, set the format option to false.

Further Reading

Who uses JSON-Schema-to-TypeScript?

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].