Skip to content

Commit

Permalink
Apply suggestions from review
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano committed Sep 9, 2024
1 parent d115a79 commit abb387e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
43 changes: 22 additions & 21 deletions internal/fields/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/fields/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit abb387e

Please sign in to comment.