Skip to content

Commit

Permalink
Improve CreateRepositoryAttribute error messages (#338)
Browse files Browse the repository at this point in the history
* 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
Magnus-Kuhn and mergify[bot] authored Jan 14, 2025
1 parent 3ff1ff8 commit e5d3324
Show file tree
Hide file tree
Showing 5 changed files with 5,627 additions and 5,338 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class Attributes {
}

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

public isNotSharedAttribute(attributeId: string | CoreId) {
Expand Down
52 changes: 32 additions & 20 deletions packages/runtime/scripts/buildSchemas.mjs
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");
}
Loading

0 comments on commit e5d3324

Please sign in to comment.