-
Notifications
You must be signed in to change notification settings - Fork 1
/
attributes.go
82 lines (72 loc) · 2.46 KB
/
attributes.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
package jsonapivalidator
import (
"reflect"
"strings"
)
func validateAttributesObject(a interface{}, result *Result) {
// The value of the attributes key MUST be an object (an “attributes object”).
attributes, ok := a.(map[string]interface{})
if !ok {
result.AddError(ErrNotAttributesObject)
return // cannot procceed
}
for k, v := range attributes {
validateAttributeMemberName(k, result)
validateAttributeValue(v, result)
}
}
func validateAttributeMemberName(m string, result *Result) {
// Although has-one foreign keys (e.g. author_id) are often stored internally
// alongside other information to be represented in a resource object, these
// keys SHOULD NOT appear as attributes.
if strings.Contains(strings.ToLower(m), "id") {
// TODO: improve this with a regex; currently it will product false warnings
// for words that contain "id"
result.AddWarning(WarnAttributesObjectFK)
}
// Complex data structures involving JSON objects and arrays are allowed as
// attribute values. However, any object that constitutes or is contained in
// an attribute MUST NOT contain a relationships or links member, as those
// members are reserved by this specification for future use.
if m == memberRelationships {
result.AddWarning(WarnAttributesObjectHasRelationshipsMember)
}
if m == memberLinks {
result.AddWarning(WarnAttributesObjectHasLinksMember)
}
}
func validateAttributeValue(v interface{}, result *Result) {
// Complex data structures involving JSON objects and arrays are allowed as
// attribute values. However, any object that constitutes or is contained in
// an attribute MUST NOT contain a relationships or links member, as those
// members are reserved by this specification for future use.
if v == nil {
return // JSON attribute was null
}
// We had a non null value
switch reflect.TypeOf(v).Kind() {
case reflect.Map:
// TODO: check this map for relationships or links members
mapValue, ok := v.(map[string]interface{})
if !ok {
result.AddError(ErrNotLinkObject) // TODO: what error?
return
}
for k, v := range mapValue {
validateAttributeMemberName(k, result)
validateAttributeValue(v, result)
}
case reflect.Slice:
// Check each element of this slice for relationships or links members
sliceValues, ok := v.([]interface{})
if !ok {
result.AddError(ErrNotLinkObject) // TODO: what error?
return
}
for _, sv := range sliceValues {
validateAttributeValue(sv, result)
}
default:
return
}
}