diff --git a/internal/fields/validate.go b/internal/fields/validate.go index 87d7e7baf..20cde32a5 100644 --- a/internal/fields/validate.go +++ b/internal/fields/validate.go @@ -828,31 +828,32 @@ func skipValidationForField(key string) bool { // skipLeafOfObject checks if the element is a child of an object that was skipped in some previous // version of the spec. This is relevant in documents that store fields without subobjects. func skipLeafOfObject(root, name string, specVersion semver.Version, schema []FieldDefinition) bool { - if specVersion.LessThan(semver3_0_1) { - // Check if this is a subobject of an object we didn't traverse. - if !strings.Contains(name, ".") { + // We are only skipping validation of these fields on versions older than 3.0.1. + if !specVersion.LessThan(semver3_0_1) { + return false + } + + // If it doesn't contain a dot in the name, we have traversed its parent, if any. + if !strings.Contains(name, ".") { + return false + } + + key := name + if root != "" { + key = root + "." + name + } + _, ancestor := findAncestorElementDefinition(key, schema, func(key string, def *FieldDefinition) bool { + // Don't look for ancestors beyond root, these objects have been already traversed. + if len(key) < len(root) { return false } - key := name - if root != "" { - key = root + "." + name - } - _, ancestor := findAncestorElementDefinition(key, schema, func(key string, def *FieldDefinition) bool { - // Don't look for ancestors beyond root, these objects have been already traversed. - if len(key) < len(root) { - return false - } - if !slices.Contains([]string{"group", "object", "nested", "flattened"}, def.Type) { - return false - } - return true - }) - if ancestor != nil { - return true + if !slices.Contains([]string{"group", "object", "nested", "flattened"}, def.Type) { + return false } - } + return true + }) - return false + return ancestor != nil } func isFieldFamilyMatching(family, key string) bool { diff --git a/internal/fields/validate_test.go b/internal/fields/validate_test.go index 881b5754d..e52673430 100644 --- a/internal/fields/validate_test.go +++ b/internal/fields/validate_test.go @@ -1108,7 +1108,7 @@ func TestSkipLeafOfObject(t *testing.T) { // Cases we expect to skip depending on the version. okRoots := []string{"flattened", "object", "group", "nested"} for _, root := range okRoots { - t.Run("(empty root)", func(t *testing.T) { + t.Run("empty root with prefix "+root, func(t *testing.T) { for _, c := range cases { t.Run(c.name+"_"+c.version.String(), func(t *testing.T) { found := skipLeafOfObject("", root+"."+c.name, *c.version, schema)