Skip to content

Commit ea65932

Browse files
ochafikclaude
andcommitted
feat: add .describe() to top-level schemas
Add addTopLevelDescribe() post-processing transform to add .describe() calls to top-level schema declarations based on their @description JSDoc tags. ts-to-zod only adds .describe() to properties, not to the schema itself, so this transform fills that gap. - 109 top-level schemas now have .describe() - Total .describe() calls: 335 (up from 226) Example: export const RequestParamsSchema = z.object({...}) .describe('Common params for any request.'); 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent a06d2ff commit ea65932

File tree

2 files changed

+1245
-1059
lines changed

2 files changed

+1245
-1059
lines changed

scripts/generate-schemas.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ const AST_TRANSFORMS: Transform[] = [
438438
applyFieldOverrides,
439439
addStrictToSchemas,
440440
convertToDiscriminatedUnion,
441+
addTopLevelDescribe,
441442
];
442443

443444
/**
@@ -692,6 +693,54 @@ function convertToDiscriminatedUnion(sourceFile: SourceFile): void {
692693
}
693694
}
694695

696+
/**
697+
* Add .describe() to top-level schemas based on their JSDoc @description tag.
698+
* ts-to-zod only adds .describe() to properties, not to the schema itself.
699+
*/
700+
function addTopLevelDescribe(sourceFile: SourceFile): void {
701+
let count = 0;
702+
703+
for (const varStmt of sourceFile.getVariableStatements()) {
704+
// Get JSDoc from the variable statement
705+
const jsDocs = varStmt.getJsDocs();
706+
if (jsDocs.length === 0) continue;
707+
708+
const jsDoc = jsDocs[0];
709+
const descTag = jsDoc.getTags().find(tag => tag.getTagName() === 'description');
710+
if (!descTag) continue;
711+
712+
// Get the description text
713+
const descText = descTag.getCommentText()?.trim();
714+
if (!descText) continue;
715+
716+
// Get the variable declaration
717+
const decl = varStmt.getDeclarations()[0];
718+
if (!decl) continue;
719+
720+
const schemaName = decl.getName();
721+
if (!schemaName.endsWith('Schema')) continue;
722+
723+
const initializer = decl.getInitializer();
724+
if (!initializer) continue;
725+
726+
const currentText = initializer.getText();
727+
728+
// Skip if already has .describe() at the end
729+
if (/\.describe\([^)]+\)\s*$/.test(currentText)) continue;
730+
731+
// Escape quotes in description
732+
const escapedDesc = descText.replace(/'/g, "\\'").replace(/\n/g, ' ');
733+
734+
// Add .describe() to the schema
735+
decl.setInitializer(`${currentText}.describe('${escapedDesc}')`);
736+
count++;
737+
}
738+
739+
if (count > 0) {
740+
console.log(` ✓ Added .describe() to ${count} top-level schemas`);
741+
}
742+
}
743+
695744
// =============================================================================
696745
// Main
697746
// =============================================================================

0 commit comments

Comments
 (0)