-
Notifications
You must be signed in to change notification settings - Fork 1
/
resources.go
121 lines (105 loc) · 2.98 KB
/
resources.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package jsonapivalidator
import "reflect"
func validateResource(r map[string]interface{}, result *Result) {
// We may have either a RIO or RO
// RO = Resource Objec
// RIO = Resource Identifier Object
isRO := isResourceObject(r)
isRIO := isResourceIdentifierObject(r)
if !isRO && !isRIO {
result.AddError(ErrNotAResource)
return
}
if isRO {
validateResourceObject(r, result)
return // return since RO is a superset of RIO
}
validateResourceIdentifierObject(r, result)
return
}
func isResourceObject(r map[string]interface{}) bool {
for key := range r {
switch key {
case memberID, memberType, memberAttributes, memberRelationships, memberLinks, memberMeta:
// do nothing
default:
return false
}
}
return true
}
func validateResourceObject(ro map[string]interface{}, result *Result) {
// A resource object MUST contain at least the following top-level members:
if id, hasID := ro[memberID]; !hasID {
// TODO: The id member is not required when the resource object originates
// at the client and represents a new resource to be created on the server.
result.AddError(ErrResourceObjectMissingID)
} else {
validateID(id, result)
}
if jType, hasType := ro[memberType]; !hasType {
result.AddError(ErrResourceObjectMissingType)
} else {
validateType(jType, result)
}
// A resource object MAY contain any of these top-level members:
if attributes, hasAttributes := ro[memberAttributes]; hasAttributes {
validateAttributesObject(attributes, result)
}
if relationships, hasRelationships := ro[memberRelationships]; hasRelationships {
validateRelationshipsObject(relationships, result)
}
if links, hasLinks := ro[memberLinks]; hasLinks {
validateLinksObject(links, result, nil)
}
if meta, hasMeta := ro[memberMeta]; hasMeta {
validateMetaObject(meta, result)
}
}
func isResourceIdentifierObject(r map[string]interface{}) bool {
for key := range r {
switch key {
case memberID, memberType, memberMeta:
// do nothing
default:
return false
}
}
return true
}
func validateResourceIdentifierObject(ro map[string]interface{}, result *Result) {
if id, hasID := ro[memberID]; !hasID {
result.AddError(ErrResourceObjectIdentifierMissingID)
} else {
validateID(id, result)
}
if jType, hasType := ro[memberType]; !hasType {
result.AddError(ErrResourceObjectIdentifierMissingType)
} else {
validateType(jType, result)
}
if meta, hasMeta := ro[memberMeta]; hasMeta {
validateMetaObject(meta, result)
}
return
}
func validateResourceLinkage(rl interface{}, result *Result) {
if rl == nil {
return // null for empty to-one relationships.
}
switch reflect.TypeOf(rl).Kind() {
case reflect.Map:
// Single
r := rl.(map[string]interface{})
validateResourceIdentifierObject(r, result)
case reflect.Slice:
// Array
resources := rl.([]interface{})
for _, resource := range resources {
r := resource.(map[string]interface{})
validateResourceIdentifierObject(r, result)
}
default:
result.AddError(ErrInvalidResourceLinkage)
}
}