Skip to content

Commit

Permalink
multivalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
mkysel committed Dec 4, 2024
1 parent a958a13 commit 6807c79
Showing 1 changed file with 56 additions and 5 deletions.
61 changes: 56 additions & 5 deletions pkg/config/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,85 @@ package config

import (
"errors"
"strings"
)

func ValidateServerOptions(options ServerOptions) error {
missingSet := make(map[string]struct{})
customSet := make(map[string]struct{})

if options.Contracts.RpcUrl == "" {
missingSet["--contracts.rpc-url"] = struct{}{}
}

if options.Contracts.NodesContractAddress == "" {
missingSet["--contracts.nodes-address"] = struct{}{}
}

if options.Contracts.MessagesContractAddress == "" {
missingSet["--contracts.messages-address"] = struct{}{}
}

if options.Contracts.IdentityUpdatesContractAddress == "" {
missingSet["--contracts.identity-updates-address"] = struct{}{}
}

if options.Contracts.ChainID == 0 {
customSet["--contracts.chain-id must be greater than 0"] = struct{}{}
}

if options.Contracts.RefreshInterval <= 0 {
customSet["--contracts.refresh-interval must be greater than 0"] = struct{}{}
}

if options.Contracts.MaxChainDisconnectTime <= 0 {
customSet["--contracts.max-chain-disconnect-time must be greater than 0"] = struct{}{}
}

if options.Payer.Enable {
if options.Payer.PrivateKey == "" {
return errors.New("payer.PrivateKey is required")
missingSet["--payer.PrivateKey"] = struct{}{}
}
}

if options.Replication.Enable {
if options.DB.WriterConnectionString == "" {
return errors.New("DB.WriterConnectionString is required")
missingSet["--DB.WriterConnectionString"] = struct{}{}
}
if options.Signer.PrivateKey == "" {
return errors.New("Signer.PrivateKey is required")
missingSet["--Signer.PrivateKey"] = struct{}{}
}
}

if options.Sync.Enable {
if options.DB.WriterConnectionString == "" {
return errors.New("DB.WriterConnectionString is required")
missingSet["--DB.WriterConnectionString"] = struct{}{}
}
}

if options.Indexer.Enable {
if options.DB.WriterConnectionString == "" {
return errors.New("DB.WriterConnectionString is required")
missingSet["--DB.WriterConnectionString"] = struct{}{}
}
}

if len(missingSet) > 0 || len(customSet) > 0 {
var errs []string
if len(missingSet) > 0 {

var errorMessages []string
errorMessages = append(errorMessages, "Missing required arguments:")
for err := range missingSet {
errorMessages = append(errorMessages, err)
}
errs = append(errs, strings.Join(errorMessages, ", "))
}
if len(customSet) > 0 {
for err := range customSet {
errs = append(errs, err)
}
}
return errors.New(strings.Join(errs, "; "))
}

return nil
Expand Down

0 comments on commit 6807c79

Please sign in to comment.