MCPcopy
hub / github.com/bcherny/json-schema-to-typescript

github.com/bcherny/json-schema-to-typescript @v1.2.2 sqlite

repository ↗ · DeepWiki ↗ · release v1.2.2 ↗
76 symbols 119 edges 18 files 0 documented · 0%
README

json-schema-to-typescript

[In Alpha]: Bug reports appreciated!

Circle CI

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
    }
  },
  "required": ["firstName", "lastName"]
}

Output:

interface ExampleSchema {
  firstName: string;
  lastName: string;
  age?: number; // Age in years
}

Installation

npm install json-schema-to-typescript

Usage

import {compileFromFile} from 'json-schema-to-typescript'
fs.writeFileSync('foo.d.ts', await compileFromFile('foo.json'))

Tests

npm test

Todo

  • [x] title => interface
  • [x] Primitive types:
  • [x] array
  • [x] array of type
  • [x] boolean
  • [x] integer
  • [x] number
  • [x] null
  • [x] object
  • [x] string
  • [x] enum
  • [x] Non/extensible interfaces
  • [ ] Custom JSON-schema extensions
  • [x] Nested properties
  • [x] Schema definitions
  • [x] Schema references
  • [ ] External (network) schema references
  • [ ] Add support for running in browser
  • [x] default interface name
  • [ ] infer unnamed interface name from filename
  • [x] anyOf ("union")
  • [x] allOf ("intersection")
  • [x] additionalProperties of type
  • [ ] extends
  • [x] required properties on objects (eg)
  • [ ] validateRequired (eg)
  • [x] literal objects in enum (eg)
  • [ ] referencing schema by id (eg)
  • [ ] clean up + refactor code

Not expressible in TypeScript:

  • dependencies (single, multiple)
  • divisibleBy (eg)
  • format (eg)
  • multipleOf (eg)
  • maximum (eg)
  • minimum (eg)
  • maxItems (eg)
  • minItems (eg)
  • maxProperties (eg)
  • minProperties (eg)
  • not/disallow
  • oneOf ("xor", use anyOf instead)
  • pattern (string, regex)
  • patternProperties (eg)
  • uniqueItems (eg)

Further Reading

  • JSON-schema spec: http://json-schema.org/latest/json-schema-core.html
  • JSON-schema wiki: https://github.com/json-schema/json-schema/wiki
  • JSON-schema test suite: https://github.com/json-schema/JSON-Schema-Test-Suite/blob/node
  • TypeScript spec: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md

Extension points exported contracts — how you extend this code

TsTypeSettings (Interface)
(no doc)
src/TsTypes.ts
TsProp (Interface)
(no doc)
src/TsTypes.ts

Core symbols most depended-on inside this repo

toTsType
called by 7
src/index.ts
exec
called by 6
gulpfile.js
toString
called by 3
src/index.ts
declareType
called by 3
src/index.ts
compile
called by 3
src/index.ts
_type
called by 3
src/TsTypes.ts
toSafeType
called by 3
src/TsTypes.ts
_type
called by 2
src/TsTypes.ts

Shape

Method 33
Class 25
Function 15
Interface 2
Enum 1

Languages

TypeScript100%

Modules by API surface

src/TsTypes.ts54 symbols
src/index.ts17 symbols
src/pretty-printer.ts4 symbols
gulpfile.js1 symbols

Dependencies from manifests, versioned

@types/chai3.4.29 · 1×
@types/lodash0.0.28 · 1×
@types/mocha2.2.28 · 1×
@types/node4.0.30 · 1×
browserify13.1.0 · 1×
chai3.5.0 · 1×
dts-bundle0.5.0 · 1×
gulp3.9.1 · 1×
gulp-tslint6.1.1 · 1×
lodash4.6.1 · 1×
mocha2.5.3 · 1×
tslint3.15.1 · 1×

For agents

$ claude mcp add json-schema-to-typescript \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact