Skip to content

Commit

Permalink
Refactor validation in auxiliaryRequest
Browse files Browse the repository at this point in the history
This commit replaces the govalidator usage for auxiliaryRequest validation with custom error checking. It also removes unnecessary `valid` struct tags and enhances error messages. Now validation checks for specifics: the validity of both owner ids, asset ids and makes sure that value is greater than zero.
  • Loading branch information
deepanshutr committed Apr 12, 2024
1 parent 44b61e6 commit ac6d5e6
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions x/splits/auxiliaries/burn/auxiliary_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,35 @@
package burn

import (
errorConstants "github.com/AssetMantle/schema/go/errors/constants"
"github.com/AssetMantle/schema/go/ids"
"github.com/asaskevich/govalidator"
sdkTypes "github.com/cosmos/cosmos-sdk/types"

"github.com/AssetMantle/modules/helpers"
)

type auxiliaryRequest struct {
OwnerID ids.IdentityID `json:"ownerID" valid:"required~required field ownerID missing"`
ids.AssetID `json:"assetID" valid:"required~required field assetId missing"`
Value sdkTypes.Int `json:"value" valid:"required~required field value missing"`
OwnerID ids.IdentityID
ids.AssetID
Value sdkTypes.Int
}

var _ helpers.AuxiliaryRequest = (*auxiliaryRequest)(nil)

func (auxiliaryRequest auxiliaryRequest) Validate() error {
_, err := govalidator.ValidateStruct(auxiliaryRequest)
return err
}
if err := auxiliaryRequest.OwnerID.ValidateBasic(); err != nil {
return errorConstants.InvalidRequest.Wrapf("invalid owner ID: %s", err)
}

func auxiliaryRequestFromInterface(request helpers.AuxiliaryRequest) auxiliaryRequest {
switch value := request.(type) {
case auxiliaryRequest:
return value
default:
return auxiliaryRequest{}
if err := auxiliaryRequest.AssetID.ValidateBasic(); err != nil {
return errorConstants.InvalidRequest.Wrapf("invalid asset ID: %s", err)
}

if auxiliaryRequest.Value.LTE(sdkTypes.ZeroInt()) {
return errorConstants.InvalidRequest.Wrapf("value must be greater than zero")
}

return nil
}

func NewAuxiliaryRequest(ownerID ids.IdentityID, assetID ids.AssetID, value sdkTypes.Int) helpers.AuxiliaryRequest {
Expand Down

0 comments on commit ac6d5e6

Please sign in to comment.