Skip to content

Commit

Permalink
feat: Update AssertionMethod Validator
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran committed Oct 10, 2024
1 parent 39ab80d commit 8ea629e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
3 changes: 1 addition & 2 deletions x/did/types/diddoc_diddoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ func (didDoc DidDoc) Validate(allowedNamespaces []string) error {
IsUniqueStrList(), validation.Each(IsDIDUrl(allowedNamespaces, Empty, Empty, Required), HasPrefix(didDoc.Id)),
),
validation.Field(&didDoc.AssertionMethod,
IsUniqueStrList(), validation.Each(IsDIDUrl(allowedNamespaces, Empty, Empty, Required), HasPrefix(didDoc.Id)),
),
IsUniqueStrList(), validation.Each(IsAssertionMethod(allowedNamespaces))),
validation.Field(&didDoc.CapabilityInvocation,
IsUniqueStrList(), validation.Each(IsDIDUrl(allowedNamespaces, Empty, Empty, Required), HasPrefix(didDoc.Id)),
),
Expand Down
51 changes: 51 additions & 0 deletions x/did/types/validate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package types

import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"

validation "github.com/go-ozzo/ozzo-validation/v4"
Expand Down Expand Up @@ -105,6 +107,44 @@ func IsDIDUrl(allowedNamespaces []string, pathRule, queryRule, fragmentRule Vali
})
}

type AssertionMethod struct {
Id string
Type string
Controller []string

}

func IsAssertionMethod(allowedNamespaces []string) *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
err := IsDIDUrl(allowedNamespaces, Empty, Empty, Required).Validate(value)
if err != nil {
casted, ok := value.(string)
if !ok {
panic("IsAssertionMethod must be only applied on string properties")
}

unescapedJSON, err := strconv.Unquote(casted)
if err != nil {
return errors.New("assertionMethod should be a DIDUrl or an Escaped JSON string")
}

var result AssertionMethod
err = json.Unmarshal([]byte(unescapedJSON), &result)
if err != nil {
return errors.New("assertionMethod should be a DIDUrl or an Escaped JSON string")
}

return validation.ValidateStruct(&result,
validation.Field(&result.Id, validation.Required, IsDIDUrl(allowedNamespaces, Empty, Empty, Required)),
validation.Field(&result.Controller, validation.Required, IsUniqueStrList(), validation.Each(IsDID(allowedNamespaces))),
validation.Field(&result.Type, IsStr()),
)
}

return nil
})
}

func IsURI() *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
casted, ok := value.(string)
Expand Down Expand Up @@ -221,3 +261,14 @@ func IsUUID() *CustomErrorRule {
return utils.ValidateUUID(casted)
})
}

func IsStr() *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
_, ok := value.([]string)
if !ok {
panic("IsSet must be only applied on string array properties")
}

return nil
})
}
1 change: 1 addition & 0 deletions x/did/utils/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ func ValidateBase58Ed25519VerificationKey2018(data string) error {
}
return ValidateEd25519PubKey(pubKey)
}

0 comments on commit 8ea629e

Please sign in to comment.