generated from Seekra/repository-template
Allow only sections as first-level settings #117
@@ -53,10 +53,6 @@ limitations under the License.
|
||||
* @typedef {BoolSettingConfig | NumberSettingConfig | StringSettingConfig | SelectionSettingConfig | SectionSettingConfig} SettingConfigEntry
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} SettingsConfig
|
||||
* @property {SettingConfigEntry[]} contents
|
||||
*/
|
||||
/**
|
||||
* @typedef {{ type: 'bool', name: string, i18n: string, description?: string, default: boolean }} BoolSettingConfig
|
||||
* @typedef {{ type: 'number', name: string, i18n: string, description?: string, default: number }} NumberSettingConfig
|
||||
@@ -64,5 +60,6 @@ limitations under the License.
|
||||
* @typedef {{ type: 'selection', name: string, i18n: string, description?: string, default: string | string[], allowMultiple?: boolean, options: SelectionOption[] }} SelectionSettingConfig
|
||||
* @typedef {{ type: 'section', name: string, i18n?: string, description?: string, content: SettingConfigEntry[] }} SectionSettingConfig
|
||||
* @typedef {BoolSettingConfig | NumberSettingConfig | StringSettingConfig | SelectionSettingConfig | SectionSettingConfig} SettingConfigEntry
|
||||
* @typedef {{ contents: SettingConfigEntry[] }} SettingsConfig
|
||||
* @typedef {{ contents: SettingConfigEntry[] }} FirstLevelSettingConfigEntry
|
||||
* @typedef {{ contents: FirstLevelSettingConfigEntry[] }} SettingsConfig
|
||||
*/
|
||||
@@ -33,7 +33,6 @@ describe('validateSettingsConfig', () => {
|
||||
{ raw: {
|
||||
contents: [
|
||||
{
|
||||
type: 'section',
|
||||
name: 'general',
|
||||
i18n: 'settings.settings.general',
|
||||
content: [
|
||||
@@ -49,7 +48,6 @@ describe('validateSettingsConfig', () => {
|
||||
{ raw: {
|
||||
contents: [
|
||||
{
|
||||
type: 'section',
|
||||
name: 'general',
|
||||
i18n: 'settings.settings.general',
|
||||
content: [
|
||||
@@ -61,7 +59,6 @@ describe('validateSettingsConfig', () => {
|
||||
]
|
||||
},
|
||||
{
|
||||
type: 'section',
|
||||
name: 'copyOfGeneral',
|
||||
i18n: 'settings.settings.copyOfGeneral',
|
||||
content: [
|
||||
@@ -77,7 +74,6 @@ describe('validateSettingsConfig', () => {
|
||||
{ raw: {
|
||||
contents: [
|
||||
{
|
||||
type: 'section',
|
||||
name: 'general',
|
||||
i18n: 'settings.settings.general',
|
||||
content: [
|
||||
@@ -89,7 +85,6 @@ describe('validateSettingsConfig', () => {
|
||||
]
|
||||
},
|
||||
{
|
||||
type: 'section',
|
||||
name: 'copyOfGeneral',
|
||||
i18n: 'settings.settings.copyOfGeneral',
|
||||
content: [
|
||||
@@ -102,7 +97,6 @@ describe('validateSettingsConfig', () => {
|
||||
]
|
||||
},
|
||||
{
|
||||
type: 'section',
|
||||
name: 'aSection',
|
||||
i18n: 'settings.settings.aSection',
|
||||
content: [
|
||||
@@ -141,7 +135,6 @@ describe('validateSettingsConfig', () => {
|
||||
{ raw: {
|
||||
contents: [
|
||||
{
|
||||
type: 'section',
|
||||
name: 'general',
|
||||
i18n: 'settings.settings.general',
|
||||
content: [
|
||||
@@ -153,7 +146,6 @@ describe('validateSettingsConfig', () => {
|
||||
]
|
||||
},
|
||||
{
|
||||
type: 'section',
|
||||
name: 'copyOfGeneral',
|
||||
i18n: 'settings.settings.copyOfGeneral',
|
||||
content: [
|
||||
@@ -166,7 +158,6 @@ describe('validateSettingsConfig', () => {
|
||||
]
|
||||
},
|
||||
{
|
||||
type: 'section',
|
||||
name: 'aSection',
|
||||
i18n: 'settings.settings.aSection',
|
||||
content: [
|
||||
|
||||
@@ -83,6 +83,19 @@ export const validateEntry = function validateEntry (entry, path) {
|
||||
}
|
||||
}
|
||||
|
||||
export const validateFirstLevelSection = function validateFirstLevelSection (section, path) {
|
||||
assertString(section.name);
|
||||
assertString(section.i18n);
|
||||
|
||||
if (!Array.isArray(section.content)) {
|
||||
throw new Error(`[settings] "${path}.content" must be an array`);
|
||||
};
|
||||
|
||||
section.content.forEach((entry, i) =>
|
||||
validateEntry(entry, `${path}.content[${i}]`)
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates a raw settings config object.
|
||||
* @param {unknown} raw
|
||||
@@ -97,7 +110,7 @@ export function validateSettingsConfig(raw) {
|
||||
throw new Error('[settings] "contents" must be an array');
|
||||
}
|
||||
raw.contents.forEach((entry, i) =>
|
||||
validateEntry(entry, `contents[${i}]`)
|
||||
validateFirstLevelSection(entry, `contents[${i}]`)
|
||||
);
|
||||
return { valid: true, config: raw };
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user