jsonschema: resolve anchors

Change-Id: Ibe7d8df0a46d3e3a8b5d0651b1113a54e756c7df
Reviewed-on: https://go-review.googlesource.com/c/tools/+/669998
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Jonathan Amsterdam 2025-04-26 11:24:37 -04:00
commit 5cc4e6ea54

View file

@ -122,8 +122,7 @@ func (s *Schema) checkLocal(report func(error)) {
// See https://json-schema.org/draft/2020-12/json-schema-core#section-8.2, section
// 8.2.1.
// TODO(jba): anchors (section 8.2.2)
// TODO(jba): dynamicAnchors (ditto)
// TODO(jba): dynamicAnchors (§8.2.2)
//
// Every schema has a base URI and a parent base URI.
//
@ -180,6 +179,18 @@ func resolveURIs(root *Schema, baseURI *url.URL) (map[string]*Schema, error) {
base = s // needed for anchors
}
// Anchors are URI fragments that are scoped to their base.
// We treat them as keys in a map stored within the schema.
if s.Anchor != "" {
if base.anchors[s.Anchor] != nil {
return fmt.Errorf("duplicate anchor %q in %s", s.Anchor, base.baseURI)
}
if base.anchors == nil {
base.anchors = map[string]*Schema{}
}
base.anchors[s.Anchor] = s
}
for c := range s.children() {
if err := resolve(c, base); err != nil {
return err