-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve CreateRepositoryAttribute error messages (#338)
* fix: don't check create repository attribute content * test: illustration of the test result * test: add more tests * test: remove unnecessary tests, add more * feat: use separate tsconfig * feat: separate schemas for attribute values * refactor: no copying of types * refactor: rename file * test/feat: error messages * refactor: error message, string working * refactor: string template * refactor: naming * feat: include type in error message * fix: only generate schemas for identity attributes * refactor: variable naming * feat: remove JSON in attribute schemas * refactor/fix: variable naming, remove JSON * refactor: naming * refactor: remove redundant tsconfig file * refactor: move validation interface --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
3ff1ff8
commit e5d3324
Showing
5 changed files
with
5,627 additions
and
5,338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,44 @@ | ||
import * as content from "@nmshd/content"; | ||
import fs from "fs"; | ||
import * as tsj from "ts-json-schema-generator"; | ||
|
||
const config = { | ||
const runtimeConfig = { | ||
tsconfig: new URL("../tsconfig.json", import.meta.url).pathname, | ||
type: "*", | ||
extraTags: ["errorMessage"] | ||
}; | ||
// use SchemaValidatableCreateRepositoryAttributeRequest instead of CreateRepositoryAttributeRequest | ||
const useCaseSchemaDeclarations = getSchemaDeclarations(runtimeConfig, (x) => x.endsWith("Request") && x !== "CreateRepositoryAttributeRequest"); | ||
const cleanUseCaseSchemaDeclarations = useCaseSchemaDeclarations.replaceAll("SchemaValidatable", ""); | ||
|
||
const schemaGenerator = tsj.createGenerator(config); | ||
|
||
const requestTypes = schemaGenerator | ||
.getRootNodes() | ||
.map((x) => x.symbol.escapedName) | ||
.filter((x) => x.endsWith("Request")); | ||
|
||
const schemaDeclarations = requestTypes | ||
.map((type) => { | ||
try { | ||
const schema = schemaGenerator.createSchema(type); | ||
return `export const ${type}: any = ${JSON.stringify(schema, undefined, 4)}`; | ||
} catch (e) { | ||
if (!(e instanceof tsj.NoRootTypeError)) throw e; | ||
} | ||
}) | ||
.filter((s) => s) | ||
.join("\n\n"); | ||
const contentConfig = { | ||
...runtimeConfig, | ||
tsconfig: new URL("../../content/tsconfig.json", import.meta.url).pathname | ||
}; | ||
const attributeValues = content.AttributeValues.Identity.TYPE_NAMES.map((x) => `${x}JSON`); | ||
const attributeSchemaDeclarations = getSchemaDeclarations(contentConfig, (x) => attributeValues.includes(x)); | ||
const cleanAttributeSchemaDeclarations = attributeSchemaDeclarations.replaceAll("JSON", ""); | ||
|
||
const outputPath = new URL("../src/useCases/common/Schemas.ts", import.meta.url).pathname; | ||
fs.writeFile(outputPath, schemaDeclarations, (err) => { | ||
fs.writeFile(outputPath, `${cleanUseCaseSchemaDeclarations}\n\n${cleanAttributeSchemaDeclarations}`, (err) => { | ||
if (err) throw err; | ||
}); | ||
|
||
function getSchemaDeclarations(tsconfig, filter) { | ||
const schemaGenerator = tsj.createGenerator(tsconfig); | ||
const types = schemaGenerator | ||
.getRootNodes() | ||
.map((x) => x.symbol.escapedName) | ||
.filter(filter); | ||
return types | ||
.map((type) => { | ||
try { | ||
const schema = schemaGenerator.createSchema(type); | ||
return `export const ${type}: any = ${JSON.stringify(schema, undefined, 4)}`; | ||
} catch (e) { | ||
if (!(e instanceof tsj.NoRootTypeError)) throw e; | ||
} | ||
}) | ||
.filter((s) => s) | ||
.join("\n\n"); | ||
} |
Oops, something went wrong.