Skip to content

Commit

Permalink
Improved validation of provider types
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Jul 11, 2021
1 parent 63d6cce commit 10511d1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 50 deletions.
22 changes: 11 additions & 11 deletions x/provider/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
)

var (
ErrorMarshal = errors.Register(ModuleName, 101, "error occurred while marshalling")
ErrorUnmarshal = errors.Register(ModuleName, 102, "error occurred while unmarshalling")
ErrorUnknownMsgType = errors.Register(ModuleName, 103, "unknown message type")
ErrorUnknownQueryType = errors.Register(ModuleName, 104, "unknown query type")
ErrorInvalidFieldName = errors.Register(ModuleName, 105, "invalid field name; expected length between 1 and 64")
ErrorDuplicateProvider = errors.Register(ModuleName, 106, "duplicate provider")
ErrorProviderDoesNotExist = errors.Register(ModuleName, 107, "provider does not exist")
ErrorInvalidFieldFrom = errors.Register(ModuleName, 108, "invalid field from; expected a valid address")
ErrorInvalidFieldIdentity = errors.Register(ModuleName, 109, "invalid field identity; expected length between 0 and 64")
ErrorInvalidFieldWebsite = errors.Register(ModuleName, 110, "invalid field website; expected length between 0 and 64")
ErrorInvalidFieldDescription = errors.Register(ModuleName, 111, "invalid field description; expected length between 0 and 256")
ErrorInvalidField = errors.Register(ModuleName, 101, "invalid field")
ErrorInvalidFrom = errors.Register(ModuleName, 102, "invalid from")
ErrorInvalidName = errors.Register(ModuleName, 103, "invalid name")
ErrorInvalidIdentity = errors.Register(ModuleName, 104, "invalid identity")
ErrorInvalidWebsite = errors.Register(ModuleName, 105, "invalid website")
ErrorInvalidDescription = errors.Register(ModuleName, 106, "invalid description")
)

var (
ErrorDuplicateProvider = errors.Register(ModuleName, 201, "duplicate provider")
ErrorProviderDoesNotExist = errors.Register(ModuleName, 202, "provider does not exist")
)
63 changes: 34 additions & 29 deletions x/provider/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package types

import (
"fmt"
"net/url"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"

hubtypes "github.com/sentinel-official/hub/types"
)
Expand Down Expand Up @@ -32,28 +34,31 @@ func (m *MsgRegisterRequest) Type() string {
}

func (m *MsgRegisterRequest) ValidateBasic() error {
if m.From == "" {
return errors.Wrap(ErrorInvalidFrom, "from cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(m.From); err != nil {
return ErrorInvalidFieldFrom
return errors.Wrapf(ErrorInvalidFrom, "%s", err)
}

// Name length should be between 1 and 64
if len(m.Name) == 0 || len(m.Name) > 64 {
return ErrorInvalidFieldName
if m.Name == "" {
return errors.Wrap(ErrorInvalidName, "name cannot be empty")
}
if len(m.Name) > 64 {
return errors.Wrapf(ErrorInvalidName, "name length cannot be greater than %d", 64)
}

// Identity length should be between 0 and 64
if len(m.Identity) > 64 {
return ErrorInvalidFieldIdentity
return errors.Wrapf(ErrorInvalidIdentity, "identity length cannot be greater than %d", 64)
}

// Website length should be between 0 and 64
if len(m.Website) > 64 {
return ErrorInvalidFieldWebsite
if m.Website != "" {
if len(m.Website) > 64 {
return errors.Wrapf(ErrorInvalidWebsite, "website length cannot be greater than %d", 64)
}
if _, err := url.ParseRequestURI(m.Website); err != nil {
return errors.Wrapf(ErrorInvalidWebsite, "%s", err)
}
}

// Description length should be between 0 and 256
if len(m.Description) > 256 {
return ErrorInvalidFieldDescription
return errors.Wrapf(ErrorInvalidDescription, "description length cannot be greater than %d", 256)
}

return nil
Expand Down Expand Up @@ -91,28 +96,28 @@ func (m *MsgUpdateRequest) Type() string {
}

func (m MsgUpdateRequest) ValidateBasic() error {
if m.From == "" {
return errors.Wrap(ErrorInvalidFrom, "from cannot be empty")
}
if _, err := hubtypes.ProvAddressFromBech32(m.From); err != nil {
return ErrorInvalidFieldFrom
return errors.Wrapf(ErrorInvalidFrom, "%s", err)
}

// Name length should be between 0 and 64
if len(m.Name) > 64 {
return ErrorInvalidFieldName
return errors.Wrapf(ErrorInvalidName, "name length cannot be greater than %d", 64)
}

// Identity length should be between 0 and 64
if len(m.Identity) > 64 {
return ErrorInvalidFieldIdentity
return errors.Wrapf(ErrorInvalidIdentity, "identity length cannot be greater than %d", 64)
}

// Website length should be between 0 and 64
if len(m.Website) > 64 {
return ErrorInvalidFieldWebsite
if m.Website != "" {
if len(m.Website) > 64 {
return errors.Wrapf(ErrorInvalidWebsite, "website length cannot be greater than %d", 64)
}
if _, err := url.ParseRequestURI(m.Website); err != nil {
return errors.Wrapf(ErrorInvalidWebsite, "%s", err)
}
}

// Description length should be between 0 and 256
if len(m.Description) > 256 {
return ErrorInvalidFieldDescription
return errors.Wrapf(ErrorInvalidDescription, "description length cannot be greater than %d", 256)
}

return nil
Expand Down
19 changes: 17 additions & 2 deletions x/provider/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ var (
)

func (p *Params) Validate() error {
if p.Deposit.IsNegative() {
return fmt.Errorf("deposit cannot be negative")
}
if !p.Deposit.IsValid() {
return fmt.Errorf("deposit should be valid")
return fmt.Errorf("invalid deposit %s", p.Deposit)
}

return nil
Expand All @@ -32,7 +35,19 @@ func (p *Params) ParamSetPairs() params.ParamSetPairs {
{
Key: KeyDeposit,
Value: &p.Deposit,
ValidatorFn: func(_ interface{}) error {
ValidatorFn: func(v interface{}) error {
value, ok := v.(sdk.Coin)
if !ok {
return fmt.Errorf("invalid parameter type %T", v)
}

if value.IsNegative() {
return fmt.Errorf("deposit cannot be negative")
}
if !value.IsValid() {
return fmt.Errorf("invalid deposit %s", value)
}

return nil
},
},
Expand Down
32 changes: 24 additions & 8 deletions x/provider/types/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package types

import (
"fmt"
"net/url"

"github.com/cosmos/cosmos-sdk/types/errors"

hubtypes "github.com/sentinel-official/hub/types"
)
Expand All @@ -20,23 +23,36 @@ func (p *Provider) GetAddress() hubtypes.ProvAddress {
}

func (p *Provider) Validate() error {
if p.Address == "" {
return fmt.Errorf("address cannot be empty")
}
if _, err := hubtypes.ProvAddressFromBech32(p.Address); err != nil {
return fmt.Errorf("address should not be nil or empty")
return errors.Wrapf(err, "invalid address %s", p.Address)
}
if len(p.Name) == 0 || len(p.Name) > 64 {
return fmt.Errorf("name length should be between 1 and 64")
if p.Name == "" {
return fmt.Errorf("name cannot be empty")
}
if len(p.Name) > 64 {
return fmt.Errorf("name length cannot be greater than %d", 64)
}
if len(p.Identity) > 64 {
return fmt.Errorf("identity length should be between 0 and 64")
return fmt.Errorf("identity length cannot be greater than %d", 64)
}
if len(p.Website) > 64 {
return fmt.Errorf("website length should be between 0 and 64")
if p.Website != "" {
if len(p.Website) > 64 {
return fmt.Errorf("website length cannot be greater than %d", 64)
}
if _, err := url.ParseRequestURI(p.Website); err != nil {
return errors.Wrapf(err, "invalid website %s", p.Website)
}
}
if len(p.Description) > 256 {
return fmt.Errorf("description length should be between 0 and 256")
return fmt.Errorf("description length cannot be greater than %d", 256)
}

return nil
}

type Providers []Provider
type (
Providers []Provider
)

0 comments on commit 10511d1

Please sign in to comment.