mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
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.
This commit is contained in:
parent
0cfa236570
commit
d29f512823
2 changed files with 22 additions and 5 deletions
|
@ -10,7 +10,8 @@ const messages = {
|
||||||
invalidFilterProvided: {
|
invalidFilterProvided: {
|
||||||
message: 'Invalid filter provided for automatic Collection',
|
message: 'Invalid filter provided for automatic Collection',
|
||||||
context: 'Automatic type of collection should always have a filter value'
|
context: 'Automatic type of collection should always have a filter value'
|
||||||
}
|
},
|
||||||
|
noTitleProvided: 'Title must be provided'
|
||||||
};
|
};
|
||||||
|
|
||||||
type CollectionPost = {
|
type CollectionPost = {
|
||||||
|
@ -189,6 +190,12 @@ export class Collection {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!data.title) {
|
||||||
|
throw new ValidationError({
|
||||||
|
message: tpl(messages.noTitleProvided)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return new Collection({
|
return new Collection({
|
||||||
id: id.toHexString(),
|
id: id.toHexString(),
|
||||||
title: data.title,
|
title: data.title,
|
||||||
|
|
|
@ -18,6 +18,12 @@ describe('Collection', function () {
|
||||||
assert.ok((collection.deleted === false), 'deleted should be false');
|
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 () {
|
it('Can serialize Collection to JSON', async function () {
|
||||||
const collection = await Collection.create({
|
const collection = await Collection.create({
|
||||||
title: 'Serialize me',
|
title: 'Serialize me',
|
||||||
|
@ -60,7 +66,8 @@ describe('Collection', function () {
|
||||||
it('Can create a Collection with predefined ID', async function () {
|
it('Can create a Collection with predefined ID', async function () {
|
||||||
const id = new ObjectID();
|
const id = new ObjectID();
|
||||||
const savedCollection = await Collection.create({
|
const savedCollection = await Collection.create({
|
||||||
id: id.toHexString()
|
id: id.toHexString(),
|
||||||
|
title: 'Blah'
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(savedCollection.id, id.toHexString(), 'Collection should have same id');
|
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 () {
|
it('Can create a Collection with predefined ObjectID instance', async function () {
|
||||||
const id = new ObjectID();
|
const id = new ObjectID();
|
||||||
const savedCollection = await Collection.create({
|
const savedCollection = await Collection.create({
|
||||||
id: id
|
id: id,
|
||||||
|
title: 'Bleh'
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(savedCollection.id, id.toHexString(), 'Collection should have same id');
|
assert.equal(savedCollection.id, id.toHexString(), 'Collection should have same id');
|
||||||
|
@ -80,7 +88,8 @@ describe('Collection', function () {
|
||||||
const updatedAt = new Date();
|
const updatedAt = new Date();
|
||||||
const savedCollection = await Collection.create({
|
const savedCollection = await Collection.create({
|
||||||
created_at: createdAt,
|
created_at: createdAt,
|
||||||
updated_at: updatedAt
|
updated_at: updatedAt,
|
||||||
|
title: 'Bluh'
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(savedCollection.createdAt, createdAt, 'Collection should have same created_at');
|
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 () {
|
it('Throws an error when trying to create a Collection with invalid created_at date', async function () {
|
||||||
await assert.rejects(async () => {
|
await assert.rejects(async () => {
|
||||||
await Collection.create({
|
await Collection.create({
|
||||||
created_at: 'invalid date'
|
created_at: 'invalid date',
|
||||||
|
title: 'Blih'
|
||||||
});
|
});
|
||||||
}, (err: any) => {
|
}, (err: any) => {
|
||||||
assert.equal(err.message, 'Invalid date provided for created_at', 'Error message should match');
|
assert.equal(err.message, 'Invalid date provided for created_at', 'Error message should match');
|
||||||
|
|
Loading…
Add table
Reference in a new issue