@@ -151,6 +151,15 @@ function removeComments(obj, isRoot = false) {
151151 return newObj ;
152152}
153153
154+ function stripTopLevelKeys ( obj , keysToRemove = [ ] ) {
155+ if ( ! isObject ( obj ) ) return obj ;
156+ const clone = { ...obj } ;
157+ for ( const k of keysToRemove ) {
158+ if ( k in clone ) delete clone [ k ] ;
159+ }
160+ return clone ;
161+ }
162+
154163async function bundleSchemas ( modelsDirectory , rootSchemaPath , options = { } ) {
155164 try {
156165 const absoluteModelsDir = path . resolve ( modelsDirectory ) ;
@@ -265,11 +274,18 @@ async function bundleSchemas(modelsDirectory, rootSchemaPath, options = {}) {
265274 // Get the rewritten root schema
266275 const rootSchemaRewritten = rewrittenDefinitions [ rootSchemaName ] ;
267276
277+ // Strip top-level metadata keys from each embedded definition ($defs)
278+ const keysToStripFromDefs = [ '$schema' , '$id' , '$comment' ] ;
279+ const cleanedDefinitions = { } ;
280+ for ( const [ defName , defSchema ] of Object . entries ( rewrittenDefinitions ) ) {
281+ cleanedDefinitions [ defName ] = stripTopLevelKeys ( defSchema , keysToStripFromDefs ) ;
282+ }
283+
268284 // Build the final schema with root schema properties at the top level
269285 const finalSchema = {
270286 ...rootSchemaRewritten ,
271287 "$schema" : schemaVersion ,
272- [ defsKeyword ] : rewrittenDefinitions
288+ [ defsKeyword ] : cleanedDefinitions
273289 } ;
274290
275291 // Post-check: ensure all internal JSON Pointer refs resolve in the final bundle
0 commit comments