** NOT MAINTAINED -- PLEASE DO NOT USE **
(for more context, see signaux-faibles/opensignauxfaibles#317)
Transform MongoDB-compliant JSON Schema files to TypeScript.
On top of json-schema-to-typescript
, it supports bsonType
values, including date
.
This fork also supports some bsonType
types: Date
or fallback to usual values for type
.
Input:
{
"title": "Example Schema",
"bsonType": "object",
"properties": {
"name": {
"bsonType": "string"
},
"subscriptionDate": {
"bsonType": "date"
},
"age": {
"description": "Age in years",
"bsonType": "integer",
"minimum": 0
},
"hairColor": {
"enum": ["black", "brown", "blue"],
"bsonType": "string"
}
},
"additionalProperties": false,
"required": ["name", "subscriptionDate"]
}
Output:
export interface ExampleSchema {
name: string;
subscriptionDate: Date;
/**
* Age in years
*/
age?: number;
hairColor?: "black" | "brown" | "blue";
}
A CLI utility is provided with this package.
npx mongodb-json-schema-to-typescript foo.json > foo.d.ts
npm test
-
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 likeanyOf
) -
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
tsType
: Overrides the type that's generated from the schema. Useful for forcing a type toany
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).
dependencies
(single, multiple)divisibleBy
(eg)format
(eg)multipleOf
(eg)maximum
(eg)minimum
(eg)maxProperties
(eg)minProperties
(eg)not
/disallow
oneOf
("xor", useanyOf
instead)pattern
(string, regex)uniqueItems
(eg)
Prettier is known to run slowly on really big files. To skip formatting and improve performance, set the format
option to false
.
- Original implementation: https://github.com/bcherny/json-schema-to-typescript
- JSON-schema spec: https://tools.ietf.org/html/draft-zyp-json-schema-04
- 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