-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Map @defaulValue back to @default in generation #609
Comments
Can you send a pull request? |
This is not in the TSDoc RFC yet and is only part of |
to anyone else struggling to make TSDoc's Here's an example script#!/usr/bin/env node
import { writeFileSync } from "node:fs";
import { join } from "node:path";
import { createGenerator } from "ts-json-schema-generator";
const __dirname = new URL(".", import.meta.url).pathname;
const packageRoot = join(__dirname, "..", "src");
/**
* post-process the schema recursively to:
* 1. replace any key named `defaultValue` with `default`
* 1. remove any backticks from the value
* 1. attempt to parsing the value as JSON (falling back, if not)
*/
const postProcess = <T>(item: T): T => {
if (typeof item !== "object" || item === null) {
return item;
}
if (Array.isArray(item)) {
return item.map(postProcess) as unknown as T;
}
return Object.fromEntries(
Object.entries(item).map(([key, value]) => {
if (key === "defaultValue" && typeof value === "string") {
const replaced = value.replaceAll(/`/g, "");
try {
return ["default", JSON.parse(replaced)];
} catch (e) {
return ["default", replaced];
}
}
return [key, postProcess(value)];
})
) as T;
};
const create = (fileName: string, typeName: string) => {
const generator = createGenerator({
path: join(packageRoot, "index.ts"),
tsconfig: join(__dirname, "../tsconfig.json"),
type: "Schema",
extraTags: ["defaultValue"],
});
const schema = postProcess(generator.createSchema(typeName));
const filePath = join(__dirname, "..", "schemas", fileName);
writeFileSync(filePath, JSON.stringify(schema, null, 2));
};
create("schema.v1.json", "SchemaV1");
create("schema.v2.json", "Schema");
create("schema.json", "Schema"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TSDoc uses/recommends
@defaultValue
instead of@default
https://tsdoc.org/pages/tags/defaultvalue/It would be good to map it back to the more common
default
key on generationThe text was updated successfully, but these errors were encountered: