-
Notifications
You must be signed in to change notification settings - Fork 1
/
relationships.go
43 lines (37 loc) · 1.11 KB
/
relationships.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package jsonapivalidator
func validateRelationshipsObject(r interface{}, result *Result) {
// The value of the relationships key MUST be an object (a “relationships
// object”).
relationships, ok := r.(map[string]interface{})
if !ok {
result.AddError(ErrNotRelationshipsObject)
return // cannot procceed
}
for _, relationshipObject := range relationships {
// TODO: perhaps warn if pluaral && ![]
validateRelationshipObject(relationshipObject, result)
}
}
func validateRelationshipObject(ro interface{}, result *Result) {
relationshipObject, ok := ro.(map[string]interface{})
if !ok {
result.AddError(ErrNotRelationshipObject)
return // cannot procceed
}
links, hasLinks := relationshipObject[memberLinks]
data, hasData := relationshipObject[memberData]
meta, hasMeta := relationshipObject[memberMeta]
if !hasLinks && !hasData && !hasMeta {
result.AddError(ErrRelationshipsObjectOneRequiredMember)
return // cannot procede
}
if hasLinks {
validateLinksObject(links, result, nil)
}
if hasData {
validateResourceLinkage(data, result)
}
if hasMeta {
validateMetaObject(meta, result)
}
}