in src/schemas.ts [88:173]
export function dumpSchemasToString<T extends ObjectMap>(
schemas: T,
resourceMap: ResourceMap,
outputPath: string,
): StringArray {
/* Refactor to allow a test that doesn't write to disk */
const resources = Array.from(resourceMap.values());
const authors = extractReferences(resources, "author").sort();
const topics = extractReferences(resources, "topic").sort();
const pathsAndSchemas: StringArray = {};
for (const [key, schema] of Object.entries(schemas)) {
const thisSchema: Schema<T> = {
...getPreamble(key),
...{ properties: {} },
// ...{ allOf: [] },
...JSON.parse(JSON.stringify(schema)),
};
const resourceTypeName = key.toLowerCase();
const propertiesToTraverse = featureFlags.traverseAllOfProperties
? determinePropertiesToTraverse(thisSchema)
: [thisSchema.properties];
for (const properties of propertiesToTraverse) {
// Remove resourceType
if ("resourceType" in properties) {
delete properties["resourceType"];
}
if ("required" in thisSchema) {
thisSchema.required = thisSchema.required.filter(
(it: any) => it !== "resourceType",
);
}
if ("allOf" in thisSchema) {
for (const allOf of thisSchema.allOf as Array<any>) {
if ("required" in allOf) {
allOf.required = allOf.required.filter(
(it: any) => it !== "resourceType",
);
}
}
}
// Rewrite author to an enum
if (
featureFlags.rewriteReferenceProperties &&
hasProperty(properties, "author")
) {
properties["author"] = {
enum: authors,
description: properties["author"]["description"],
};
}
// Rewrite topics open array to an array of enum
if (
featureFlags.rewriteReferenceProperties &&
hasProperty(properties, "topics")
) {
properties["topics"]["items"] = { enum: topics };
if ("type" in properties["topics"]["items"]) {
delete properties["topics"]["items"]["type"];
}
}
// Rewrite date to be a string with date format
if (
featureFlags.rewriteDateProperties &&
hasProperty(properties, "date")
) {
properties["date"]["type"] = "string";
properties["date"]["format"] = "date";
if ("instanceOf" in properties["date"]) {
delete properties["date"]["instanceOf"];
}
}
}
const thisPath = path.join(outputPath, `${resourceTypeName}.schema.json`);
pathsAndSchemas[thisPath] = JSON.stringify(thisSchema, null, 2);
}
return pathsAndSchemas;
}