From 1913850c616198f42700515920fec416ea836453 Mon Sep 17 00:00:00 2001 From: Jye Cusch Date: Wed, 14 Jul 2021 14:07:32 +1000 Subject: [PATCH] fix: add recursive key/collection validation --- plugins/document/document.go | 18 +++++++----------- plugins/document/document_test.go | 18 ++++++++++++------ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/plugins/document/document.go b/plugins/document/document.go index 26369e76e..ed5dc58ee 100644 --- a/plugins/document/document.go +++ b/plugins/document/document.go @@ -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 @@ -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 diff --git a/plugins/document/document_test.go b/plugins/document/document_test.go index e970e8021..99a14de03 100644 --- a/plugins/document/document_test.go +++ b/plugins/document/document_test.go @@ -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() { @@ -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() { @@ -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")) }) }) }) @@ -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())