A very simple JSONSchema to TypeScript types generator
A very simple JSONSchema7/OpenAPI3 to TypeScript types definitions generator
It is intended to support JSONSchema7/OpenAPI3 but may work for some if not most
JSONSchema versions.
This module assumes your JSONSchema / OpenAPI3 documents are valid. It also
doesn’t support external references at the moment (and probably for ever) and
expect a single object whose definitions are all relative to the root object.
It is also meant to be a building block for higher level generators.
import { readFile, writeFile } from 'node:fs/promises';
import {
generateJSONSchemaTypes,
generateOpenAPITypes,
toSource,
} from 'schema2dts';
// Open API
const openAPISchema = JSON.parse(readFileSync('openapi.json').toString());
await writeFile(
'API.d.ts',
toSource(await generateOpenAPITypes(openAPISchema)),
);
// JSON Schema
const jsonSchema = JSON.parse(readFileSync('schema.json').toString());
await writeFile(
'API.d.ts',
toSource(await generateJSONSchemaTypes(jsonSchema)),
);
If you find some cases with unexpected results, please add the fixtures to this
repository in a pull request and describe the problem you encounter.
You can change the API main namespace in order to be able to use several
generated types in the same repository. Just provide its namespace a the second
argument to generateOpenAPITypes
.
The third argument is for options:
generateUnusedSchemas
to true
.filterStatuses
to [200, 201, 202, 300]
for example so that the 500 errorsThere is some differences between the JSONSchema anyOf
, allOf
and oneOf
keywords (learn
more here on combining schemas).
The current way to handle this in this library is to:
oneOf
to ananyOf
to an union type too which is not really what it means in JSONallOf
to anobject
in both schemas explicitly:
{
"allOf": [
{
"$ref": "#/definitions/User"
},
{ "type": "object", "required": ["id"] }
]
}
Currently, the if
/then
keywords of JSONSchema do not work. You should be
able to replace most of its use per a oneOf
form.
TypeScript.NodeArray
Create the TypeScript types declarations from an Open API document
TypeScript.NodeArray
Create the TypeScript types declarations from a JSONSchema document
Returns source from a list of TypeScript statements
TypeScript.NodeArray
Create the TypeScript types declarations from an Open API document
Kind: global function
Param | Type |
---|---|
schema | JSONSchema.Document |
options | Object |
options.baseName | string |
options.filterStatuses | Array. |
options.generateUnusedSchemas | boolean |
options.camelizeInputs | boolean |
options.brandedTypes | Array. |
options.generateRealEnums | boolean |
options.exportNamespaces | boolean |
options.requireCleanAPI | boolean |
TypeScript.NodeArray
Create the TypeScript types declarations from a JSONSchema document
Kind: global function
Param | Type |
---|---|
schema | JSONSchema.Document |
options | Object |
options.name | string |
options.brandedTypes | Array. |
Returns source from a list of TypeScript statements
Kind: global function
Returns: string
Param | Type |
---|---|
nodes | TypedPropertyDescriptor.NodeArray |