Skip to content

Commit

Permalink
fix: validation fixes (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
harish551 authored Jun 7, 2023
1 parent e6c4f32 commit e0d6a54
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 75 deletions.
55 changes: 0 additions & 55 deletions types/denom.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 1 addition & 19 deletions types/onft.go
Original file line number Diff line number Diff line change
@@ -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{}
Expand Down Expand Up @@ -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
}
122 changes: 122 additions & 0 deletions types/validations.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e0d6a54

Please sign in to comment.