-
Notifications
You must be signed in to change notification settings - Fork 1
/
links.go
73 lines (67 loc) · 1.89 KB
/
links.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
package jsonapivalidator
import (
"fmt"
"reflect"
"strings"
)
func validateLinksObject(l interface{}, result *Result,
allowedMembers *map[string]interface{}) {
// TODO: in a lot of cases there are only certain members allowed in the links
// object; for instance when dealing with a /links object at the top level,
// only "self" and "related" members are allowed. The onlyMembers argument
// will be used to specifiy the allowd member keys.
links, ok := l.(map[string]interface{})
if !ok {
result.AddError(ErrNotLinksObject)
return // cannot procceed
}
for k, v := range links {
if allowedMembers != nil {
allowed := *allowedMembers
if _, ok := allowed[k]; !ok {
result.AddError(
fmt.Errorf(
"The links object member `%s` is not allowed; has to be one of `%s`",
k,
strings.Join(keys(allowed), ", "),
),
)
}
}
validateLinkObject(v, result)
}
}
func validateLinkObject(l interface{}, result *Result) {
// Each member of a links object is a “link”. A link MUST be represented as
// either:
switch reflect.TypeOf(l).Kind() {
case reflect.String:
// a string containing the link’s URL.
validateURL(l, result)
case reflect.Map:
// an object (“link object”) which can contain the following members:
// href: a string containing the link’s URL.
// meta: a meta object containing non-standard meta-information about the
// link.
link, ok := l.(map[string]interface{})
if !ok {
result.AddError(ErrNotLinkObject)
return // cannot proceed
}
for k, v := range link {
switch k {
case memberHref:
// a string containing the link’s URL.
validateURL(v, result)
case memberMeta:
// a meta object containing non-standard meta-information about the
// link.
validateMetaObject(v, result)
default:
result.AddError(ErrInvalidLinkMember)
}
}
default:
result.AddError(ErrInvalidLinkType)
}
}