|
1 | | -const fs = require("fs"); |
2 | | -const tsj = require("ts-json-schema-generator"); |
| 1 | +import fs from 'fs'; |
| 2 | +import { |
| 3 | + createFormatter, |
| 4 | + createParser, |
| 5 | + createProgram, MutableTypeFormatter, |
| 6 | + SchemaGenerator |
| 7 | +} from 'ts-json-schema-generator'; |
| 8 | +import { |
| 9 | + BaseType, |
| 10 | + Definition, |
| 11 | + FunctionType, |
| 12 | + SubTypeFormatter |
| 13 | +} from 'ts-json-schema-generator'; |
| 14 | + |
| 15 | +class CustomTypeFormatter implements SubTypeFormatter { |
| 16 | + public supportsType(type: FunctionType): boolean { |
| 17 | + return type instanceof FunctionType; |
| 18 | + } |
| 19 | + |
| 20 | + public getDefinition(): Definition { |
| 21 | + // Return a custom schema for the function property. |
| 22 | + return { |
| 23 | + type: "object", |
| 24 | + properties: { |
| 25 | + isFunction: { |
| 26 | + type: "boolean", |
| 27 | + const: true, |
| 28 | + }, |
| 29 | + }, |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + public getChildren(): BaseType[] { |
| 34 | + return []; |
| 35 | + } |
| 36 | +} |
3 | 37 |
|
4 | 38 | function writeSchema(config) { |
5 | | - const schema = tsj.createGenerator(config).createSchema(config.type); |
6 | | - const schemaString = JSON.stringify(schema, null, 2); |
| 39 | + const formatter = createFormatter(config, (fmt: MutableTypeFormatter) => { |
| 40 | + fmt.addTypeFormatter(new CustomTypeFormatter()); |
| 41 | + }); |
| 42 | + |
| 43 | + const program = createProgram(config); |
| 44 | + const schema = new SchemaGenerator(program, createParser(program, config), formatter, config).createSchema(config.type); |
| 45 | + |
| 46 | + let schemaString = JSON.stringify(schema, null, 2); |
| 47 | + schemaString = correctSchema(schemaString) |
| 48 | + |
7 | 49 | fs.writeFile(config.outputPath, `export const ${config.outputConstName} = ${schemaString};`, (err) => { |
8 | 50 | if (err) throw err; |
9 | 51 | }); |
10 | 52 | } |
11 | 53 |
|
| 54 | +function correctSchema(schemaString: string) { |
| 55 | + return schemaString.replace( |
| 56 | + "\"SuppliedSignature\": {\n" + |
| 57 | + " \"type\": \"object\",\n" + |
| 58 | + " \"properties\": {\n" + |
| 59 | + " \"signature\": {\n" + |
| 60 | + " \"type\": \"object\",\n" + |
| 61 | + " \"properties\": {\n" + |
| 62 | + " \"isFunction\": {\n" + |
| 63 | + " \"type\": \"boolean\",\n" + |
| 64 | + " \"const\": true\n" + |
| 65 | + " }\n" + |
| 66 | + " }\n" + |
| 67 | + " },\n" + |
| 68 | + " \"did\": {\n" + |
| 69 | + " \"type\": \"string\"\n" + |
| 70 | + " },\n" + |
| 71 | + " \"kid\": {\n" + |
| 72 | + " \"type\": \"string\"\n" + |
| 73 | + " }\n" + |
| 74 | + " },\n" + |
| 75 | + " \"required\": [\n" + |
| 76 | + " \"signature\",\n" + |
| 77 | + " \"did\",\n" + |
| 78 | + " \"kid\"\n" + |
| 79 | + " ],\n" + |
| 80 | + " \"additionalProperties\": false\n" + |
| 81 | + " },", |
| 82 | + "\"SuppliedSignature\": {\n" + |
| 83 | + " \"type\": \"object\",\n" + |
| 84 | + " \"properties\": {\n" + |
| 85 | + " \"did\": {\n" + |
| 86 | + " \"type\": \"string\"\n" + |
| 87 | + " },\n" + |
| 88 | + " \"kid\": {\n" + |
| 89 | + " \"type\": \"string\"\n" + |
| 90 | + " }\n" + |
| 91 | + " },\n" + |
| 92 | + " \"required\": [\n" + |
| 93 | + " \"did\",\n" + |
| 94 | + " \"kid\"\n" + |
| 95 | + " ],\n" + |
| 96 | + " \"additionalProperties\": true\n" + |
| 97 | + " },") |
| 98 | +} |
| 99 | + |
12 | 100 | const requestOptsConf = { |
13 | 101 | path: "../src/main/types/SIOP.types.ts", |
14 | 102 | tsconfig: "tsconfig.json", |
|
0 commit comments