internal/mcp/jsonschema: clearer validations errors

Validation errors now more clearly indicate the path from the root
schema.

- Enhance schema iteration to yield the path, and generalize it by using
  reflection.

- Store each schema's path inside the schema.

- Remove the path arg from validate: we don't need it anymore.

- Improve the test for the iterators and add one for validation
  errors.

Change-Id: Ief55133ef4ae2eac0736208a3b9f78ed69933b31
Reviewed-on: https://go-review.googlesource.com/c/tools/+/676375
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
Jonathan Amsterdam 2025-05-26 07:49:23 -04:00 • committed by Gopher Robot
commit e3938ce68c

View file

@ -78,6 +78,21 @@ func TestValidate(t *testing.T) {
}
}
func TestValidateErrors(t *testing.T) {
schema := &Schema{
PrefixItems: []*Schema{{Contains: &Schema{Type: "integer"}}},
}
rs, err := schema.Resolve("", nil)
if err != nil {
t.Fatal(err)
}
err = rs.Validate([]any{[]any{"1"}})
want := "prefixItems/0"
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("error:\n%s\ndoes not contain %q", err, want)
}
}
func TestStructInstance(t *testing.T) {
instance := struct {
I int