Skip to content

Commit 83ba3f7

Browse files
authored
feat(registerplugin): make configValidators optional (#70)
This pr makes configvalidatios optional. closes #64
1 parent 24483cf commit 83ba3f7

3 files changed

Lines changed: 46 additions & 16 deletions

File tree

extraPlugin/extra-plugin.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1+
const {configValidator, routeSplit} = require('../scully/bin');
12

2-
const {configValidator} = require('../scully/bin');
3-
4-
console.log(__dirname)
5-
6-
exports.extraRoutesPlugin = (route, options) => {
3+
const extraRoutesPlugin = (route, options) => {
74
const {createPath} = routeSplit(route);
85
if (options.numberOfPages) {
96
return Array.from({length: options.numberOfPages}, (_v, k) => k).map(n => ({
@@ -19,6 +16,31 @@ exports.extraRoutesPlugin = (route, options) => {
1916
}
2017
return [];
2118
};
22-
/** the validator is mandatory */
23-
exports.extraRoutesPlugin[configValidator] = async options => [];
2419

20+
extraRoutesPlugin[configValidator] = async options => {
21+
const errors = [];
22+
23+
if (options.numberOfPages && typeof options.numberOfPages !== 'number') {
24+
errors.push(
25+
`extraroutesPlugin plugin numberOfPages should be a number, not a ${typeof options.numberOfPages}`
26+
);
27+
}
28+
if (options.numberOfPages && options.data) {
29+
errors.push(`extraroutesPlugin plugin can't have property 'numberOfPages' and 'data' at the same time`);
30+
}
31+
if (options.data) {
32+
if (!Array.isArray(options.data)) {
33+
errors.push(`extraroutesPlugin property 'data' needs to be an array`);
34+
} else {
35+
if (!options.data.every(item => typeof item.title === 'string' && typeof item.data === 'string')) {
36+
errors.push(
37+
`extraroutesPlugin property 'data' needs to have 'title' and 'data' strings on every tuple`
38+
);
39+
}
40+
}
41+
}
42+
return errors;
43+
};
44+
45+
exports.extraRoutesPlugin = extraRoutesPlugin;
46+
/** the validator is mandatory */

scully.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ exports.config = {
1111
routes: {
1212
'/demo/:id': {
1313
type: 'extra',
14-
numberOfPages: 10,
14+
numberOfPages: 5,
1515
},
1616
'/home/:topLevel': {
1717
type: 'extra',

scully/pluginManagement/pluginRepository.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface Plugin {
1010
}
1111

1212
export type ErrorString = string;
13-
export type ConfigValidator = (HandledRoute) => ErrorString[]
13+
export type ConfigValidator = (HandledRoute) => ErrorString[];
1414

1515
export interface FilePlugin {
1616
alternateExtensions?: string[];
@@ -31,16 +31,24 @@ export const plugins: Plugins = {
3131

3232
export type PluginTypes = keyof Plugins;
3333

34-
export const registerPlugin = (type: PluginTypes, name: string, plugin: any) => {
35-
if (plugins[type][name]) {
34+
export const registerPlugin = (
35+
type: PluginTypes,
36+
name: string,
37+
plugin: any,
38+
{replaceExistingPlugin = false} = {}
39+
) => {
40+
if (replaceExistingPlugin === false && plugins[type][name]) {
3641
throw new Error(`Plugin ${name} already exists`);
3742
}
3843
if (type === 'router' && plugin[configValidator] === undefined) {
39-
logError(
40-
`Route plugin "${yellow(
41-
name
42-
)}" should have an config validator attached to the 'validate' symbol `
43-
);
44+
logError(`
45+
---------------
46+
Route plugin "${yellow(name)}" should have an config validator attached to '${
47+
plugin.name
48+
}[configValidator]'
49+
---------------
50+
`);
51+
plugin[configValidator] = async () => [];
4452
}
4553
plugins[type][name] = plugin;
4654
};

0 commit comments

Comments
 (0)