mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Added automatic collection validation
refs https://github.com/TryGhost/Team/issues/3170 - When an automatic collection is created it should always have a valid filter assigned
This commit is contained in:
parent
f64276b26e
commit
6e224f3702
2 changed files with 27 additions and 3 deletions
|
@ -5,7 +5,11 @@ import ObjectID from 'bson-objectid';
|
|||
|
||||
const messages = {
|
||||
invalidIDProvided: 'Invalid ID provided for Collection',
|
||||
invalidDateProvided: 'Invalid date provided for {fieldName}'
|
||||
invalidDateProvided: 'Invalid date provided for {fieldName}',
|
||||
invalidFilterProvided: {
|
||||
message: 'Invalid filter provided for automatic Collection',
|
||||
context: 'Automatic type of collection should always have a filter value'
|
||||
}
|
||||
};
|
||||
|
||||
export class Collection {
|
||||
|
@ -109,6 +113,13 @@ export class Collection {
|
|||
});
|
||||
}
|
||||
|
||||
if (data.type === 'automatic' && !data.filter) {
|
||||
throw new ValidationError({
|
||||
message: tpl(messages.invalidFilterProvided.message),
|
||||
context: tpl(messages.invalidFilterProvided.context)
|
||||
});
|
||||
}
|
||||
|
||||
return new Collection({
|
||||
id: id.toHexString(),
|
||||
title: data.title,
|
||||
|
|
|
@ -88,7 +88,7 @@ describe('Collection', function () {
|
|||
});
|
||||
|
||||
it('Throws an error when trying to create a Collection with an invalid ID', async function () {
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await Collection.create({
|
||||
id: 12345
|
||||
});
|
||||
|
@ -99,7 +99,7 @@ describe('Collection', function () {
|
|||
});
|
||||
|
||||
it('Throws an error when trying to create a Collection with invalid created_at date', async function () {
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await Collection.create({
|
||||
created_at: 'invalid date'
|
||||
});
|
||||
|
@ -109,6 +109,19 @@ describe('Collection', function () {
|
|||
});
|
||||
});
|
||||
|
||||
it('Throws an error when trying to create an automatic Collection without a filter', async function () {
|
||||
await assert.rejects(async () => {
|
||||
await Collection.create({
|
||||
type: 'automatic',
|
||||
filter: null
|
||||
});
|
||||
}, (err: any) => {
|
||||
assert.equal(err.message, 'Invalid filter provided for automatic Collection', 'Error message should match');
|
||||
assert.equal(err.context, 'Automatic type of collection should always have a filter value', 'Error message should match');
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
it('Can add posts to different positions', async function () {
|
||||
const collection = await Collection.create({
|
||||
title: 'Testing adding posts'
|
||||
|
|
Loading…
Add table
Reference in a new issue