Skip to content

Commit 836553b

Browse files
authored
Validate the given project ID is valid (#149)
1 parent dc9ed2c commit 836553b

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

internal/cli/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func (opts *configOpts) Run() error {
9393
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",
9494
Default: config.ProjectID(),
9595
},
96+
Validate: validate.OptionalObjectID,
9697
},
9798
}
9899

internal/validate/validate.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,49 @@ import (
2424
"github.com/mongodb/mongocli/internal/config"
2525
)
2626

27-
func URL(val interface{}) error {
27+
// toString tires to cast an interface to string
28+
func toString(val interface{}) (string, error) {
2829
var u string
2930
var ok bool
3031
if u, ok = val.(string); !ok {
31-
return fmt.Errorf("'%v' is not a valid URL", val)
32+
return "", fmt.Errorf("'%v' is not valid", val)
33+
}
34+
return u, nil
35+
}
36+
37+
// URL validates a value is a valid URL for the cli store
38+
func URL(val interface{}) error {
39+
s, err := toString(val)
40+
if err != nil {
41+
return err
3242
}
33-
if !strings.HasSuffix(u, "/") {
34-
return fmt.Errorf("'%s' must have a trailing slash", u)
43+
if !strings.HasSuffix(s, "/") {
44+
return fmt.Errorf("'%s' must have a trailing slash", s)
3545
}
36-
_, err := url.ParseRequestURI(u)
46+
_, err = url.ParseRequestURI(s)
3747
if err != nil {
38-
return fmt.Errorf("'%s' is not a valid URL", u)
48+
return fmt.Errorf("'%s' is not a valid URL", s)
3949
}
4050

4151
return nil
4252
}
4353

54+
// OptionalObjectID validates a value is a valid ObjectID
55+
func OptionalObjectID(val interface{}) error {
56+
if val == nil {
57+
return nil
58+
}
59+
s, err := toString(val)
60+
if err != nil {
61+
return err
62+
}
63+
if s == "" {
64+
return nil
65+
}
66+
return ObjectID(s)
67+
}
68+
69+
// ObjectID validates a value is a valid ObjectID
4470
func ObjectID(s string) error {
4571
b, err := hex.DecodeString(s)
4672
if err != nil || len(b) != 12 {
@@ -49,6 +75,7 @@ func ObjectID(s string) error {
4975
return nil
5076
}
5177

78+
// Credentials validates public and private API keys have been set
5279
func Credentials() error {
5380
if config.PrivateAPIKey() == "" || config.PublicAPIKey() == "" {
5481
return errors.New("missing credentials")

internal/validate/validate_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,39 @@ func TestURL(t *testing.T) {
4242
})
4343
}
4444

45+
func TestOptionalObjectID(t *testing.T) {
46+
t.Run("Empty value", func(t *testing.T) {
47+
err := OptionalObjectID("")
48+
if err != nil {
49+
t.Fatalf("OptionalObjectID() unexpected error %v\n", err)
50+
}
51+
})
52+
t.Run("nil value", func(t *testing.T) {
53+
err := OptionalObjectID(nil)
54+
if err != nil {
55+
t.Fatalf("OptionalObjectID() unexpected error %v\n", err)
56+
}
57+
})
58+
t.Run("Valid ObjectID", func(t *testing.T) {
59+
err := OptionalObjectID("5e9f088b4797476aa0a5d56a")
60+
if err != nil {
61+
t.Fatalf("OptionalObjectID() unexpected error %v\n", err)
62+
}
63+
})
64+
t.Run("Short ObjectID", func(t *testing.T) {
65+
err := OptionalObjectID("5e9f088b4797476aa0a5d56")
66+
if err == nil {
67+
t.Fatal("OptionalObjectID() expected an error\n")
68+
}
69+
})
70+
t.Run("Invalid ObjectID", func(t *testing.T) {
71+
err := OptionalObjectID("5e9f088b4797476aa0a5d56z")
72+
if err == nil {
73+
t.Fatal("OptionalObjectID() expected an error\n")
74+
}
75+
})
76+
}
77+
4578
func TestObjectID(t *testing.T) {
4679
t.Run("Valid ObjectID", func(t *testing.T) {
4780
err := ObjectID("5e9f088b4797476aa0a5d56a")

0 commit comments

Comments
 (0)