Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model validation #39

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
14 changes: 9 additions & 5 deletions model/client/account/account_channels_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package account

import (
"encoding/json"
"errors"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand All @@ -21,10 +21,14 @@ func (*AccountChannelsRequest) Method() string {
return "account_channels"
}

// Validate method to be added to each request struct
func (a *AccountChannelsRequest) Validate() error {
if a.Account == "" {
return errors.New("no account ID specified")
func (r *AccountChannelsRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the validate method there is still a
// TODO checksum
does that need adding still?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I have not added any intricacies for checking valid values for thinks like addresses, keys or object IDs with encoded fields

return fmt.Errorf("account channels request: %w", err)
}
if len(r.DestinationAccount) > 0 {
if err := r.DestinationAccount.Validate(); err != nil {
return fmt.Errorf("account channels request: %w", err)
}
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion model/client/account/account_channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ func TestValidate(t *testing.T) {

err := s.Validate()

assert.EqualError(t, err, "no account ID specified")
assert.ErrorContains(t, err, "missing xrpl address")
}
9 changes: 9 additions & 0 deletions model/client/account/account_currencies_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"encoding/json"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand All @@ -18,6 +19,14 @@ func (*AccountCurrenciesRequest) Method() string {
return "account_currencies"
}

func (r *AccountCurrenciesRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return fmt.Errorf("account currencies request: %w", err)
}

return nil
}

func (r *AccountCurrenciesRequest) UnmarshalJSON(data []byte) error {
type acrHelper struct {
Account types.Address `json:"account"`
Expand Down
11 changes: 11 additions & 0 deletions model/client/account/account_currencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package account
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/test"
)
Expand Down Expand Up @@ -55,3 +56,13 @@ func TestAccountCurrenciesResponse(t *testing.T) {
t.Error(err)
}
}

func TestAccountCurrenciesValidation(t *testing.T) {
s := AccountCurrenciesRequest{
Account: "",
}
err := s.Validate()

assert.ErrorContains(t, err, "missing xrpl address")

}
7 changes: 6 additions & 1 deletion model/client/account/account_info_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"encoding/json"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand All @@ -20,7 +21,11 @@ func (*AccountInfoRequest) Method() string {
return "account_info"
}

func (*AccountInfoRequest) Validate() error {
func (r *AccountInfoRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return fmt.Errorf("account info request: %w", err)
}

return nil
}

Expand Down
11 changes: 11 additions & 0 deletions model/client/account/account_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package account
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/ledger"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand Down Expand Up @@ -110,3 +111,13 @@ func TestAccountInfoResponse(t *testing.T) {
t.Error(err)
}
}

func TestAccountInfoValidate(t *testing.T) {
s := AccountInfoRequest{
Account: "",
}

err := s.Validate()

assert.ErrorContains(t, err, "missing xrpl address")
}
12 changes: 12 additions & 0 deletions model/client/account/account_lines_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"encoding/json"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand All @@ -20,6 +21,17 @@ func (*AccountLinesRequest) Method() string {
return "account_lines"
}

func (r *AccountLinesRequest) Validate() error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to also run the account.Validate on the 'peer' parameter if it is present

if err := r.Account.Validate(); err != nil {
return fmt.Errorf("account lines request: %w", err)
}

if r.Limit != 0 && (r.Limit < 10 || r.Limit > 400) {
return fmt.Errorf("account lines request: invalid limit, must be 10 <= limit <= 400")
}

return nil
}
func (r *AccountLinesRequest) UnmarshalJSON(data []byte) error {
type alrHelper struct {
Account types.Address `json:"account"`
Expand Down
17 changes: 17 additions & 0 deletions model/client/account/account_lines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package account
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/test"
)
Expand Down Expand Up @@ -74,3 +75,19 @@ func TestAccountLinesResponse(t *testing.T) {
t.Error(err)
}
}

func TestAccountLinesValidate(t *testing.T) {
s := AccountLinesRequest{
Account: "",
}

err := s.Validate()

assert.ErrorContains(t, err, "missing xrpl address")

s.Account = "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59"
s.Limit = 2
err = s.Validate()

assert.ErrorContains(t, err, "invalid limit")
}
13 changes: 13 additions & 0 deletions model/client/account/account_nfts_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"encoding/json"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand All @@ -19,6 +20,18 @@ func (*AccountNFTsRequest) Method() string {
return "account_nfts"
}

func (r *AccountNFTsRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return fmt.Errorf("account nfts request: %w", err)
}

if r.Limit != 0 && (r.Limit < 20 || r.Limit > 400) {
return fmt.Errorf("account nfts request: invalid limit, must be 20 <= limit <= 400")
}

return nil
}

func (r *AccountNFTsRequest) UnmarshalJSON(data []byte) error {
type anrHelper struct {
Account types.Address `json:"account"`
Expand Down
13 changes: 13 additions & 0 deletions model/client/account/account_objects_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"encoding/json"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand Down Expand Up @@ -35,6 +36,18 @@ func (*AccountObjectsRequest) Method() string {
return "account_objects"
}

func (r *AccountObjectsRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return fmt.Errorf("account objects request: %w", err)
}

if r.Limit != 0 && (r.Limit < 10 || r.Limit > 400) {
return fmt.Errorf("account objects request: invalid limit, must be 10 <= limit <= 400")
}

return nil
}

func (r *AccountObjectsRequest) UnmarshalJSON(data []byte) error {
type aorHelper struct {
Account types.Address `json:"account"`
Expand Down
13 changes: 13 additions & 0 deletions model/client/account/account_offers_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"encoding/json"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand All @@ -20,6 +21,18 @@ func (*AccountOffersRequest) Method() string {
return "account_offers"
}

func (r *AccountOffersRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return fmt.Errorf("account offers request: %w", err)
}

if r.Limit != 0 && (r.Limit < 10 || r.Limit > 400) {
return fmt.Errorf("account offers request: invalid limit, must be 10 <= limit <= 400")
}

return nil
}

func (r *AccountOffersRequest) UnmarshalJSON(data []byte) error {
type aorHelper struct {
Account types.Address `json:"account"`
Expand Down
13 changes: 13 additions & 0 deletions model/client/account/account_transactions_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"encoding/json"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand All @@ -23,6 +24,18 @@ func (*AccountTransactionsRequest) Method() string {
return "account_tx"
}

func (r *AccountTransactionsRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return fmt.Errorf("account transactions request: %w", err)
}

if r.Limit != 0 && (r.Limit < 10 || r.Limit > 400) {
return fmt.Errorf("account transactions request: invalid limit, must be 10 <= limit <= 400")
}

return nil
}

func (r *AccountTransactionsRequest) UnmarshalJSON(data []byte) error {
type atrHelper struct {
Account types.Address `json:"account"`
Expand Down
4 changes: 4 additions & 0 deletions model/client/admin/data/can_delete_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func (*CanDeleteRequest) Method() string {
return "can_delete"
}

func (*CanDeleteRequest) Validate() error {
return nil
}

func (r *CanDeleteRequest) UnmarshalJSON(data []byte) error {
type cdHelper struct {
CanDelete json.RawMessage `json:"can_delete"`
Expand Down
10 changes: 10 additions & 0 deletions model/client/admin/data/crawl_shards_request.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package data

import "fmt"

type CrawlShardsRequest struct {
PublicKey bool `json:"public_key,omitempty"`
Limit int `json:"limit,omitempty"`
Expand All @@ -8,3 +10,11 @@ type CrawlShardsRequest struct {
func (*CrawlShardsRequest) Method() string {
return "crawl_shards"
}

func (r *CrawlShardsRequest) Validate() error {
if r.Limit < 0 || r.Limit > 3 {
return fmt.Errorf("crawl shards request: invalid limit, must be 0 <= limit <= 3")
}

return nil
}
4 changes: 4 additions & 0 deletions model/client/admin/data/download_shard_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ type ShardDescriptor struct {
func (*DownloadShardRequest) Method() string {
return "download_shard"
}

func (*DownloadShardRequest) Validate() error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we assert on shards not being nil? Bit stupid if user does send empty tho

return nil
}
4 changes: 4 additions & 0 deletions model/client/admin/data/ledger_cleaner_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ type LedgerCleanerRequest struct {
func (*LedgerCleanerRequest) Method() string {
return "ledger_cleaner"
}

func (*LedgerCleanerRequest) Validate() error {
return nil
}
4 changes: 4 additions & 0 deletions model/client/admin/data/ledger_request_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ type LedgerRequestRequest struct {
func (*LedgerRequest) Method() string {
return "ledger_request"
}

func (*LedgerRequest) Validate() error {
return nil
}
19 changes: 17 additions & 2 deletions model/client/admin/data/log_level_request.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package data

const (
Fatal LogSeverity = "fatal"
Error LogSeverity = "error"
Warn LogSeverity = "warn"
Info LogSeverity = "info"
Debug LogSeverity = "debug"
Trace LogSeverity = "trace"
)

type LogSeverity string

type LogLevelRequest struct {
Severity string `json:"severity,omitempty"`
Partition string `json:"partition,omitempty"`
Severity LogSeverity `json:"severity,omitempty"`
Partition string `json:"partition,omitempty"`
}

func (*LogLevelRequest) Method() string {
return "log_level"
}

func (*LogLevelRequest) Validate() error {
return nil
}
4 changes: 4 additions & 0 deletions model/client/admin/data/logrotate_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ type LogrotateRequest struct {
func (*LogrotateRequest) Method() string {
return "logrotate"
}

func (*LogrotateRequest) Validate() error {
return nil
}
10 changes: 10 additions & 0 deletions model/client/admin/data/node_to_shard_request.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package data

import "fmt"

type NodeToShardRequest struct {
Action string `json:"action"`
}

func (*NodeToShardRequest) Method() string {
return "node_to_shard"
}

func (r *NodeToShardRequest) Validate() error {
if r.Action != "start" && r.Action != "stop" && r.Action != "status" {
return fmt.Errorf("node to shard request: invalid action '%s'", r.Action)
}

return nil
}
Loading
Loading