From 6cd1dc80057b3b1368df21d4635ff5f893899736 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 2 Oct 2018 01:40:29 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9BFixed=20blog=20not=20responding=20w?= =?UTF-8?q?hen=20uploading=20routes.yaml=20with=20unknown=20taxonomy=20(#9?= =?UTF-8?q?926)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #9870 Instead of 500'ing when uploading a routes.yaml with an invalid taxonomy we will now send back an error saying it's invalid. --- core/server/services/settings/validate.js | 10 +++++++++ .../unit/services/settings/validate_spec.js | 21 ++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/core/server/services/settings/validate.js b/core/server/services/settings/validate.js index 4bce0e9f95..c67d4c5cbc 100644 --- a/core/server/services/settings/validate.js +++ b/core/server/services/settings/validate.js @@ -302,6 +302,7 @@ _private.validateCollections = function validateCollections(collections) { }; _private.validateTaxonomies = function validateTaxonomies(taxonomies) { + const validRoutingTypeObjectKeys = Object.keys(RESOURCE_CONFIG.TAXONOMIES); _.each(taxonomies, (routingTypeObject, routingTypeObjectKey) => { if (!routingTypeObject) { throw new common.errors.ValidationError({ @@ -313,6 +314,15 @@ _private.validateTaxonomies = function validateTaxonomies(taxonomies) { }); } + if (!validRoutingTypeObjectKeys.includes(routingTypeObjectKey)) { + throw new common.errors.ValidationError({ + message: common.i18n.t('errors.services.settings.yaml.validate', { + at: routingTypeObjectKey, + reason: 'Unknown taxonomy.' + }) + }); + } + // CASE: we hard-require trailing slashes for the taxonomie permalink route if (!routingTypeObject.match(/\/$/)) { throw new common.errors.ValidationError({ diff --git a/core/test/unit/services/settings/validate_spec.js b/core/test/unit/services/settings/validate_spec.js index 3d757c5cb6..e13b7646b9 100644 --- a/core/test/unit/services/settings/validate_spec.js +++ b/core/test/unit/services/settings/validate_spec.js @@ -35,7 +35,22 @@ describe('UNIT: services/settings/validate', function () { try { validate({ taxonomies: { - tags: '/categories/:slug/' + tag: '/categories/:slug/' + } + }); + } catch (err) { + (err instanceof common.errors.ValidationError).should.be.true(); + return; + } + + throw new Error('should fail'); + }); + + it('throws error when using an undefined taxonomy', function () { + try { + validate({ + taxonomies: { + sweet_baked_good: '/patisserie/{slug}' } }); } catch (err) { @@ -220,7 +235,7 @@ describe('UNIT: services/settings/validate', function () { } }, taxonomies: { - tags: '/tags/{slug}/', + tag: '/tags/{slug}/', author: '/authors/{slug}/', } }); @@ -228,7 +243,7 @@ describe('UNIT: services/settings/validate', function () { object.should.eql({ routes: {}, taxonomies: { - tags: '/tags/:slug/', + tag: '/tags/:slug/', author: '/authors/:slug/' }, collections: {