From d29f5128239e56e0a02c7321709ea639b4e13670 Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Wed, 28 Jun 2023 22:44:53 +0100 Subject: [PATCH] Required titles for collections We don't want to allow collections to be created without a title, and we need to encoe that business rule in the entity. --- ghost/collections/src/Collection.ts | 9 ++++++++- ghost/collections/test/Collection.test.ts | 18 ++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/ghost/collections/src/Collection.ts b/ghost/collections/src/Collection.ts index d771caea4e..108adf2200 100644 --- a/ghost/collections/src/Collection.ts +++ b/ghost/collections/src/Collection.ts @@ -10,7 +10,8 @@ const messages = { invalidFilterProvided: { message: 'Invalid filter provided for automatic Collection', context: 'Automatic type of collection should always have a filter value' - } + }, + noTitleProvided: 'Title must be provided' }; type CollectionPost = { @@ -189,6 +190,12 @@ export class Collection { }); } + if (!data.title) { + throw new ValidationError({ + message: tpl(messages.noTitleProvided) + }); + } + return new Collection({ id: id.toHexString(), title: data.title, diff --git a/ghost/collections/test/Collection.test.ts b/ghost/collections/test/Collection.test.ts index 540eb7dc49..7e36c323da 100644 --- a/ghost/collections/test/Collection.test.ts +++ b/ghost/collections/test/Collection.test.ts @@ -18,6 +18,12 @@ describe('Collection', function () { assert.ok((collection.deleted === false), 'deleted should be false'); }); + it('Cannot create a collection without a title', async function () { + assert.rejects(async () => { + await Collection.create({}); + }); + }); + it('Can serialize Collection to JSON', async function () { const collection = await Collection.create({ title: 'Serialize me', @@ -60,7 +66,8 @@ describe('Collection', function () { it('Can create a Collection with predefined ID', async function () { const id = new ObjectID(); const savedCollection = await Collection.create({ - id: id.toHexString() + id: id.toHexString(), + title: 'Blah' }); assert.equal(savedCollection.id, id.toHexString(), 'Collection should have same id'); @@ -69,7 +76,8 @@ describe('Collection', function () { it('Can create a Collection with predefined ObjectID instance', async function () { const id = new ObjectID(); const savedCollection = await Collection.create({ - id: id + id: id, + title: 'Bleh' }); assert.equal(savedCollection.id, id.toHexString(), 'Collection should have same id'); @@ -80,7 +88,8 @@ describe('Collection', function () { const updatedAt = new Date(); const savedCollection = await Collection.create({ created_at: createdAt, - updated_at: updatedAt + updated_at: updatedAt, + title: 'Bluh' }); assert.equal(savedCollection.createdAt, createdAt, 'Collection should have same created_at'); @@ -101,7 +110,8 @@ describe('Collection', function () { it('Throws an error when trying to create a Collection with invalid created_at date', async function () { await assert.rejects(async () => { await Collection.create({ - created_at: 'invalid date' + created_at: 'invalid date', + title: 'Blih' }); }, (err: any) => { assert.equal(err.message, 'Invalid date provided for created_at', 'Error message should match');