-
Notifications
You must be signed in to change notification settings - Fork 351
/
validate.go
116 lines (103 loc) · 2.19 KB
/
validate.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
package graveler
import (
"strings"
"github.com/treeverse/lakefs/pkg/validator"
)
func ValidateStorageNamespace(v interface{}) error {
s, ok := v.(StorageNamespace)
if !ok {
panic(ErrInvalidType)
}
if len(s) == 0 {
return ErrRequiredValue
}
return nil
}
func ValidateRef(v interface{}) error {
s, ok := v.(Ref)
if !ok {
panic(ErrInvalidType)
}
if len(s) == 0 {
return ErrRequiredValue
}
if !validator.ReValidRef.MatchString(s.String()) {
return ErrInvalidRef
}
return nil
}
func ValidateBranchID(v interface{}) error {
s, ok := v.(BranchID)
if !ok {
panic(ErrInvalidType)
}
if len(s) == 0 {
return ErrRequiredValue
}
if !validator.ReValidBranchID.MatchString(s.String()) {
return ErrInvalidBranchID
}
return nil
}
func ValidateTagID(v interface{}) error {
s, ok := v.(TagID)
if !ok {
panic(ErrInvalidType)
}
// https://git-scm.com/docs/git-check-ref-format
if len(s) == 0 {
return ErrRequiredValue
}
tagID := s.String()
if tagID == "@" {
return ErrInvalidTagID
}
if strings.HasSuffix(tagID, ".") || strings.HasSuffix(tagID, ".lock") {
return ErrInvalidTagID
}
if strings.Contains(tagID, "..") || strings.Contains(tagID, "/") || strings.Contains(tagID, "@{") {
return ErrInvalidTagID
}
// Unlike git, we do allow '~'. That supports migration from our previous ref format where commits started with a tilde.
if strings.ContainsAny(tagID, "^:?*[\\") {
return ErrInvalidTagID
}
for _, r := range tagID {
if isControlCodeOrSpace(r) {
return ErrInvalidTagID
}
}
return nil
}
func isControlCodeOrSpace(r rune) bool {
const space = 0x20
return r <= space
}
func ValidateRepositoryID(v interface{}) error {
var repositoryID string
switch s := v.(type) {
case string:
repositoryID = s
case RepositoryID:
repositoryID = s.String()
default:
panic(ErrInvalidType)
}
if len(repositoryID) == 0 {
return ErrRequiredValue
}
if !validator.ReValidRepositoryID.MatchString(repositoryID) {
return ErrInvalidRepositoryID
}
return nil
}
func ValidateRequiredStrategy(v interface{}) error {
s, ok := v.(string)
if !ok {
panic(ErrInvalidType)
}
if s != "dest-wins" && s != "source-wins" && s != "" {
return ErrInvalidValue
}
return nil
}