-
Notifications
You must be signed in to change notification settings - Fork 41
/
validate.go
201 lines (157 loc) · 4.27 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package types
import (
"errors"
"fmt"
"strings"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/cheqd/cheqd-node/x/did/utils"
"github.com/multiformats/go-multibase"
)
// Helper enums
type ValidationType int
const (
Optional ValidationType = iota
Required ValidationType = iota
Empty ValidationType = iota
)
// Custom error rule
var _ validation.Rule = &CustomErrorRule{}
type CustomErrorRule struct {
fn func(value interface{}) error
}
func NewCustomErrorRule(fn func(value interface{}) error) *CustomErrorRule {
return &CustomErrorRule{fn: fn}
}
func (c CustomErrorRule) Validate(value interface{}) error {
return c.fn(value)
}
// Validation helpers
func IsID() *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.(string)
if !ok {
panic("IsID must be only applied on string properties")
}
return utils.ValidateID(casted)
})
}
func IsDID(allowedNamespaces []string) *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.(string)
if !ok {
panic("IsDID must be only applied on string properties")
}
return utils.ValidateDID(casted, DidMethod, allowedNamespaces)
})
}
func IsDIDUrl(allowedNamespaces []string, pathRule, queryRule, fragmentRule ValidationType) *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.(string)
if !ok {
panic("IsDIDUrl must be only applied on string properties")
}
if err := utils.ValidateDIDUrl(casted, DidMethod, allowedNamespaces); err != nil {
return err
}
_, path, query, fragment, err := utils.TrySplitDIDUrl(casted)
if err != nil {
return err
}
if pathRule == Required && path == "" {
return errors.New("path is required")
}
if pathRule == Empty && path != "" {
return errors.New("path must be empty")
}
if queryRule == Required && query == "" {
return errors.New("query is required")
}
if queryRule == Empty && query != "" {
return errors.New("query must be empty")
}
if fragmentRule == Required && fragment == "" {
return errors.New("fragment is required")
}
if fragmentRule == Empty && fragment != "" {
return errors.New("fragment must be empty")
}
return nil
})
}
func IsURI() *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.(string)
if !ok {
panic("IsURI must be only applied on string properties")
}
return utils.ValidateURI(casted)
})
}
func IsMultibase() *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.(string)
if !ok {
panic("IsMultibase must be only applied on string properties")
}
return utils.ValidateMultibase(casted)
})
}
func IsMultibaseEncodedEd25519PubKey() *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.(string)
if !ok {
panic("IsMultibaseEncodedEd25519PubKey must be only applied on string properties")
}
_, keyBytes, err := multibase.Decode(casted)
if err != nil {
return err
}
err = utils.ValidateEd25519PubKey(keyBytes)
if err != nil {
return err
}
return nil
})
}
func IsJWK() *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.(string)
if !ok {
panic("IsJWK must be only applied on string properties")
}
return utils.ValidateJWK(casted)
})
}
func HasPrefix(prefix string) *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.(string)
if !ok {
panic("HasPrefix must be only applied on string properties")
}
if !strings.HasPrefix(casted, prefix) {
return fmt.Errorf("must have prefix: %s", prefix)
}
return nil
})
}
func IsUniqueStrList() *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.([]string)
if !ok {
panic("IsSet must be only applied on string array properties")
}
if !utils.IsUnique(casted) {
return errors.New("there should be no duplicates")
}
return nil
})
}
func IsUUID() *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.(string)
if !ok {
panic("IsDID must be only applied on string properties")
}
return utils.ValidateUUID(casted)
})
}