Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions jsonschema/json_pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
"reflect"
"strconv"
"strings"

"github.com/modelcontextprotocol/go-sdk/internal/util"
)

var (
Expand Down Expand Up @@ -71,7 +69,7 @@ func parseJSONPointer(ptr string) (segments []string, err error) {
// This implementation suffices for JSON Schema: pointers are applied only to Schemas,
// and refer only to Schemas.
func dereferenceJSONPointer(s *Schema, sptr string) (_ *Schema, err error) {
defer util.Wrapf(&err, "JSON Pointer %q", sptr)
defer wrapf(&err, "JSON Pointer %q", sptr)

segments, err := parseJSONPointer(sptr)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions jsonschema/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,10 @@ func fieldJSONInfo(f reflect.StructField) jsonInfo {
}
return info
}

// wrapf wraps *errp with the given formatted message if *errp is not nil.
func wrapf(errp *error, format string, args ...any) {
if *errp != nil {
*errp = fmt.Errorf("%s: %w", fmt.Sprintf(format, args...), *errp)
}
}
6 changes: 2 additions & 4 deletions jsonschema/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"strings"
"sync"
"unicode/utf8"

"github.com/modelcontextprotocol/go-sdk/internal/util"
)

// The value of the "$schema" keyword for the version that we can validate.
Expand Down Expand Up @@ -74,7 +72,7 @@ type state struct {

// validate validates the reflected value of the instance.
func (st *state) validate(instance reflect.Value, schema *Schema, callerAnns *annotations) (err error) {
defer util.Wrapf(&err, "validating %s", st.rs.schemaString(schema))
defer wrapf(&err, "validating %s", st.rs.schemaString(schema))

// Maintain a stack for dynamic schema resolution.
st.stack = append(st.stack, schema) // push
Expand Down Expand Up @@ -613,7 +611,7 @@ func (rs *Resolved) ApplyDefaults(instancep any) error {
// Leave this as a potentially recursive helper function, because we'll surely want
// to apply defaults on sub-schemas someday.
func (st *state) applyDefaults(instancep reflect.Value, schema *Schema) (err error) {
defer util.Wrapf(&err, "applyDefaults: schema %s, instance %v", st.rs.schemaString(schema), instancep)
defer wrapf(&err, "applyDefaults: schema %s, instance %v", st.rs.schemaString(schema), instancep)

schemaInfo := st.rs.resolvedInfos[schema]
instance := instancep.Elem()
Expand Down