-
Notifications
You must be signed in to change notification settings - Fork 13
/
messages_validation_utils.go
139 lines (115 loc) · 3.02 KB
/
messages_validation_utils.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package types
import (
"encoding/base64"
"fmt"
"reflect"
"regexp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ipfs/go-cid"
)
func ValidateRepositoryName(name string) error {
if len(name) < 3 {
return fmt.Errorf("Repository name must be at least 3 characters long")
} else if len(name) > 100 {
return fmt.Errorf("Repository name exceeds limit: 100")
}
sanitized := IsNameSanitized(name)
if !sanitized {
return fmt.Errorf("repository name is not sanitized")
}
return nil
}
func ValidateRepositoryDescription(description string) error {
if len(description) > 255 {
return fmt.Errorf("description length exceeds limit: 255")
}
return nil
}
func ValidateRepositoryId(repositoryId RepositoryId) error {
_, err := sdk.AccAddressFromBech32(repositoryId.Id)
if err != nil {
if len(repositoryId.Id) < 3 {
return fmt.Errorf("id must consist minimum 3 chars")
} else if len(repositoryId.Id) > 39 {
return fmt.Errorf("id limit exceed: 39")
}
valid, err := regexp.MatchString("^[a-zA-Z0-9]+(?:[-]?[a-zA-Z0-9])*$", repositoryId.Id)
if err != nil {
return fmt.Errorf(err.Error())
}
if !valid {
return fmt.Errorf("invalid id (%v)", repositoryId.Id)
}
}
if err := ValidateRepositoryName(repositoryId.Name); err != nil {
return err
}
return nil
}
func ValidateOptionalBranchName(name string) error {
if len(name) > 255 {
return fmt.Errorf("branch length exceeds limit: 255")
}
if valid, err := IsValidRefname(name); !valid {
return err
}
return nil
}
func ValidateBranchName(name string) error {
if len(name) > 255 {
return fmt.Errorf("branch length exceeds limit: 255")
} else if len(name) < 1 {
return fmt.Errorf("branch name can't be empty")
}
if valid, err := IsValidRefname(name); !valid {
return err
}
return nil
}
func ValidateOptionalCommentBody(body string) error {
if len(body) > 20000 {
return fmt.Errorf("comment exceeds limit: 20000")
}
return nil
}
func ValidateCommentBody(body string) error {
if len(body) < 1 {
return fmt.Errorf("empty comment not allowed")
} else if len(body) > 20000 {
return fmt.Errorf("comment exceeds limit: 20000")
}
return nil
}
func ValidateIpfsCid(ipfsCid string) error {
// if len(ipfsCid) != 46 {
// return fmt.Errorf("invalid IPFS cid (%s)", ipfsCid)
// }
if _, err := cid.Decode(ipfsCid); err != nil {
return fmt.Errorf("invalid IPFS cid (%s)", ipfsCid)
}
return nil
}
func ValidateArweaveTxId(arweaveTxId string) error {
if len(arweaveTxId) != 43 {
return fmt.Errorf("invalid arweave transaction id (%s)", arweaveTxId)
}
if _, err := base64.URLEncoding.WithPadding(base64.NoPadding).DecodeString(arweaveTxId); err != nil {
return fmt.Errorf("invalid arweave transaction id (%s)", arweaveTxId)
}
return nil
}
func allUnique(slice interface{}) bool {
seen := make(map[interface{}]bool)
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
panic("allUnique: not a slice")
}
for i := 0; i < v.Len(); i++ {
val := v.Index(i).Interface()
if seen[val] {
return false
}
seen[val] = true
}
return true
}