@@ -539,6 +539,49 @@ function resolvePointer(root: unknown, ref: string) {
539539 return true
540540}
541541
542+ // A plain-name `$ref` (`#name`) resolves against the anchors declared in its own schema
543+ // resource, so an anchor only satisfies a ref sharing its nearest `$id` scope; ajv rejects
544+ // a ref that reaches an anchor in another resource. Collect the anchors that survive in the
545+ // emitted document, keyed by resource. Only positions ajv registers anchors from are
546+ // walked: it ignores an `$anchor` on the top-level schema itself and inside `prefixItems`,
547+ // while an `$anchor` under `enum`/`const`/a custom keyword is instance data (NEW-V14-07).
548+ function collectAnchors ( root : unknown ) {
549+ const byResource = new Map < string , Set < string > > ( )
550+ const walk = ( value : unknown , kind : SchemaKind , resource : string , atRoot : boolean ) => {
551+ if ( Array . isArray ( value ) ) {
552+ value . forEach ( ( item ) => walk ( item , kind , resource , false ) )
553+ return
554+ }
555+ if ( ! isPlainObject ( value ) ) return
556+ const nodeResource = kind === "schema" && typeof value . $id === "string" ? value . $id : resource
557+ for ( const [ key , item ] of Object . entries ( value ) ) {
558+ if ( ! atRoot && kind === "schema" && ( key === "$anchor" || key === "$dynamicAnchor" ) ) {
559+ if ( typeof item === "string" ) {
560+ const names = byResource . get ( nodeResource )
561+ if ( names ) names . add ( item )
562+ else byResource . set ( nodeResource , new Set ( [ item ] ) )
563+ }
564+ continue
565+ }
566+ const childKind : SchemaKind =
567+ kind === "map"
568+ ? "schema"
569+ : kind === "instance"
570+ ? "instance"
571+ : key === "enum" || key === "const" || key === "default" || key === "examples"
572+ ? "instance"
573+ : SCHEMA_MAP_KEYWORDS . has ( key )
574+ ? "map"
575+ : SCHEMA_VALUE_KEYWORDS . has ( key ) || SCHEMA_ARRAY_KEYWORDS . has ( key )
576+ ? "schema"
577+ : "instance"
578+ walk ( item , childKind , nodeResource , false )
579+ }
580+ }
581+ walk ( root , "schema" , "" , true )
582+ return byResource
583+ }
584+
542585// A `$defs` entry can be pruned (depth, node budget, or shared identity) while a `$ref`
543586// to it survives, leaving an unresolvable local reference that makes providers reject the
544587// whole manifest. Drop such refs, leaving the containing schema unconstrained. Only
@@ -549,12 +592,15 @@ function dropDanglingRefs(
549592 root : unknown = value ,
550593 kind : SchemaKind = "schema" ,
551594 defs : Map < string , Set < string > > = topLevelDefs ( root ) ,
595+ anchors : Map < string , Set < string > > = collectAnchors ( root ) ,
596+ resource = "" ,
552597) {
553598 if ( Array . isArray ( value ) ) {
554- value . forEach ( ( item ) => dropDanglingRefs ( item , root , kind , defs ) )
599+ value . forEach ( ( item ) => dropDanglingRefs ( item , root , kind , defs , anchors , resource ) )
555600 return
556601 }
557602 if ( ! isPlainObject ( value ) ) return
603+ const nodeResource = kind === "schema" && typeof value . $id === "string" ? value . $id : resource
558604 for ( const [ key , item ] of Object . entries ( value ) ) {
559605 if ( kind === "schema" && key === "$ref" ) {
560606 // Only local `#` pointers are resolvable in the emitted document. `urn:`/relative/
@@ -569,6 +615,10 @@ function dropDanglingRefs(
569615 if ( ! defs . get ( def [ 1 ] ) ?. has ( def [ 2 ] ) ) delete value [ key ]
570616 continue
571617 }
618+ // `#name` is a plain-name anchor reference. Keep it only when an anchor with that
619+ // name survives in the same `$id` resource; a ref to an anchor that was stripped or
620+ // never existed stays dangling and is pruned like any other unresolvable pointer.
621+ if ( item . length > 1 && item [ 1 ] !== "/" && anchors . get ( nodeResource ) ?. has ( item . slice ( 1 ) ) ) continue
572622 if ( ! resolvePointer ( root , item ) ) delete value [ key ]
573623 continue
574624 }
@@ -582,7 +632,7 @@ function dropDanglingRefs(
582632 : SCHEMA_MAP_KEYWORDS . has ( key )
583633 ? "map"
584634 : "schema"
585- dropDanglingRefs ( item , root , childKind , defs )
635+ dropDanglingRefs ( item , root , childKind , defs , anchors , nodeResource )
586636 }
587637}
588638
0 commit comments