github.com/cosmos/cosmos-sdk@v0.50.10/client/prompt_validation.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"unicode"
     7  
     8  	sdk "github.com/cosmos/cosmos-sdk/types"
     9  )
    10  
    11  // ValidatePromptNotEmpty validates that the input is not empty.
    12  func ValidatePromptNotEmpty(input string) error {
    13  	if input == "" {
    14  		return fmt.Errorf("input cannot be empty")
    15  	}
    16  
    17  	return nil
    18  }
    19  
    20  // ValidatePromptURL validates that the input is a valid URL.
    21  func ValidatePromptURL(input string) error {
    22  	_, err := url.ParseRequestURI(input)
    23  	if err != nil {
    24  		return fmt.Errorf("invalid URL: %w", err)
    25  	}
    26  
    27  	return nil
    28  }
    29  
    30  // ValidatePromptAddress validates that the input is a valid Bech32 address.
    31  func ValidatePromptAddress(input string) error {
    32  	_, err := sdk.AccAddressFromBech32(input)
    33  	if err == nil {
    34  		return nil
    35  	}
    36  
    37  	_, err = sdk.ValAddressFromBech32(input)
    38  	if err == nil {
    39  		return nil
    40  	}
    41  
    42  	_, err = sdk.ConsAddressFromBech32(input)
    43  	if err == nil {
    44  		return nil
    45  	}
    46  
    47  	return fmt.Errorf("invalid address: %w", err)
    48  }
    49  
    50  // ValidatePromptYesNo validates that the input is valid sdk.COins
    51  func ValidatePromptCoins(input string) error {
    52  	if _, err := sdk.ParseCoinsNormalized(input); err != nil {
    53  		return fmt.Errorf("invalid coins: %w", err)
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  // CamelCaseToString converts a camel case string to a string with spaces.
    60  func CamelCaseToString(str string) string {
    61  	w := []rune(str)
    62  	for i := len(w) - 1; i > 1; i-- {
    63  		if unicode.IsUpper(w[i]) {
    64  			w = append(w[:i], append([]rune{' '}, w[i:]...)...)
    65  		}
    66  	}
    67  	return string(w)
    68  }