Skip to content

Commit

Permalink
fix: add recursive key/collection validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jyecusch committed Jul 14, 2021
1 parent 0124cf0 commit 1913850
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
18 changes: 7 additions & 11 deletions plugins/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,14 @@ func ValidateKey(key *sdk.Key) error {
if key == nil {
return fmt.Errorf("provide non-nil key")
}
if key.Collection.Name == "" {
return fmt.Errorf("provide non-blank key.Collection.Name")
}
if key.Id == "" {
return fmt.Errorf("provide non-blank key.Id")
}
if key.Collection.Parent != nil {
if key.Collection.Parent.Collection.Name == "" {
return fmt.Errorf("provide non-blank key.Collection.Parent.Collection.Name")
}
if key.Collection.Parent.Id == "" {
return fmt.Errorf("provide non-blank key.Collection.Parent.Id")
if key.Collection == nil {
return fmt.Errorf("provide non-nil key.Collection")
} else {
if err := ValidateQueryCollection(key.Collection); err != nil {
return fmt.Errorf("invalid collection for document key %s, %v", key.Id, err)
}
}
return nil
Expand All @@ -68,8 +64,8 @@ func ValidateQueryCollection(collection *sdk.Collection) error {
return fmt.Errorf("provide non-blank collection.Name")
}
if collection.Parent != nil {
if collection.Parent.Collection.Name == "" {
return fmt.Errorf("provide non-blank collection.Parent.Collection.Name")
if err := ValidateKey(collection.Parent); err != nil {
return fmt.Errorf("invalid parent for collection %s, %v", collection.Name, err)
}
}
return nil
Expand Down
18 changes: 12 additions & 6 deletions plugins/document/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var _ = Describe("Document Plugin", func() {
When("Blank key.Collection", func() {
It("should return error", func() {
err := doc.ValidateKey(&sdk.Key{})
Expect(err).To(BeEquivalentTo(errors.New("provide non-blank key.Collection.Name")))
Expect(err).To(BeEquivalentTo(errors.New("provide non-blank key.Id")))
})
})
When("Blank key.Id", func() {
Expand All @@ -56,7 +56,7 @@ var _ = Describe("Document Plugin", func() {
Id: "123",
}
err := doc.ValidateKey(&key)
Expect(err).To(BeEquivalentTo(errors.New("provide non-blank key.Collection.Parent.Collection.Name")))
Expect(err.Error()).To(ContainSubstring("invalid parent for collection users, provide non-blank key.Id"))
})
})
When("Blank key.Collection.Parent.Id", func() {
Expand All @@ -69,7 +69,7 @@ var _ = Describe("Document Plugin", func() {
Id: "123",
}
err := doc.ValidateKey(&key)
Expect(err).To(BeEquivalentTo(errors.New("provide non-blank key.Collection.Parent.Id")))
Expect(err.Error()).To(ContainSubstring("invalid parent for collection orders, provide non-blank key.Id"))
})
})
})
Expand Down Expand Up @@ -98,17 +98,23 @@ var _ = Describe("Document Plugin", func() {
It("should return error", func() {
coll := sdk.Collection{
Name: "users",
Parent: &sdk.Key{},
Parent: &sdk.Key{
Id: "test-key",
Collection: &sdk.Collection{},
},
}
err := doc.ValidateQueryCollection(&coll)
Expect(err).To(BeEquivalentTo(errors.New("provide non-blank collection.Parent.Collection.Name")))
Expect(err.Error()).To(ContainSubstring("provide non-blank collection.Name"))
})
})
When("Blank collection.Parent.Id", func() {
It("should return nil", func() {
coll := sdk.Collection{
Name: "orders",
Parent: &sdk.Key{Collection: &sdk.Collection{Name: "customers"}},
Parent: &sdk.Key{
Id: "test-key",
Collection: &sdk.Collection{Name: "customers"},
},
}
err := doc.ValidateQueryCollection(&coll)
Expect(err).To(BeNil())
Expand Down

0 comments on commit 1913850

Please sign in to comment.