Skip to content

Commit

Permalink
move client validation
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano committed Dec 6, 2024
1 parent ab16339 commit f4c80a6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 46 deletions.
28 changes: 0 additions & 28 deletions client/prompt_validation.go

This file was deleted.

16 changes: 0 additions & 16 deletions client/prompt_validation_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion client/v2/autocli/prompt/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func valueOf(field protoreflect.FieldDescriptor, result string) (protoreflect.Va
}
}

// valueOf prompts the user for a comma-separated list of values for a repeated field.
// promptList prompts the user for a comma-separated list of values for a repeated field.
// The user will be prompted to enter values separated by commas which will be parsed
// according to the field's type using valueOf.
func promptList(field protoreflect.FieldDescriptor, msg protoreflect.Message, promptUi promptui.Prompt, promptPrefix string) (protoreflect.List, error) {
Expand Down
23 changes: 23 additions & 0 deletions client/v2/autocli/prompt/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package prompt
import (
"errors"
"fmt"
"net/url"
"unicode"

"cosmossdk.io/core/address"
)
Expand All @@ -27,3 +29,24 @@ func ValidateAddress(ac address.Codec) func(string) error {
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
}

// 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)
}
8 changes: 7 additions & 1 deletion client/v2/autocli/prompt/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package prompt
import (
"testing"


"github.com/stretchr/testify/require"

"cosmossdk.io/core/address"
Expand Down Expand Up @@ -47,3 +46,10 @@ func TestValidateAddress(t *testing.T) {
})
}
}

func TestValidatePromptURL(t *testing.T) {
require := require.New(t)

require.NoError(ValidatePromptURL("https://example.com"))
require.ErrorContains(ValidatePromptURL("foo"), "invalid URL")
}

0 comments on commit f4c80a6

Please sign in to comment.