The first of several CLs providing JSON Schema validation. Replace the existing Schema type with one that has all the keywords of the most current version of the JSON Schema spec, called 2020-12. The 2020-12 spec can be represented type-safely in Go with one exception: the "type" keyword can be either a string or a []string. To handle this safely, both Type and Types fields are defined. Custom JSONMarshal/Unmarshal methods handle the conversion. We need the unmarshal anyway because "true" and "false" are valid schemas. Unfortunately, the MCP spec uses the older "draft7" version of JSON Schema, but the two are close enough for code generation to work. I confirmed that the protocol generator produces the identical output. Change-Id: I44f6ea2f3a3f8498979946f69dfb7f50afa1acad Reviewed-on: https://go-review.googlesource.com/c/tools/+/667855 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com>
162 lines
3.7 KiB
Go
162 lines
3.7 KiB
Go
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
|
|
// Use of this source code is governed by an MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package jsonschema
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"math/big"
|
|
"reflect"
|
|
)
|
|
|
|
// Equal reports whether two Go values representing JSON values are equal according
|
|
// to the JSON Schema spec.
|
|
// The values must not contain cycles.
|
|
// See https://json-schema.org/draft/2020-12/json-schema-core#section-4.2.2.
|
|
// It behaves like reflect.DeepEqual, except that numbers are compared according
|
|
// to mathematical equality.
|
|
func Equal(x, y any) bool {
|
|
return equalValue(reflect.ValueOf(x), reflect.ValueOf(y))
|
|
}
|
|
|
|
func equalValue(x, y reflect.Value) bool {
|
|
// Copied from src/reflect/deepequal.go, omitting the visited check (because JSON
|
|
// values are trees).
|
|
if !x.IsValid() || !y.IsValid() {
|
|
return x.IsValid() == y.IsValid()
|
|
}
|
|
|
|
// Treat numbers specially.
|
|
rx, ok1 := jsonNumber(x)
|
|
ry, ok2 := jsonNumber(y)
|
|
if ok1 && ok2 {
|
|
return rx.Cmp(ry) == 0
|
|
}
|
|
if x.Kind() != y.Kind() {
|
|
return false
|
|
}
|
|
switch x.Kind() {
|
|
case reflect.Array:
|
|
if x.Len() != y.Len() {
|
|
return false
|
|
}
|
|
for i := range x.Len() {
|
|
if !equalValue(x.Index(i), y.Index(i)) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
case reflect.Slice:
|
|
if x.IsNil() != y.IsNil() {
|
|
return false
|
|
}
|
|
if x.Len() != y.Len() {
|
|
return false
|
|
}
|
|
if x.UnsafePointer() == y.UnsafePointer() {
|
|
return true
|
|
}
|
|
// Special case for []byte, which is common.
|
|
if x.Type().Elem().Kind() == reflect.Uint8 && x.Type() == y.Type() {
|
|
return bytes.Equal(x.Bytes(), y.Bytes())
|
|
}
|
|
for i := range x.Len() {
|
|
if !equalValue(x.Index(i), y.Index(i)) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
case reflect.Interface:
|
|
if x.IsNil() || y.IsNil() {
|
|
return x.IsNil() == y.IsNil()
|
|
}
|
|
return equalValue(x.Elem(), y.Elem())
|
|
case reflect.Pointer:
|
|
if x.UnsafePointer() == y.UnsafePointer() {
|
|
return true
|
|
}
|
|
return equalValue(x.Elem(), y.Elem())
|
|
case reflect.Struct:
|
|
t := x.Type()
|
|
if t != y.Type() {
|
|
return false
|
|
}
|
|
for i := range t.NumField() {
|
|
sf := t.Field(i)
|
|
if !sf.IsExported() {
|
|
continue
|
|
}
|
|
if !equalValue(x.FieldByIndex(sf.Index), y.FieldByIndex(sf.Index)) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
case reflect.Map:
|
|
if x.IsNil() != y.IsNil() {
|
|
return false
|
|
}
|
|
if x.Len() != y.Len() {
|
|
return false
|
|
}
|
|
if x.UnsafePointer() == y.UnsafePointer() {
|
|
return true
|
|
}
|
|
iter := x.MapRange()
|
|
for iter.Next() {
|
|
vx := iter.Value()
|
|
vy := y.MapIndex(iter.Key())
|
|
if !vy.IsValid() || !equalValue(vx, vy) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
case reflect.Func:
|
|
if x.Type() != y.Type() {
|
|
return false
|
|
}
|
|
if x.IsNil() && y.IsNil() {
|
|
return true
|
|
}
|
|
panic("cannot compare functions")
|
|
case reflect.String:
|
|
return x.String() == y.String()
|
|
case reflect.Bool:
|
|
return x.Bool() == y.Bool()
|
|
case reflect.Complex64, reflect.Complex128:
|
|
return x.Complex() == y.Complex()
|
|
// Ints, uints and floats handled in jsonNumber, at top of function.
|
|
default:
|
|
panic(fmt.Sprintf("unsupported kind: %s", x.Kind()))
|
|
}
|
|
}
|
|
|
|
// jsonNumber converts a numeric value or a json.Number to a [big.Rat].
|
|
// If v is not a number, it returns nil, false.
|
|
func jsonNumber(v reflect.Value) (*big.Rat, bool) {
|
|
r := new(big.Rat)
|
|
switch {
|
|
case !v.IsValid():
|
|
return nil, false
|
|
case v.CanInt():
|
|
r.SetInt64(v.Int())
|
|
case v.CanUint():
|
|
r.SetUint64(v.Uint())
|
|
case v.CanFloat():
|
|
r.SetFloat64(v.Float())
|
|
default:
|
|
jn, ok := v.Interface().(json.Number)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
if _, ok := r.SetString(jn.String()); !ok {
|
|
// This can fail in rare cases; for example, "1e9999999".
|
|
// That is a valid JSON number, since the spec puts no limit on the size
|
|
// of the exponent.
|
|
return nil, false
|
|
}
|
|
}
|
|
return r, true
|
|
}
|