-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19c4dff
commit 77c298d
Showing
13 changed files
with
181 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package prompt | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"unicode" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// ValidatePromptNotEmpty validates that the input is not empty. | ||
func ValidatePromptNotEmpty(input string) error { | ||
if input == "" { | ||
return fmt.Errorf("input cannot be empty") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ValidatePromptURL validates that the input is a valid URL. | ||
func ValidatePromptURL(input string) error { | ||
_, err := url.ParseRequestURI(input) | ||
if err != nil { | ||
return fmt.Errorf("invalid URL: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ValidatePromptAddress validates that the input is a valid Bech32 address. | ||
func ValidatePromptAddress(input string) error { // TODO(@julienrbrt) remove and add prompts in AutoCLI | ||
_, err := sdk.AccAddressFromBech32(input) | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
_, err = sdk.ValAddressFromBech32(input) | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
_, err = sdk.ConsAddressFromBech32(input) | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("invalid address: %w", err) | ||
} | ||
|
||
// ValidatePromptYesNo validates that the input is valid sdk.COins | ||
func ValidatePromptCoins(input string) error { | ||
if _, err := sdk.ParseCoinsNormalized(input); err != nil { | ||
return fmt.Errorf("invalid coins: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// CamelCaseToString converts a camel case string to a string with spaces. | ||
func CamelCaseToString(str string) string { | ||
w := []rune(str) | ||
for i := len(w) - 1; i > 1; i-- { | ||
if unicode.IsUpper(w[i]) { | ||
w = append(w[:i], append([]rune{' '}, w[i:]...)...) | ||
} | ||
} | ||
return string(w) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package prompt_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"cosmossdk.io/client/v2/internal/prompt" | ||
) | ||
|
||
func TestValidatePromptNotEmpty(t *testing.T) { | ||
require := require.New(t) | ||
|
||
require.NoError(prompt.ValidatePromptNotEmpty("foo")) | ||
require.ErrorContains(prompt.ValidatePromptNotEmpty(""), "input cannot be empty") | ||
} | ||
|
||
func TestValidatePromptURL(t *testing.T) { | ||
require := require.New(t) | ||
|
||
require.NoError(prompt.ValidatePromptURL("https://example.com")) | ||
require.ErrorContains(prompt.ValidatePromptURL("foo"), "invalid URL") | ||
} | ||
|
||
func TestValidatePromptAddress(t *testing.T) { | ||
require := require.New(t) | ||
|
||
require.NoError(prompt.ValidatePromptAddress("cosmos1huydeevpz37sd9snkgul6070mstupukw00xkw9")) | ||
require.NoError(prompt.ValidatePromptAddress("cosmosvaloper1sjllsnramtg3ewxqwwrwjxfgc4n4ef9u2lcnj0")) | ||
require.NoError(prompt.ValidatePromptAddress("cosmosvalcons1ntk8eualewuprz0gamh8hnvcem2nrcdsgz563h")) | ||
require.ErrorContains(prompt.ValidatePromptAddress("foo"), "invalid address") | ||
} | ||
|
||
func TestValidatePromptCoins(t *testing.T) { | ||
require := require.New(t) | ||
|
||
require.NoError(prompt.ValidatePromptCoins("100stake")) | ||
require.ErrorContains(prompt.ValidatePromptCoins("foo"), "invalid coins") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters