diff --git a/jsonschema/schema.go b/jsonschema/schema.go index 19b1728..4e78eb2 100644 --- a/jsonschema/schema.go +++ b/jsonschema/schema.go @@ -293,7 +293,7 @@ func Ptr[T any](x T) *T { return &x } // It stops when f returns false. func (s *Schema) every(f func(*Schema) bool) bool { return s == nil || - f(s) && s.everyChild(f) + f(s) && s.everyChild(func(s *Schema) bool { return s.every(f) }) } // everyChild reports whether f is true for every immediate child schema of s. diff --git a/jsonschema/schema_test.go b/jsonschema/schema_test.go index 614cca1..117db7b 100644 --- a/jsonschema/schema_test.go +++ b/jsonschema/schema_test.go @@ -111,3 +111,18 @@ func TestUnmarshalErrors(t *testing.T) { } } + +func TestEvery(t *testing.T) { + // Schema.every should visit all descendants of a schema, not just the immediate ones. + s := &Schema{ + Items: &Schema{ + Items: &Schema{}, + }, + } + want := 3 + got := 0 + s.every(func(*Schema) bool { got++; return true }) + if got != want { + t.Errorf("got %d, want %d", got, want) + } +}