0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:18:42 -05:00

Removed i18n dependency from routing validate module

refs https://linear.app/tryghost/issue/CORE-35/refactor-route-and-redirect-settings

- i18n is an old pattern we are getting rid of. By removing it here we get rid of an extra dependancy on frontend's "proxy" module
This commit is contained in:
Naz 2021-09-23 18:09:26 +02:00 committed by naz
parent c3f9233165
commit e9c1aff418
2 changed files with 26 additions and 27 deletions

View file

@ -1,11 +1,15 @@
const _ = require('lodash'); const _ = require('lodash');
const debug = require('@tryghost/debug')('frontend:services:settings:validate'); const debug = require('@tryghost/debug')('frontend:services:settings:validate');
const {i18n} = require('../proxy'); const tpl = require('@tryghost/tpl');
const errors = require('@tryghost/errors'); const errors = require('@tryghost/errors');
const bridge = require('../../../bridge'); const bridge = require('../../../bridge');
const _private = {}; const _private = {};
let RESOURCE_CONFIG; let RESOURCE_CONFIG;
const messages = {
validationError: `The following definition "{at}" is invalid: {reason}`
};
_private.validateTemplate = function validateTemplate(object) { _private.validateTemplate = function validateTemplate(object) {
// CASE: /about/: about // CASE: /about/: about
if (typeof object === 'string') { if (typeof object === 'string') {
@ -42,7 +46,7 @@ _private.validateData = function validateData(object) {
if (!shortForm.match(/.*\..*/)) { if (!shortForm.match(/.*\..*/)) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: shortForm, at: shortForm,
reason: 'Incorrect Format. Please use e.g. tag.recipes' reason: 'Incorrect Format. Please use e.g. tag.recipes'
}) })
@ -133,7 +137,7 @@ _private.validateData = function validateData(object) {
_.each(requiredQueryFields, (option) => { _.each(requiredQueryFields, (option) => {
if (!Object.prototype.hasOwnProperty.call(object.data[key], option)) { if (!Object.prototype.hasOwnProperty.call(object.data[key], option)) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: JSON.stringify(object.data[key]), at: JSON.stringify(object.data[key]),
reason: `${option} is required.` reason: `${option} is required.`
}) })
@ -142,7 +146,7 @@ _private.validateData = function validateData(object) {
if (allowedQueryValues[option] && allowedQueryValues[option].indexOf(object.data[key][option]) === -1) { if (allowedQueryValues[option] && allowedQueryValues[option].indexOf(object.data[key][option]) === -1) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: JSON.stringify(object.data[key]), at: JSON.stringify(object.data[key]),
reason: `${object.data[key][option]} not supported. Please use ${_.uniq(allowedQueryValues[option])}.` reason: `${object.data[key][option]} not supported. Please use ${_.uniq(allowedQueryValues[option])}.`
}) })
@ -186,7 +190,7 @@ _private.validateData = function validateData(object) {
_private.validateRoutes = function validateRoutes(routes) { _private.validateRoutes = function validateRoutes(routes) {
if (routes.constructor !== Object) { if (routes.constructor !== Object) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routes, at: routes,
reason: '`routes` must be a YAML map.' reason: '`routes` must be a YAML map.'
}) })
@ -197,7 +201,7 @@ _private.validateRoutes = function validateRoutes(routes) {
// CASE: we hard-require trailing slashes for the index route // CASE: we hard-require trailing slashes for the index route
if (!routingTypeObjectKey.match(/\/$/)) { if (!routingTypeObjectKey.match(/\/$/)) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObjectKey, at: routingTypeObjectKey,
reason: 'A trailing slash is required.' reason: 'A trailing slash is required.'
}) })
@ -207,7 +211,7 @@ _private.validateRoutes = function validateRoutes(routes) {
// CASE: we hard-require leading slashes for the index route // CASE: we hard-require leading slashes for the index route
if (!routingTypeObjectKey.match(/^\//)) { if (!routingTypeObjectKey.match(/^\//)) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObjectKey, at: routingTypeObjectKey,
reason: 'A leading slash is required.' reason: 'A leading slash is required.'
}) })
@ -217,7 +221,7 @@ _private.validateRoutes = function validateRoutes(routes) {
// CASE: you define /about/: // CASE: you define /about/:
if (!routingTypeObject) { if (!routingTypeObject) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObjectKey, at: routingTypeObjectKey,
reason: 'Please define a template.' reason: 'Please define a template.'
}), }),
@ -235,7 +239,7 @@ _private.validateRoutes = function validateRoutes(routes) {
_private.validateCollections = function validateCollections(collections) { _private.validateCollections = function validateCollections(collections) {
if (collections.constructor !== Object) { if (collections.constructor !== Object) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: collections, at: collections,
reason: '`collections` must be a YAML map.' reason: '`collections` must be a YAML map.'
}) })
@ -246,7 +250,7 @@ _private.validateCollections = function validateCollections(collections) {
// CASE: we hard-require trailing slashes for the collection index route // CASE: we hard-require trailing slashes for the collection index route
if (!routingTypeObjectKey.match(/\/$/)) { if (!routingTypeObjectKey.match(/\/$/)) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObjectKey, at: routingTypeObjectKey,
reason: 'A trailing slash is required.' reason: 'A trailing slash is required.'
}) })
@ -256,7 +260,7 @@ _private.validateCollections = function validateCollections(collections) {
// CASE: we hard-require leading slashes for the collection index route // CASE: we hard-require leading slashes for the collection index route
if (!routingTypeObjectKey.match(/^\//)) { if (!routingTypeObjectKey.match(/^\//)) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObjectKey, at: routingTypeObjectKey,
reason: 'A leading slash is required.' reason: 'A leading slash is required.'
}) })
@ -265,7 +269,7 @@ _private.validateCollections = function validateCollections(collections) {
if (!Object.prototype.hasOwnProperty.call(routingTypeObject, 'permalink')) { if (!Object.prototype.hasOwnProperty.call(routingTypeObject, 'permalink')) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObjectKey, at: routingTypeObjectKey,
reason: 'Please define a permalink route.' reason: 'Please define a permalink route.'
}), }),
@ -277,7 +281,7 @@ _private.validateCollections = function validateCollections(collections) {
if (!routingTypeObject.permalink) { if (!routingTypeObject.permalink) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObjectKey, at: routingTypeObjectKey,
reason: 'Please define a permalink route.' reason: 'Please define a permalink route.'
}), }),
@ -288,7 +292,7 @@ _private.validateCollections = function validateCollections(collections) {
// CASE: we hard-require trailing slashes for the value/permalink route // CASE: we hard-require trailing slashes for the value/permalink route
if (!routingTypeObject.permalink.match(/\/$/)) { if (!routingTypeObject.permalink.match(/\/$/)) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObject.permalink, at: routingTypeObject.permalink,
reason: 'A trailing slash is required.' reason: 'A trailing slash is required.'
}) })
@ -298,7 +302,7 @@ _private.validateCollections = function validateCollections(collections) {
// CASE: we hard-require leading slashes for the value/permalink route // CASE: we hard-require leading slashes for the value/permalink route
if (!routingTypeObject.permalink.match(/^\//)) { if (!routingTypeObject.permalink.match(/^\//)) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObject.permalink, at: routingTypeObject.permalink,
reason: 'A leading slash is required.' reason: 'A leading slash is required.'
}) })
@ -308,7 +312,7 @@ _private.validateCollections = function validateCollections(collections) {
// CASE: notation /:slug/ or /:primary_author/ is not allowed. We only accept /{{...}}/. // CASE: notation /:slug/ or /:primary_author/ is not allowed. We only accept /{{...}}/.
if (routingTypeObject.permalink && routingTypeObject.permalink.match(/\/:\w+/)) { if (routingTypeObject.permalink && routingTypeObject.permalink.match(/\/:\w+/)) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObject.permalink, at: routingTypeObject.permalink,
reason: 'Please use the following notation e.g. /{slug}/.' reason: 'Please use the following notation e.g. /{slug}/.'
}) })
@ -331,7 +335,7 @@ _private.validateCollections = function validateCollections(collections) {
_private.validateTaxonomies = function validateTaxonomies(taxonomies) { _private.validateTaxonomies = function validateTaxonomies(taxonomies) {
if (taxonomies.constructor !== Object) { if (taxonomies.constructor !== Object) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: taxonomies, at: taxonomies,
reason: '`taxonomies` must be a YAML map.' reason: '`taxonomies` must be a YAML map.'
}) })
@ -342,7 +346,7 @@ _private.validateTaxonomies = function validateTaxonomies(taxonomies) {
_.each(taxonomies, (routingTypeObject, routingTypeObjectKey) => { _.each(taxonomies, (routingTypeObject, routingTypeObjectKey) => {
if (!routingTypeObject) { if (!routingTypeObject) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObjectKey, at: routingTypeObjectKey,
reason: 'Please define a taxonomy permalink route.' reason: 'Please define a taxonomy permalink route.'
}), }),
@ -352,7 +356,7 @@ _private.validateTaxonomies = function validateTaxonomies(taxonomies) {
if (!validRoutingTypeObjectKeys.includes(routingTypeObjectKey)) { if (!validRoutingTypeObjectKeys.includes(routingTypeObjectKey)) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObjectKey, at: routingTypeObjectKey,
reason: 'Unknown taxonomy.' reason: 'Unknown taxonomy.'
}) })
@ -362,7 +366,7 @@ _private.validateTaxonomies = function validateTaxonomies(taxonomies) {
// CASE: we hard-require trailing slashes for the taxonomie permalink route // CASE: we hard-require trailing slashes for the taxonomie permalink route
if (!routingTypeObject.match(/\/$/)) { if (!routingTypeObject.match(/\/$/)) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObject, at: routingTypeObject,
reason: 'A trailing slash is required.' reason: 'A trailing slash is required.'
}) })
@ -372,7 +376,7 @@ _private.validateTaxonomies = function validateTaxonomies(taxonomies) {
// CASE: we hard-require leading slashes for the value/permalink route // CASE: we hard-require leading slashes for the value/permalink route
if (!routingTypeObject.match(/^\//)) { if (!routingTypeObject.match(/^\//)) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObject, at: routingTypeObject,
reason: 'A leading slash is required.' reason: 'A leading slash is required.'
}) })
@ -382,7 +386,7 @@ _private.validateTaxonomies = function validateTaxonomies(taxonomies) {
// CASE: notation /:slug/ or /:primary_author/ is not allowed. We only accept /{{...}}/. // CASE: notation /:slug/ or /:primary_author/ is not allowed. We only accept /{{...}}/.
if (routingTypeObject && routingTypeObject.match(/\/:\w+/)) { if (routingTypeObject && routingTypeObject.match(/\/:\w+/)) {
throw new errors.ValidationError({ throw new errors.ValidationError({
message: i18n.t('errors.services.settings.yaml.validate', { message: tpl(messages.validationError, {
at: routingTypeObject, at: routingTypeObject,
reason: 'Please use the following notation e.g. /{slug}/.' reason: 'Please use the following notation e.g. /{slug}/.'
}) })

View file

@ -539,11 +539,6 @@
} }
}, },
"services": { "services": {
"settings": {
"yaml": {
"validate": "The following definition \"{at}\" is invalid: {reason}"
}
},
"mega": { "mega": {
"requestFailed": { "requestFailed": {
"error": "The email service was unable to send an email batch." "error": "The email service was unable to send an email batch."