-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix validation of subfields of flattened objects #2088
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
"userName": "Bob", | ||
"groupName": "admin" | ||
} | ||
} | ||
}, | ||
"flattened.request_parameters.userID": 1000 | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -681,6 +681,8 @@ func (v *Validator) validateScalarElement(key string, val any, doc common.MapStr | |
switch { | ||
case skipValidationForField(key): | ||
return nil // generic field, let's skip validation for now | ||
case isFlattenedSubfield(key, v.Schema): | ||
return nil // flattened subfield, it will be stored as member of the flattened ancestor. | ||
case isArrayOfObjects(val): | ||
return fmt.Errorf(`field %q is used as array of objects, expected explicit definition with type group or nested`, key) | ||
case couldBeMultifield(key, v.Schema): | ||
|
@@ -855,6 +857,22 @@ func isArrayOfObjects(val any) bool { | |
return false | ||
} | ||
|
||
func isFlattenedSubfield(key string, schema []FieldDefinition) bool { | ||
for strings.Contains(key, ".") { | ||
i := strings.LastIndex(key, ".") | ||
key = key[:i] | ||
ancestor := FindElementDefinition(key, schema) | ||
if ancestor == nil { | ||
continue | ||
} | ||
if ancestor.Type == "flattened" { | ||
return true | ||
} | ||
Comment on lines
+868
to
+870
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it finds an ancestor with a different type of "flattened", should this return false directly here? I was thinking in the case of the ancestor being something like: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure, there are packages defining mappings for subfields of flattened objects (for example here). This doesn't make a lot of sense as a mapping, but this is allowed now, and can be used for example to provide documentation. We probably should not allow to define mappings under There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ok, I didn't know about this kind of usage.
Agreed! |
||
} | ||
|
||
return false | ||
} | ||
|
||
func findElementDefinitionForRoot(root, searchedKey string, fieldDefinitions []FieldDefinition) *FieldDefinition { | ||
for _, def := range fieldDefinitions { | ||
key := strings.TrimLeft(root+"."+def.Name, ".") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be replaced by findParentElementDefinition ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mmm I guess it would be needed to somehow return also the key of the field, or being able to retrieve it from the
ancestor
struct.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, this is the problem I found, we still need to loop over the ancestors.