Skip to content

Commit

Permalink
Update ts-json-schema-generator
Browse files Browse the repository at this point in the history
Simple type passes!
  • Loading branch information
darkowic committed Nov 21, 2020
1 parent 6d9a264 commit 5ac54b8
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |||
46 changes: 43 additions & 3 deletions generators/ts-json-schema-generator.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -13,12 +42,23 @@ const tsJsonSchemaGeneratorGenerate = getGenerate(
type: typeName, // Or <type-name> 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'
);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions schemas/quicktype-Simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
33 changes: 33 additions & 0 deletions schemas/ts-json-schema-generator-Simple.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
4 changes: 4 additions & 0 deletions schemas/typescript-json-schema-Simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
Expand Down
12 changes: 12 additions & 0 deletions src/simple.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
export type Simple = {
/**
* Example string
*/
name: string;
/**
* Example number
*/
count: number;
/**
* Example optional
*/
optional?: number;
/**
* Example function type
*/
callback: () => void;
};
27 changes: 16 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand All @@ -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"
Expand Down

0 comments on commit 5ac54b8

Please sign in to comment.