JSPM

  • Created
  • Published
  • Downloads 1035478
  • Score
    100M100P100Q243161F
  • License MIT

compile json schema to typescript typings

Package Exports

  • json-schema-to-typescript

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-schema-to-typescript) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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

  • title => interface
  • Primitive types:
    • array
    • array of type
    • boolean
    • integer
    • number
    • null
    • object
    • string
    • enum
  • Non/extensible interfaces
  • Custom JSON-schema extensions
  • Nested properties
  • Schema definitions
  • Schema references
  • External (network) schema references
  • Add support for running in browser
  • default interface name
  • infer unnamed interface name from filename
  • anyOf ("union")
  • allOf ("intersection")
  • additionalProperties of type
  • extends
  • required properties on objects (eg)
  • validateRequired (eg)
  • 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