From d413b3d654978d54d594024d256e666aaaea8939 Mon Sep 17 00:00:00 2001 From: Rishabh Date: Fri, 2 Jul 2021 22:14:21 +0530 Subject: [PATCH] Updated visibility validator for page/posts refs https://github.com/TryGhost/Team/issues/849 Custom post visibility (behind alpha flag) is added to the API using new `visibility_filter` attribute that stores the custom filter. This change - - updates validator for visibility to check new `visibility_filter` property - cleans usage of i18n in favor of tpl --- .../api/canary/utils/validators/input/pages.js | 13 +++++++++---- .../api/canary/utils/validators/input/posts.js | 13 +++++++++---- core/server/api/v2/utils/validators/input/pages.js | 13 +++++++++---- core/server/api/v2/utils/validators/input/posts.js | 13 +++++++++---- core/server/api/v3/utils/validators/input/pages.js | 13 +++++++++---- core/server/api/v3/utils/validators/input/posts.js | 13 +++++++++---- 6 files changed, 54 insertions(+), 24 deletions(-) diff --git a/core/server/api/canary/utils/validators/input/pages.js b/core/server/api/canary/utils/validators/input/pages.js index d85303dfb2..ca532276b3 100644 --- a/core/server/api/canary/utils/validators/input/pages.js +++ b/core/server/api/canary/utils/validators/input/pages.js @@ -1,7 +1,11 @@ const jsonSchema = require('../utils/json-schema'); const models = require('../../../../../models'); const {ValidationError} = require('@tryghost/errors'); -const i18n = require('../../../../../../shared/i18n'); +const tpl = require('@tryghost/tpl'); + +const messages = { + invalidVisibilityFilter: 'Invalid filter in visibility_filter property' +}; const validateVisibility = async function (frame) { if (!frame.data.pages || !frame.data.pages[0]) { @@ -10,16 +14,17 @@ const validateVisibility = async function (frame) { // validate visibility - not done at schema level because this can be an NQL query so needs model access const visibility = frame.data.pages[0].visibility; + const visibilityFilter = frame.data.pages[0].visibility_filter; if (visibility) { if (!['public', 'members', 'paid'].includes(visibility)) { // check filter is valid try { - await models.Member.findPage({filter: visibility, limit: 1}); + await models.Member.findPage({filter: visibilityFilter, limit: 1}); return Promise.resolve(); } catch (err) { return Promise.reject(new ValidationError({ - message: i18n.t('errors.api.pages.invalidVisibilityFilter'), - property: 'visibility' + message: tpl(messages.invalidVisibilityFilter), + property: 'visibility_filter' })); } } diff --git a/core/server/api/canary/utils/validators/input/posts.js b/core/server/api/canary/utils/validators/input/posts.js index 759fb56548..a0d8406e0e 100644 --- a/core/server/api/canary/utils/validators/input/posts.js +++ b/core/server/api/canary/utils/validators/input/posts.js @@ -1,7 +1,11 @@ const jsonSchema = require('../utils/json-schema'); const models = require('../../../../../models'); const {ValidationError} = require('@tryghost/errors'); -const i18n = require('../../../../../../shared/i18n'); +const tpl = require('@tryghost/tpl'); + +const messages = { + invalidVisibilityFilter: 'Invalid filter in visibility_filter property' +}; const validateVisibility = async function (frame) { if (!frame.data.posts || !frame.data.posts[0]) { @@ -10,16 +14,17 @@ const validateVisibility = async function (frame) { // validate visibility - not done at schema level because this can be an NQL query so needs model access const visibility = frame.data.posts[0].visibility; + const visibilityFilter = frame.data.posts[0].visibility_filter; if (visibility) { if (!['public', 'members', 'paid'].includes(visibility)) { // check filter is valid try { - await models.Member.findPage({filter: visibility, limit: 1}); + await models.Member.findPage({filter: visibilityFilter, limit: 1}); return Promise.resolve(); } catch (err) { return Promise.reject(new ValidationError({ - message: i18n.t('errors.api.posts.invalidVisibilityFilter'), - property: 'visibility' + message: tpl(messages.invalidVisibilityFilter), + property: 'visibility_filter' })); } } diff --git a/core/server/api/v2/utils/validators/input/pages.js b/core/server/api/v2/utils/validators/input/pages.js index d85303dfb2..ca532276b3 100644 --- a/core/server/api/v2/utils/validators/input/pages.js +++ b/core/server/api/v2/utils/validators/input/pages.js @@ -1,7 +1,11 @@ const jsonSchema = require('../utils/json-schema'); const models = require('../../../../../models'); const {ValidationError} = require('@tryghost/errors'); -const i18n = require('../../../../../../shared/i18n'); +const tpl = require('@tryghost/tpl'); + +const messages = { + invalidVisibilityFilter: 'Invalid filter in visibility_filter property' +}; const validateVisibility = async function (frame) { if (!frame.data.pages || !frame.data.pages[0]) { @@ -10,16 +14,17 @@ const validateVisibility = async function (frame) { // validate visibility - not done at schema level because this can be an NQL query so needs model access const visibility = frame.data.pages[0].visibility; + const visibilityFilter = frame.data.pages[0].visibility_filter; if (visibility) { if (!['public', 'members', 'paid'].includes(visibility)) { // check filter is valid try { - await models.Member.findPage({filter: visibility, limit: 1}); + await models.Member.findPage({filter: visibilityFilter, limit: 1}); return Promise.resolve(); } catch (err) { return Promise.reject(new ValidationError({ - message: i18n.t('errors.api.pages.invalidVisibilityFilter'), - property: 'visibility' + message: tpl(messages.invalidVisibilityFilter), + property: 'visibility_filter' })); } } diff --git a/core/server/api/v2/utils/validators/input/posts.js b/core/server/api/v2/utils/validators/input/posts.js index 759fb56548..a0d8406e0e 100644 --- a/core/server/api/v2/utils/validators/input/posts.js +++ b/core/server/api/v2/utils/validators/input/posts.js @@ -1,7 +1,11 @@ const jsonSchema = require('../utils/json-schema'); const models = require('../../../../../models'); const {ValidationError} = require('@tryghost/errors'); -const i18n = require('../../../../../../shared/i18n'); +const tpl = require('@tryghost/tpl'); + +const messages = { + invalidVisibilityFilter: 'Invalid filter in visibility_filter property' +}; const validateVisibility = async function (frame) { if (!frame.data.posts || !frame.data.posts[0]) { @@ -10,16 +14,17 @@ const validateVisibility = async function (frame) { // validate visibility - not done at schema level because this can be an NQL query so needs model access const visibility = frame.data.posts[0].visibility; + const visibilityFilter = frame.data.posts[0].visibility_filter; if (visibility) { if (!['public', 'members', 'paid'].includes(visibility)) { // check filter is valid try { - await models.Member.findPage({filter: visibility, limit: 1}); + await models.Member.findPage({filter: visibilityFilter, limit: 1}); return Promise.resolve(); } catch (err) { return Promise.reject(new ValidationError({ - message: i18n.t('errors.api.posts.invalidVisibilityFilter'), - property: 'visibility' + message: tpl(messages.invalidVisibilityFilter), + property: 'visibility_filter' })); } } diff --git a/core/server/api/v3/utils/validators/input/pages.js b/core/server/api/v3/utils/validators/input/pages.js index d85303dfb2..ca532276b3 100644 --- a/core/server/api/v3/utils/validators/input/pages.js +++ b/core/server/api/v3/utils/validators/input/pages.js @@ -1,7 +1,11 @@ const jsonSchema = require('../utils/json-schema'); const models = require('../../../../../models'); const {ValidationError} = require('@tryghost/errors'); -const i18n = require('../../../../../../shared/i18n'); +const tpl = require('@tryghost/tpl'); + +const messages = { + invalidVisibilityFilter: 'Invalid filter in visibility_filter property' +}; const validateVisibility = async function (frame) { if (!frame.data.pages || !frame.data.pages[0]) { @@ -10,16 +14,17 @@ const validateVisibility = async function (frame) { // validate visibility - not done at schema level because this can be an NQL query so needs model access const visibility = frame.data.pages[0].visibility; + const visibilityFilter = frame.data.pages[0].visibility_filter; if (visibility) { if (!['public', 'members', 'paid'].includes(visibility)) { // check filter is valid try { - await models.Member.findPage({filter: visibility, limit: 1}); + await models.Member.findPage({filter: visibilityFilter, limit: 1}); return Promise.resolve(); } catch (err) { return Promise.reject(new ValidationError({ - message: i18n.t('errors.api.pages.invalidVisibilityFilter'), - property: 'visibility' + message: tpl(messages.invalidVisibilityFilter), + property: 'visibility_filter' })); } } diff --git a/core/server/api/v3/utils/validators/input/posts.js b/core/server/api/v3/utils/validators/input/posts.js index 759fb56548..a0d8406e0e 100644 --- a/core/server/api/v3/utils/validators/input/posts.js +++ b/core/server/api/v3/utils/validators/input/posts.js @@ -1,7 +1,11 @@ const jsonSchema = require('../utils/json-schema'); const models = require('../../../../../models'); const {ValidationError} = require('@tryghost/errors'); -const i18n = require('../../../../../../shared/i18n'); +const tpl = require('@tryghost/tpl'); + +const messages = { + invalidVisibilityFilter: 'Invalid filter in visibility_filter property' +}; const validateVisibility = async function (frame) { if (!frame.data.posts || !frame.data.posts[0]) { @@ -10,16 +14,17 @@ const validateVisibility = async function (frame) { // validate visibility - not done at schema level because this can be an NQL query so needs model access const visibility = frame.data.posts[0].visibility; + const visibilityFilter = frame.data.posts[0].visibility_filter; if (visibility) { if (!['public', 'members', 'paid'].includes(visibility)) { // check filter is valid try { - await models.Member.findPage({filter: visibility, limit: 1}); + await models.Member.findPage({filter: visibilityFilter, limit: 1}); return Promise.resolve(); } catch (err) { return Promise.reject(new ValidationError({ - message: i18n.t('errors.api.posts.invalidVisibilityFilter'), - property: 'visibility' + message: tpl(messages.invalidVisibilityFilter), + property: 'visibility_filter' })); } }