diff --git a/types/denom.go b/types/denom.go index bbbec74..a56838a 100644 --- a/types/denom.go +++ b/types/denom.go @@ -1,10 +1,7 @@ package types import ( - "strings" - sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) func NewDenom(id, symbol, name, schema string, creator sdk.AccAddress, description, previewURI string) Denom { @@ -18,55 +15,3 @@ func NewDenom(id, symbol, name, schema string, creator sdk.AccAddress, descripti PreviewURI: previewURI, } } - -func ValidateDenomID(denomID string) error { - denomID = strings.TrimSpace(denomID) - if len(denomID) < MinIDLen || len(denomID) > MaxIDLen { - return sdkerrors.Wrapf(ErrInvalidDenom, "invalid denom ID %s, length must be between [%d, %d]", denomID, MinIDLen, MaxIDLen) - } - if !IsBeginWithAlpha(denomID) || !IsAlphaNumeric(denomID) { - return sdkerrors.Wrapf(ErrInvalidDenom, "invalid denom ID %s, only accepts alphanumeric characters,and begin with an english letter", denomID) - } - return nil -} -func ValidateDenomSymbol(denomSymbol string) error { - denomSymbol = strings.TrimSpace(denomSymbol) - if len(denomSymbol) < MinDenomLen || len(denomSymbol) > MaxDenomLen { - return sdkerrors.Wrapf(ErrInvalidDenom, "invalid denom symbol %s, only accepts value [%d, %d]", denomSymbol, MinDenomLen, MaxDenomLen) - } - if !IsBeginWithAlpha(denomSymbol) || !IsAlpha(denomSymbol) { - return sdkerrors.Wrapf(ErrInvalidDenom, "invalid denom symbol %s, only accepts alphabetic characters", denomSymbol) - } - return nil -} - -func ValidateName(name string) error { - name = strings.TrimSpace(name) - if len(name) > MaxNameLen { - return sdkerrors.Wrapf(ErrInvalidName, "invalid name %s, length must be less than %d", name, MaxNameLen) - } - return nil -} - -func ValidateDescription(description string) error { - description = strings.TrimSpace(description) - if len(description) > MaxDescriptionLen { - return sdkerrors.Wrapf(ErrInvalidDescription, "invalid description %s, length must be less than %d", description, MaxDescriptionLen) - } - return nil -} - -func ValidateURI(uri string) error { - uri = strings.TrimSpace(uri) - if len(uri) > MaxURILen { - return sdkerrors.Wrapf(ErrInvalidURI, "invalid uri %s, length must be less than %d", uri, MaxURILen) - } - return nil -} - -func ValidateCreationFee(fee sdk.Coin) error { - if !fee.IsValid() || fee.IsNil() { - return sdkerrors.Wrapf(ErrInvalidURI, "invalid creation fee %s, fee must be positive", fee.String()) - } - return nil -} diff --git a/types/msgs.go b/types/msgs.go index 8f51bd6..67a593d 100644 --- a/types/msgs.go +++ b/types/msgs.go @@ -221,7 +221,7 @@ func (msg MsgMintONFT) ValidateBasic() error { if err := ValidateDescription(msg.Metadata.Description); err != nil { return err } - if err := ValidateURI(msg.Metadata.MediaURI); err != nil { + if err := ValidateMediaURI(msg.Metadata.MediaURI); err != nil { return err } if err := ValidateURI(msg.Metadata.PreviewURI); err != nil { diff --git a/types/onft.go b/types/onft.go index 775efc7..4e72e82 100644 --- a/types/onft.go +++ b/types/onft.go @@ -1,13 +1,10 @@ package types import ( - "strings" "time" - sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/OmniFlix/onft/exported" + sdk "github.com/cosmos/cosmos-sdk/types" ) var _ exported.ONFT = ONFT{} @@ -86,18 +83,3 @@ func NewONFTs(onfts ...exported.ONFT) ONFTs { } return onfts } - -func ValidateONFTID(onftId string) error { - onftId = strings.TrimSpace(onftId) - if len(onftId) < MinIDLen || len(onftId) > MaxIDLen { - return sdkerrors.Wrapf( - ErrInvalidONFTID, - "invalid onftId %s, length must be between [%d, %d]", onftId, MinIDLen, MaxIDLen) - } - if !IsBeginWithAlpha(onftId) || !IsAlphaNumeric(onftId) { - return sdkerrors.Wrapf( - ErrInvalidONFTID, - "invalid onftId %s, only accepts alphanumeric characters and begin with an english letter", onftId) - } - return nil -} diff --git a/types/validations.go b/types/validations.go new file mode 100644 index 0000000..92f9fc7 --- /dev/null +++ b/types/validations.go @@ -0,0 +1,122 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +func ValidateONFTID(onftId string) error { + if len(onftId) < MinIDLen || len(onftId) > MaxIDLen { + return sdkerrors.Wrapf( + ErrInvalidONFTID, + "invalid onftId %s, length must be between [%d, %d]", onftId, MinIDLen, MaxIDLen) + } + if !IsBeginWithAlpha(onftId) || !IsAlphaNumeric(onftId) { + return sdkerrors.Wrapf( + ErrInvalidONFTID, + "invalid onftId %s, only accepts alphanumeric characters and begin with an english letter", onftId) + } + return nil +} + +func ValidateDenomID(denomID string) error { + if len(denomID) < MinIDLen || len(denomID) > MaxIDLen { + return sdkerrors.Wrapf( + ErrInvalidDenom, + "invalid denom ID %s, length must be between [%d, %d]", + denomID, + MinIDLen, + MaxIDLen, + ) + } + if !IsBeginWithAlpha(denomID) || !IsAlphaNumeric(denomID) { + return sdkerrors.Wrapf( + ErrInvalidDenom, + "invalid denom ID %s, only accepts alphanumeric characters,and begin with an english letter", + denomID, + ) + } + return nil +} +func ValidateDenomSymbol(denomSymbol string) error { + if len(denomSymbol) < MinDenomLen || len(denomSymbol) > MaxDenomLen { + return sdkerrors.Wrapf( + ErrInvalidDenom, + "invalid denom symbol %s, only accepts value [%d, %d]", + denomSymbol, + MinDenomLen, + MaxDenomLen, + ) + } + if !IsBeginWithAlpha(denomSymbol) || !IsAlpha(denomSymbol) { + return sdkerrors.Wrapf( + ErrInvalidDenom, + "invalid denom symbol %s, only accepts alphabetic characters", + denomSymbol, + ) + } + return nil +} + +func ValidateName(name string) error { + if len(name) > MaxNameLen { + return sdkerrors.Wrapf( + ErrInvalidName, + "invalid name %s, length must be less than %d", + name, + MaxNameLen, + ) + } + return nil +} + +func ValidateDescription(description string) error { + if len(description) > MaxDescriptionLen { + return sdkerrors.Wrapf( + ErrInvalidDescription, + "invalid description %s, length must be less than %d", + description, + MaxDescriptionLen, + ) + } + return nil +} + +func ValidateURI(uri string) error { + if len(uri) > MaxURILen { + return sdkerrors.Wrapf( + ErrInvalidURI, + "invalid uri %s, length must be less than %d", + uri, + MaxURILen, + ) + } + return nil +} + +func ValidateMediaURI(uri string) error { + if len(uri) == 0 { + return sdkerrors.Wrapf( + ErrInvalidURI, + "invalid uri %s, media uri should not be empty", + uri, + 0, + ) + } + if len(uri) > MaxURILen { + return sdkerrors.Wrapf( + ErrInvalidURI, + "invalid uri %s, length must be less than %d", + uri, + MaxURILen, + ) + } + return nil +} + +func ValidateCreationFee(fee sdk.Coin) error { + if !fee.IsValid() || fee.IsNil() { + return sdkerrors.Wrapf(ErrInvalidURI, "invalid creation fee %s, fee must be positive", fee.String()) + } + return nil +}