Skip to content

Validate the given project ID is valid #149

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

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (opts *configOpts) Run() error {
Help: "This is the ID of an existing project your API keys have access to, you can leave this blank and specify it on every command with --projectId",
Default: config.ProjectID(),
},
Validate: validate.OptionalObjectID,
},
}

Expand Down
39 changes: 33 additions & 6 deletions internal/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,49 @@ import (
"github.com/mongodb/mongocli/internal/config"
)

func URL(val interface{}) error {
// toString tires to cast an interface to string
func toString(val interface{}) (string, error) {
var u string
var ok bool
if u, ok = val.(string); !ok {
return fmt.Errorf("'%v' is not a valid URL", val)
return "", fmt.Errorf("'%v' is not valid", val)
}
return u, nil
}

// URL validates a value is a valid URL for the cli store
func URL(val interface{}) error {
s, err := toString(val)
if err != nil {
return err
}
if !strings.HasSuffix(u, "/") {
return fmt.Errorf("'%s' must have a trailing slash", u)
if !strings.HasSuffix(s, "/") {
return fmt.Errorf("'%s' must have a trailing slash", s)
}
_, err := url.ParseRequestURI(u)
_, err = url.ParseRequestURI(s)
if err != nil {
return fmt.Errorf("'%s' is not a valid URL", u)
return fmt.Errorf("'%s' is not a valid URL", s)
}

return nil
}

// OptionalObjectID validates a value is a valid ObjectID
func OptionalObjectID(val interface{}) error {
if val == nil {
return nil
}
s, err := toString(val)
if err != nil {
return err
}
if s == "" {
return nil
}
return ObjectID(s)
}

// ObjectID validates a value is a valid ObjectID
func ObjectID(s string) error {
b, err := hex.DecodeString(s)
if err != nil || len(b) != 12 {
Expand All @@ -49,6 +75,7 @@ func ObjectID(s string) error {
return nil
}

// Credentials validates public and private API keys have been set
func Credentials() error {
if config.PrivateAPIKey() == "" || config.PublicAPIKey() == "" {
return errors.New("missing credentials")
Expand Down
33 changes: 33 additions & 0 deletions internal/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ func TestURL(t *testing.T) {
})
}

func TestOptionalObjectID(t *testing.T) {
t.Run("Empty value", func(t *testing.T) {
err := OptionalObjectID("")
if err != nil {
t.Fatalf("OptionalObjectID() unexpected error %v\n", err)
}
})
t.Run("nil value", func(t *testing.T) {
err := OptionalObjectID(nil)
if err != nil {
t.Fatalf("OptionalObjectID() unexpected error %v\n", err)
}
})
t.Run("Valid ObjectID", func(t *testing.T) {
err := OptionalObjectID("5e9f088b4797476aa0a5d56a")
if err != nil {
t.Fatalf("OptionalObjectID() unexpected error %v\n", err)
}
})
t.Run("Short ObjectID", func(t *testing.T) {
err := OptionalObjectID("5e9f088b4797476aa0a5d56")
if err == nil {
t.Fatal("OptionalObjectID() expected an error\n")
}
})
t.Run("Invalid ObjectID", func(t *testing.T) {
err := OptionalObjectID("5e9f088b4797476aa0a5d56z")
if err == nil {
t.Fatal("OptionalObjectID() expected an error\n")
}
})
}

func TestObjectID(t *testing.T) {
t.Run("Valid ObjectID", func(t *testing.T) {
err := ObjectID("5e9f088b4797476aa0a5d56a")
Expand Down