Added boolean as allowed custom theme setting type

refs https://github.com/TryGhost/Team/issues/1106

- updated schema validation to add `'boolean'` as an allowed `type` value
- added `format()` and `parse()` methods to `CustomThemeSetting` model to match `Settings` model behaviour for boolean-type settings
This commit is contained in:
Kevin Ansfield 2021-10-13 17:25:32 +01:00
parent f3dcf578a9
commit 7cb93be60b
3 changed files with 97 additions and 2 deletions

View File

@ -678,7 +678,8 @@ module.exports = {
nullable: false,
validations: {
isIn: [[
'select'
'select',
'boolean'
]]
}
},

View File

@ -1,7 +1,48 @@
const _ = require('lodash');
const ghostBookshelf = require('./base');
const CustomThemeSetting = ghostBookshelf.Model.extend({
tableName: 'custom_theme_settings'
tableName: 'custom_theme_settings',
parse() {
const attrs = ghostBookshelf.Model.prototype.parse.apply(this, arguments);
const settingType = attrs.type;
// transform "0" to false for boolean type
if (settingType === 'boolean' && (attrs.value === '0' || attrs.value === '1')) {
attrs.value = !!+attrs.value;
}
// transform "false" to false for boolean type
if (settingType === 'boolean' && (attrs.value === 'false' || attrs.value === 'true')) {
attrs.value = JSON.parse(attrs.value);
}
return attrs;
},
format() {
const attrs = ghostBookshelf.Model.prototype.format.apply(this, arguments);
const settingType = attrs.type;
if (settingType === 'boolean') {
// CASE: Ensure we won't forward strings, otherwise model events or model interactions can fail
if (attrs.value === '0' || attrs.value === '1') {
attrs.value = !!+attrs.value;
}
// CASE: Ensure we won't forward strings, otherwise model events or model interactions can fail
if (attrs.value === 'false' || attrs.value === 'true') {
attrs.value = JSON.parse(attrs.value);
}
if (_.isBoolean(attrs.value)) {
attrs.value = attrs.value.toString();
}
}
return attrs;
}
});
module.exports = {

View File

@ -0,0 +1,53 @@
const should = require('should');
const models = require('../../../../core/server/models');
describe('Unit: models/custom-theme-setting', function () {
before(function () {
models.init();
});
describe('parse', function () {
it('ensure correct parsing when fetching from db', function () {
const setting = models.CustomThemeSetting.forge();
let returns = setting.parse({theme: 'test', key: 'dark_mode', value: 'false', type: 'boolean'});
should.equal(returns.value, false);
returns = setting.parse({theme: 'test', key: 'dark_mode', value: false, type: 'boolean'});
should.equal(returns.value, false);
returns = setting.parse({theme: 'test', key: 'dark_mode', value: true, type: 'boolean'});
should.equal(returns.value, true);
returns = setting.parse({theme: 'test', key: 'dark_mode', value: 'true', type: 'boolean'});
should.equal(returns.value, true);
returns = setting.parse({theme: 'test', key: 'dark_mode', value: '0', type: 'boolean'});
should.equal(returns.value, false);
returns = setting.parse({theme: 'test', key: 'dark_mode', value: '1', type: 'boolean'});
should.equal(returns.value, true);
returns = setting.parse({theme: 'test', key: 'something', value: 'null', type: 'select'});
should.equal(returns.value, 'null');
});
});
describe('format', function () {
it('ensure correct formatting when setting', function () {
const setting = models.CustomThemeSetting.forge();
let returns = setting.format({theme: 'test', key: 'dark_mode', value: '0', type: 'boolean'});
should.equal(returns.value, 'false');
returns = setting.format({theme: 'test', key: 'dark_mode', value: '1', type: 'boolean'});
should.equal(returns.value, 'true');
returns = setting.format({theme: 'test', key: 'dark_mode', value: 'false', type: 'boolean'});
should.equal(returns.value, 'false');
returns = setting.format({theme: 'test', key: 'dark_mode', value: 'true', type: 'boolean'});
should.equal(returns.value, 'true');
});
});
});