2.0.1 • Published 8 months ago

json-schema-to-dts v2.0.1

Weekly downloads
6
License
MIT
Repository
github
Last release
8 months ago

json-schema-to-dts

Convert JSON Schema definitions into accurate (as possible) TypeScript definitions, specifying how the main schema types and lifted sub-schemas should be declared / exported.

Example

Given the schema

{
  "type": "object",
  "properties": {
    "name": { "type": "string", "description": "The name of an object" },
    "not_annotated": { "type": "null" },
    "command": {
      "oneOf": [{ "const": "a constant!" }, { "enum": ["multiple", { "options": "are allowed" }] }]
    }
  }
}

And these options:

const options = {
  topLevel: {
    isExported: true,
  },
};

We get the following result:

type JSONPrimitive = boolean | null | number | string;
type JSONValue =
  | JSONPrimitive
  | JSONValue[]
  | {
      [key: string]: JSONValue;
    };
export type Test = {
  /** The name of an object */
  name?: string;
  not_annotated?: null;
  command?:
    | 'a constant!'
    | (
        | 'multiple'
        | {
            options: 'are allowed';
          }
      );
};

API

new Parser()

Produce a new Parser instance.

.addSchema(uri, schema)

Add a schema to the parser where:

  • uri - is a string representing the schema's uri (ie: file:///path/to/schema.json)
  • schema - is the json object representation of the schema

.compile(options)

Compile all added schemas where:

  • topLevel - options for root schemas
    • hasDeclareKeyword - (optional) mark the type declaration as declare
    • isExported - (optional) export the type declaration
  • lifted - options for sub-schemas that have been lifted during compilation
    • hasDeclareKeyword - (optional) mark the type declaration as declare
    • isExported - (optional) export the type declaration

Returns an object { diagnostics, text } where:

  • diagnostics - is an array of diagnostics
  • text - is the resulting typescript definitions
2.0.1

8 months ago

2.0.0

8 months ago

1.5.0

2 years ago

1.4.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago