Skip to content

Commit

Permalink
collections: ensure URL-safe collection names (#340)
Browse files Browse the repository at this point in the history
Signed-off-by: Sander Pick <[email protected]>
  • Loading branch information
sanderpick authored May 7, 2020
1 parent d4f1723 commit 838f09c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type Collection struct {
}

func newCollection(name string, schema *jsonschema.Schema, d *DB) (*Collection, error) {
if name != "" && !collectionNameRx.MatchString(name) {
return nil, ErrInvalidCollectionName
}

// by default, use top level properties to validate ID string property exists
properties := schema.Properties
if schema.Ref != "" {
Expand Down
14 changes: 12 additions & 2 deletions db/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ func TestNewCollection(t *testing.T) {
t.Fatal("the collection should be invalid")
}
})
t.Run("Fail/InvalidName", func(t *testing.T) {
t.Parallel()
db, clean := createTestDB(t)
defer clean()
cc := CollectionConfig{
Name: "Not a URL-safe name",
Schema: util.SchemaFromInstance(&Dog{}, false),
}
if _, err := db.NewCollection(cc); err != ErrInvalidCollectionName {
t.Fatal("the collection name should be invalid")
}
})
}

func TestAddIndex(t *testing.T) {
Expand Down Expand Up @@ -187,7 +199,6 @@ func TestCreateInstance(t *testing.T) {
assertPersonInCollection(t, collection, newPerson1)
assertPersonInCollection(t, collection, newPerson2)
})

t.Run("WithDefinedID", func(t *testing.T) {
t.Parallel()
db, clean := createTestDB(t)
Expand All @@ -210,7 +221,6 @@ func TestCreateInstance(t *testing.T) {
}
assertPersonInCollection(t, collection, newPerson)
})

t.Run("Re-Create", func(t *testing.T) {
t.Parallel()
db, clean := createTestDB(t)
Expand Down
10 changes: 10 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"regexp"
"sync"
"time"

Expand Down Expand Up @@ -39,11 +40,20 @@ var (
// ErrInvalidCollectionSchema indicates the provided schema isn't valid for a Collection.
ErrInvalidCollectionSchema = errors.New("the collection schema should specify an _id string property")

// ErrInvalidCollectionName indicates the provided name isn't valid for a Collection.
ErrInvalidCollectionName = errors.New("collection name may only contain alphanumeric characters or non-consecutive hyphens, and cannot begin or end with a hyphen")

collectionNameRx *regexp.Regexp

dsDBPrefix = ds.NewKey("/db")
dsDBSchemas = dsDBPrefix.ChildString("schema")
dsDBIndexes = dsDBPrefix.ChildString("index")
)

func init() {
collectionNameRx = regexp.MustCompile(`^[A-Za-z0-9]+(?:[-][A-Za-z0-9]+)*$`)
}

// DB is the aggregate-root of events and state. External/remote events
// are dispatched to the DB, and are internally processed to impact collection
// states. Likewise, local changes in collections registered produce events dispatched
Expand Down

0 comments on commit 838f09c

Please sign in to comment.