|
| 1 | +import * as content from "@nmshd/content"; |
1 | 2 | import fs from "fs";
|
2 | 3 | import * as tsj from "ts-json-schema-generator";
|
3 | 4 |
|
4 |
| -const config = { |
| 5 | +const runtimeConfig = { |
5 | 6 | tsconfig: new URL("../tsconfig.json", import.meta.url).pathname,
|
6 | 7 | type: "*",
|
7 | 8 | extraTags: ["errorMessage"]
|
8 | 9 | };
|
| 10 | +// use SchemaValidatableCreateRepositoryAttributeRequest instead of CreateRepositoryAttributeRequest |
| 11 | +const useCaseSchemaDeclarations = getSchemaDeclarations(runtimeConfig, (x) => x.endsWith("Request") && x !== "CreateRepositoryAttributeRequest"); |
| 12 | +const cleanUseCaseSchemaDeclarations = useCaseSchemaDeclarations.replaceAll("SchemaValidatable", ""); |
9 | 13 |
|
10 |
| -const schemaGenerator = tsj.createGenerator(config); |
11 |
| - |
12 |
| -const requestTypes = schemaGenerator |
13 |
| - .getRootNodes() |
14 |
| - .map((x) => x.symbol.escapedName) |
15 |
| - .filter((x) => x.endsWith("Request")); |
16 |
| - |
17 |
| -const schemaDeclarations = requestTypes |
18 |
| - .map((type) => { |
19 |
| - try { |
20 |
| - const schema = schemaGenerator.createSchema(type); |
21 |
| - return `export const ${type}: any = ${JSON.stringify(schema, undefined, 4)}`; |
22 |
| - } catch (e) { |
23 |
| - if (!(e instanceof tsj.NoRootTypeError)) throw e; |
24 |
| - } |
25 |
| - }) |
26 |
| - .filter((s) => s) |
27 |
| - .join("\n\n"); |
| 14 | +const contentConfig = { |
| 15 | + ...runtimeConfig, |
| 16 | + tsconfig: new URL("../../content/tsconfig.json", import.meta.url).pathname |
| 17 | +}; |
| 18 | +const attributeValues = content.AttributeValues.Identity.TYPE_NAMES.map((x) => `${x}JSON`); |
| 19 | +const attributeSchemaDeclarations = getSchemaDeclarations(contentConfig, (x) => attributeValues.includes(x)); |
| 20 | +const cleanAttributeSchemaDeclarations = attributeSchemaDeclarations.replaceAll("JSON", ""); |
28 | 21 |
|
29 | 22 | const outputPath = new URL("../src/useCases/common/Schemas.ts", import.meta.url).pathname;
|
30 |
| -fs.writeFile(outputPath, schemaDeclarations, (err) => { |
| 23 | +fs.writeFile(outputPath, `${cleanUseCaseSchemaDeclarations}\n\n${cleanAttributeSchemaDeclarations}`, (err) => { |
31 | 24 | if (err) throw err;
|
32 | 25 | });
|
| 26 | + |
| 27 | +function getSchemaDeclarations(tsconfig, filter) { |
| 28 | + const schemaGenerator = tsj.createGenerator(tsconfig); |
| 29 | + const types = schemaGenerator |
| 30 | + .getRootNodes() |
| 31 | + .map((x) => x.symbol.escapedName) |
| 32 | + .filter(filter); |
| 33 | + return types |
| 34 | + .map((type) => { |
| 35 | + try { |
| 36 | + const schema = schemaGenerator.createSchema(type); |
| 37 | + return `export const ${type}: any = ${JSON.stringify(schema, undefined, 4)}`; |
| 38 | + } catch (e) { |
| 39 | + if (!(e instanceof tsj.NoRootTypeError)) throw e; |
| 40 | + } |
| 41 | + }) |
| 42 | + .filter((s) => s) |
| 43 | + .join("\n\n"); |
| 44 | +} |
0 commit comments