-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvalidation-rules.go
70 lines (50 loc) · 1.77 KB
/
validation-rules.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
package validation
import (
"fmt"
"regexp"
)
type Rule string
const (
RuleType Rule = "[^:#@\\*\\s]{1,254}"
RuleRelation Rule = "[^:#@\\*\\s]{1,50}"
RuleCondition Rule = "[^\\*\\s]{1,50}"
RuleID Rule = "[^#:\\s*][a-zA-Z0-9_|*@.+]*"
RuleObject Rule = "[^\\s]{2,256}"
)
func ValidateObject(object string) bool {
typeMatch, _ := regexp.MatchString(fmt.Sprintf("^%s:%s$", RuleType, RuleID), object)
objectMatch, _ := regexp.MatchString(fmt.Sprintf("^%s$", RuleObject), object)
return typeMatch && objectMatch
}
func ValidateObjectID(relation string) bool {
match, _ := regexp.MatchString(fmt.Sprintf("^%s$", RuleID), relation)
return match
}
func ValidateRelation(relation string) bool {
match, _ := regexp.MatchString(fmt.Sprintf("^%s$", RuleRelation), relation)
return match
}
func ValidateUserSet(userSet string) bool {
match, _ := regexp.MatchString(fmt.Sprintf("^%s:%s#%s$", RuleType, RuleID, RuleRelation), userSet)
return match
}
func ValidateUserObject(userObject string) bool {
typeMatch, _ := regexp.MatchString(fmt.Sprintf("^%s:%s$", RuleType, RuleID), userObject)
objectMatch, _ := regexp.MatchString(fmt.Sprintf("^%s$", RuleObject), userObject)
return typeMatch && objectMatch
}
func ValidateUserWildcard(userWildcard string) bool {
match, _ := regexp.MatchString(fmt.Sprintf("^%s:\\*$", RuleType), userWildcard)
return match
}
func ValidateUser(user string) bool {
return ValidateUserSet(user) || ValidateObject(user) || ValidateUserWildcard(user)
}
func ValidateRelationshipCondition(condition string) bool {
match, _ := regexp.MatchString(fmt.Sprintf("^%s$", RuleCondition), condition)
return match
}
func ValidateType(typeString string) bool {
match, _ := regexp.MatchString(fmt.Sprintf("^%s$", RuleType), typeString)
return match
}