-
Notifications
You must be signed in to change notification settings - Fork 41
/
diddoc_service.go
71 lines (56 loc) · 1.73 KB
/
diddoc_service.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
package types
import (
"errors"
"github.com/cheqd/cheqd-node/x/did/utils"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
func NewService(id string, serviceType string, serviceEndpoint []string) *Service {
return &Service{
Id: id,
ServiceType: serviceType,
ServiceEndpoint: serviceEndpoint,
}
}
// ReplaceDids replaces ids in all fields
func (s *Service) ReplaceDids(old, new string) {
// Id
s.Id = utils.ReplaceDidInDidURL(s.Id, old, new)
}
// Helpers
func GetServiceIds(vms []*Service) []string {
res := make([]string, len(vms))
for i := range vms {
res[i] = vms[i].Id
}
return res
}
// Validation
func (s Service) Validate(baseDid string, allowedNamespaces []string) error {
return validation.ValidateStruct(&s,
validation.Field(&s.Id, validation.Required, IsDIDUrl(allowedNamespaces, Empty, Empty, Required), HasPrefix(baseDid)),
validation.Field(&s.ServiceType, validation.Required, validation.Length(1, 255)),
validation.Field(&s.ServiceEndpoint, validation.Each(validation.Required)),
)
}
func ValidServiceRule(baseDid string, allowedNamespaces []string) *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.(Service)
if !ok {
panic("ValidVerificationMethodRule must be only applied on verification methods")
}
return casted.Validate(baseDid, allowedNamespaces)
})
}
func IsUniqueServiceListByIDRule() *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.([]*Service)
if !ok {
panic("IsUniqueServiceListByIdRule must be only applied on service lists")
}
ids := GetServiceIds(casted)
if !utils.IsUnique(ids) {
return errors.New("there are service duplicates")
}
return nil
})
}