From 5ac54b868b32f242b18f8d7c5c649d9046d9f1f1 Mon Sep 17 00:00:00 2001 From: Dariusz Rzepka Date: Sat, 21 Nov 2020 20:18:00 +0100 Subject: [PATCH] Update ts-json-schema-generator Simple type passes! --- README.md | 2 +- generators/ts-json-schema-generator.ts | 46 ++++++++++++++++++-- package.json | 2 +- schemas/quicktype-Simple.json | 4 ++ schemas/ts-json-schema-generator-Simple.json | 33 ++++++++++++++ schemas/typescript-json-schema-Simple.json | 4 ++ src/simple.ts | 12 +++++ yarn.lock | 27 +++++++----- 8 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 schemas/ts-json-schema-generator-Simple.json diff --git a/README.md b/README.md index 6d4223d..4a8faff 100644 --- a/README.md +++ b/README.md @@ -38,5 +38,5 @@ yarn | Library | Command | `Simple` | `ExampleProps` | | ---------------------------------------------------------------------------- | ---------------------------------------- | -------- | -------------- | | [typescript-json-schema](https://github.com/YousefED/typescript-json-schema) | `yarn generate:typescript-json-schema` | ✅ | ❌ | -| [ts-json-schema-generator](https://github.com/vega/ts-json-schema-generator) | `yarn generate:ts-json-schema-generator` | ❌ | ❌ | +| [ts-json-schema-generator](https://github.com/vega/ts-json-schema-generator) | `yarn generate:ts-json-schema-generator` | ✅ | ❌ | | [quicktype](https://github.com/quicktype/quicktype) | `yarn generate:quicktype` | ✅ | ❌ | diff --git a/generators/ts-json-schema-generator.ts b/generators/ts-json-schema-generator.ts index ee69fad..51c6da9 100644 --- a/generators/ts-json-schema-generator.ts +++ b/generators/ts-json-schema-generator.ts @@ -1,7 +1,36 @@ -import * as TSJ from 'ts-json-schema-generator'; +import { + BaseType, + Definition, + FunctionType, + SubTypeFormatter, + createProgram, + createParser, + SchemaGenerator, + createFormatter, +} from 'ts-json-schema-generator'; import { resolve } from 'path'; import { getGenerate, runAsync } from './helpers'; +/** + * Custom type schema formatter for function + */ +class FunctionTypeFormatter implements SubTypeFormatter { + public supportsType(type: FunctionType): boolean { + return type instanceof FunctionType; + } + + public getDefinition(_type: FunctionType): Definition { + // Return a custom schema for the function property. + return { + type: 'string', + }; + } + + public getChildren(_type: FunctionType): BaseType[] { + return []; + } +} + // https://github.com/vega/ts-json-schema-generator const tsJsonSchemaGeneratorGenerate = getGenerate( @@ -13,12 +42,23 @@ const tsJsonSchemaGeneratorGenerate = getGenerate( type: typeName, // Or if you want to generate schema for that one type only }; - return TSJ.createGenerator(config).createSchema(config.type); + // We configure the formatter an add our custom formatter to it. + const formatter = createFormatter(config, fmt => { + fmt.addTypeFormatter(new FunctionTypeFormatter()); + }); + const program = createProgram(config); + const parser = createParser(program, config); + const generator = new SchemaGenerator(program, parser, formatter, config); + + return generator.createSchema(config.type); } ); runAsync(async () => { await tsJsonSchemaGeneratorGenerate(resolve('./src/simple.ts'), 'Simple'); - await tsJsonSchemaGeneratorGenerate(resolve('./src/index.tsx'), 'ExampleProps'); + await tsJsonSchemaGeneratorGenerate( + resolve('./src/index.tsx'), + 'ExampleProps' + ); }); diff --git a/package.json b/package.json index d1fd5d9..7876ba9 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "react": "^17.0.1", "react-dom": "^17.0.1", "size-limit": "^4.6.2", - "ts-json-schema-generator": "^0.77.0", + "ts-json-schema-generator": "^0.78.0", "ts-node": "^9.0.0", "tsdx": "^0.14.1", "tslib": "^2.0.3", diff --git a/schemas/quicktype-Simple.json b/schemas/quicktype-Simple.json index 4905fa4..64a224b 100644 --- a/schemas/quicktype-Simple.json +++ b/schemas/quicktype-Simple.json @@ -5,18 +5,22 @@ "type": "object", "properties": { "name": { + "description": "Example string", "type": "string", "title": "name" }, "count": { + "description": "Example number", "type": "number", "title": "count" }, "optional": { + "description": "Example optional", "type": "number", "title": "optional" }, "callback": { + "description": "Example function type", "type": "object", "title": "callback" } diff --git a/schemas/ts-json-schema-generator-Simple.json b/schemas/ts-json-schema-generator-Simple.json new file mode 100644 index 0000000..c282a7d --- /dev/null +++ b/schemas/ts-json-schema-generator-Simple.json @@ -0,0 +1,33 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/Simple", + "definitions": { + "Simple": { + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "Example string" + }, + "count": { + "type": "number", + "description": "Example number" + }, + "optional": { + "type": "number", + "description": "Example optional" + }, + "callback": { + "type": "string", + "description": "Example function type" + } + }, + "required": [ + "name", + "count", + "callback" + ], + "additionalProperties": false + } + } +} \ No newline at end of file diff --git a/schemas/typescript-json-schema-Simple.json b/schemas/typescript-json-schema-Simple.json index 81059a7..1c6a895 100644 --- a/schemas/typescript-json-schema-Simple.json +++ b/schemas/typescript-json-schema-Simple.json @@ -2,15 +2,19 @@ "type": "object", "properties": { "name": { + "description": "Example string", "type": "string" }, "count": { + "description": "Example number", "type": "number" }, "optional": { + "description": "Example optional", "type": "number" }, "callback": { + "description": "Example function type", "type": "object" } }, diff --git a/src/simple.ts b/src/simple.ts index 9439c5f..8564be6 100644 --- a/src/simple.ts +++ b/src/simple.ts @@ -1,6 +1,18 @@ export type Simple = { + /** + * Example string + */ name: string; + /** + * Example number + */ count: number; + /** + * Example optional + */ optional?: number; + /** + * Example function type + */ callback: () => void; }; diff --git a/yarn.lock b/yarn.lock index 8d1c618..f8b950d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2624,10 +2624,10 @@ commander@^2.18.0, commander@^2.20.0: resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33" integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ== -commander@^6.1.0: - version "6.1.0" - resolved "https://registry.yarnpkg.com/commander/-/commander-6.1.0.tgz#f8d722b78103141006b66f4c7ba1e97315ba75bc" - integrity sha512-wl7PNrYWd2y5mp1OK/LhTlv8Ff4kQJQRXXAvF+uU/TPNiVJUxZLRYGj/B0y/lPGAVcSbJqH2Za/cvHmrPMC8mA== +commander@^6.2.0: + version "6.2.0" + resolved "https://registry.yarnpkg.com/commander/-/commander-6.2.0.tgz#b990bfb8ac030aedc6d11bc04d1488ffef56db75" + integrity sha512-zP4jEKbe8SHzKJYQmq8Y9gYjtO/POJLgIdKgV7B9qNmABVFVc+ctqSX6iXh4mCpJfRBOabiZ2YKPg8ciDw6C+Q== commondir@^1.0.1: version "1.0.1" @@ -8712,17 +8712,17 @@ ts-jest@^25.3.1: semver "6.x" yargs-parser "18.x" -ts-json-schema-generator@^0.77.0: - version "0.77.0" - resolved "https://registry.yarnpkg.com/ts-json-schema-generator/-/ts-json-schema-generator-0.77.0.tgz#6b7f659ecda9a2be05babfef97ce2633364e3357" - integrity sha512-cM+9NfO/jFVEQNLog1XAYmBk9k5fRwTqP7rp1c2IMBREuOjIpiSkgv3/QjoH89UXhh8Ik6Wc8FT0mg7wssazcw== +ts-json-schema-generator@^0.78.0: + version "0.78.0" + resolved "https://registry.yarnpkg.com/ts-json-schema-generator/-/ts-json-schema-generator-0.78.0.tgz#784cb795e6f4eefd1d9ee0ed94c1a154cfd0b084" + integrity sha512-hJ0Z8qLKJ73Trs54O7IZsAj4eoyigL6W3Zm/tQr3YFRHvuyjQAhYvEq1Q2OAi8MO4RQJAOot2XdfL78nuQI0mQ== dependencies: "@types/json-schema" "^7.0.6" - commander "^6.1.0" + commander "^6.2.0" fast-json-stable-stringify "^2.1.0" glob "^7.1.6" json-stable-stringify "^1.0.1" - typescript "~4.0.3" + typescript "~4.0.5" ts-node@^9.0.0: version "9.0.0" @@ -8914,7 +8914,7 @@ typescript@^3.7.3: resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.7.tgz#98d600a5ebdc38f40cb277522f12dc800e9e25fa" integrity sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw== -typescript@^4.0.3, typescript@~4.0.2, typescript@~4.0.3: +typescript@^4.0.3, typescript@~4.0.2: version "4.0.3" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.3.tgz#153bbd468ef07725c1df9c77e8b453f8d36abba5" integrity sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg== @@ -8924,6 +8924,11 @@ typescript@~3.2.1: resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.2.4.tgz#c585cb952912263d915b462726ce244ba510ef3d" integrity sha512-0RNDbSdEokBeEAkgNbxJ+BLwSManFy9TeXz8uW+48j/xhEXv1ePME60olyzw2XzUqUBNAYFeJadIqAgNqIACwg== +typescript@~4.0.5: + version "4.0.5" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389" + integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ== + unicode-canonical-property-names-ecmascript@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz#2619800c4c825800efdd8343af7dd9933cbe2818"