internal/mcp/jsonschema: support validating structs

The instance passed to Resolved.Validate can be a struct, or can
contain structs. A struct is treated like the JSON object that it
marshals to.

Also support pointers to any depth. That is, if an instance is a **int,
then Validate will treat it like an int (or nil). That won't happen if
the instance is unmarshaled into an `any`, but may if it's unmarshaled
into a struct.

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

View file

@ -121,6 +121,11 @@ type Schema struct {
patternProperties map[*regexp.Regexp]*Schema
}
// falseSchema returns a new Schema tree that fails to validate any value.
func falseSchema() *Schema {
return &Schema{Not: &Schema{}}
}
// String returns a short description of the schema.
func (s *Schema) String() string {
if s.ID != "" {
@ -182,7 +187,7 @@ func (s *Schema) UnmarshalJSON(data []byte) error {
*s = Schema{}
} else {
// false is the schema that validates nothing.
*s = Schema{Not: &Schema{}}
*s = *falseSchema()
}
return nil
}