From 3a4dd8753065bc050cd47a30d6dcf88634d0ea01 Mon Sep 17 00:00:00 2001 From: DaevMithran Date: Thu, 10 Oct 2024 16:17:52 +0530 Subject: [PATCH] feat: Update AssertionMethod Validator --- x/did/types/diddoc_diddoc.go | 3 +-- x/did/types/validate.go | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/x/did/types/diddoc_diddoc.go b/x/did/types/diddoc_diddoc.go index 83df2fb88..7c30fdf80 100644 --- a/x/did/types/diddoc_diddoc.go +++ b/x/did/types/diddoc_diddoc.go @@ -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)), ), diff --git a/x/did/types/validate.go b/x/did/types/validate.go index 75643885b..697b303d7 100644 --- a/x/did/types/validate.go +++ b/x/did/types/validate.go @@ -1,8 +1,10 @@ package types import ( + "encoding/json" "errors" "fmt" + "strconv" "strings" validation "github.com/go-ozzo/ozzo-validation/v4" @@ -105,6 +107,43 @@ 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) @@ -221,3 +260,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 + }) +}