Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check for spaces in collection names
- Loading branch information
Showing
6 changed files
with
122 additions
and
12 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
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,22 @@ | ||
package mongoutils | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
var ( | ||
ErrMissingCollectionName = errors.New("missing collection name") | ||
ErrInvalidCharInCollectionName = errors.New("invalid char in collection name (space)") | ||
) | ||
|
||
// CheckCollectionName checks if a collection name is valid for mongodb. | ||
// We only check on spaces because they are hard to see by humans. | ||
func CheckCollectionName(name string) error { | ||
if name == "" { | ||
return ErrMissingCollectionName | ||
} else if strings.ContainsAny(name, " ") { | ||
return ErrInvalidCharInCollectionName | ||
} | ||
return nil | ||
} |
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,45 @@ | ||
package mongoutils | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestCheckValidCollectionName(t *testing.T) { | ||
type args struct { | ||
name string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
"empty name", | ||
args{ | ||
name: "", | ||
}, | ||
true, | ||
}, | ||
{ | ||
"valid name", | ||
args{ | ||
name: "valid", | ||
}, | ||
false, | ||
}, | ||
{ | ||
"with spaces", | ||
args{ | ||
name: "invalid name", | ||
}, | ||
true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := CheckCollectionName(tt.args.name); (err != nil) != tt.wantErr { | ||
t.Errorf("CheckCollectionName() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
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