Skip to content

Commit e5d3324

Browse files
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>
1 parent 3ff1ff8 commit e5d3324

File tree

5 files changed

+5627
-5338
lines changed

5 files changed

+5627
-5338
lines changed

packages/consumption/src/consumption/ConsumptionCoreErrors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class Attributes {
237237
}
238238

239239
public isNotRepositoryAttribute(attributeId: string | CoreId) {
240-
return new CoreError("error.consumption.attributes.isNotRepositoryAttribute", `The attribute (id: ${attributeId}) is not a RepositoryAttribute.`);
240+
return new CoreError("error.consumption.attributes.isNotRepositoryAttribute", `The Attribute (id: ${attributeId}) is not a RepositoryAttribute.`);
241241
}
242242

243243
public isNotSharedAttribute(attributeId: string | CoreId) {
Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
1+
import * as content from "@nmshd/content";
12
import fs from "fs";
23
import * as tsj from "ts-json-schema-generator";
34

4-
const config = {
5+
const runtimeConfig = {
56
tsconfig: new URL("../tsconfig.json", import.meta.url).pathname,
67
type: "*",
78
extraTags: ["errorMessage"]
89
};
10+
// use SchemaValidatableCreateRepositoryAttributeRequest instead of CreateRepositoryAttributeRequest
11+
const useCaseSchemaDeclarations = getSchemaDeclarations(runtimeConfig, (x) => x.endsWith("Request") && x !== "CreateRepositoryAttributeRequest");
12+
const cleanUseCaseSchemaDeclarations = useCaseSchemaDeclarations.replaceAll("SchemaValidatable", "");
913

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", "");
2821

2922
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) => {
3124
if (err) throw err;
3225
});
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

Comments
 (0)