Moved validation to one place

This commit is contained in:
Wyatt Johnson
2017-04-06 10:56:32 -06:00
parent 1f367e666b
commit 28ce77a002
9 changed files with 24 additions and 26 deletions
-3
View File
@@ -10,7 +10,6 @@ const enabled = require('debug').enabled;
const RedisStore = require('connect-redis')(session);
const redis = require('./services/redis');
const csrf = require('csurf');
const Joi = require('joi');
const errors = require('./errors');
const graph = require('./graph');
const apollo = require('graphql-server-express');
@@ -81,8 +80,6 @@ const passportDebug = require('debug')('talk:passport');
// Install the passport plugins.
plugins.get('server', 'passport').forEach((plugin) => {
Joi.assert(plugin.passport, Joi.func().arity(1), `Plugin '${plugin.name}' had an error loading the passport hook`);
passportDebug(`added plugin '${plugin.plugin.name}'`);
// Pass the passport.js instance to the plugin to allow it to inject it's
-3
View File
@@ -3,7 +3,6 @@ const mutators = require('./mutators');
const plugins = require('../services/plugins');
const debug = require('debug')('talk:graph:context');
const Joi = require('joi');
/**
* Contains the array of plugins that provide context to the server, these top
@@ -11,8 +10,6 @@ const Joi = require('joi');
* @type {Array}
*/
const contextPlugins = plugins.get('server', 'context').map(({plugin, context}) => {
Joi.assert(context, Joi.object().pattern(/\w/, Joi.func().maxArity(1)), `Plugin '${plugin.name}' had an error loading the context`);
debug(`added plugin '${plugin.name}'`);
return {context};
});
-5
View File
@@ -1,6 +1,5 @@
const {forEachField} = require('graphql-tools');
const debug = require('debug')('talk:graph:schema');
const Joi = require('joi');
/**
* XXX taken from graphql-js: src/execution/execute.js, because that function
@@ -47,10 +46,6 @@ const decorateWithHooks = (schema, hooks) => forEachField(schema, (field, typeNa
// Combine the pre/post hooks from each plugin into an array we can
// execute.
.reduce((acc, {plugin, hooks}) => {
Joi.assert(hooks, Joi.object({
pre: Joi.func(),
post: Joi.func()
}), `Plugin '${plugin.name}' had an error loading the hooks`);
// Itterate over the hooks on the fields and look at it with a switch
// block to check for misconfigured plugins.
-3
View File
@@ -1,5 +1,4 @@
const _ = require('lodash');
const Joi = require('joi');
const debug = require('debug')('talk:graph:loaders');
const Actions = require('./actions');
@@ -24,8 +23,6 @@ let loaders = [
// Load the plugin loaders from the manager.
...plugins
.get('server', 'loaders').map(({plugin, loaders}) => {
Joi.assert(loaders, Joi.object().pattern(/\w/, Joi.object().pattern(/\w/, Joi.func())), `Plugin '${plugin.name}' had an error loading the loaders`);
debug(`added plugin '${plugin.name}'`);
return loaders;
-3
View File
@@ -1,5 +1,4 @@
const _ = require('lodash');
const Joi = require('joi');
const debug = require('debug')('talk:graph:mutators');
const Comment = require('./comment');
@@ -18,8 +17,6 @@ let mutators = [
// Load the plugin mutators from the manager.
...plugins
.get('server', 'mutators').map(({plugin, mutators}) => {
Joi.assert(mutators, Joi.object().pattern(/\w/, Joi.object().pattern(/\w/, Joi.func())), `Plugin '${plugin.name}' had an error loading the mutators`);
debug(`added plugin '${plugin.name}'`);
return mutators;
-3
View File
@@ -1,5 +1,4 @@
const _ = require('lodash');
const Joi = require('joi');
const debug = require('debug')('talk:graph:resolvers');
const ActionSummary = require('./action_summary');
@@ -51,8 +50,6 @@ let resolvers = {
* as provide new ones.
*/
resolvers = plugins.get('server', 'resolvers').reduce((acc, {plugin, resolvers}) => {
Joi.assert(resolvers, Joi.object().pattern(/\w/, Joi.object().pattern(/\w/, Joi.func())), `Plugin '${plugin.name}' had an error loading the resolvers`);
debug(`added plugin '${plugin.name}'`);
return _.merge(acc, resolvers);
-3
View File
@@ -3,7 +3,6 @@
// this change is done now everything will likely break on the front end.
const fs = require('fs');
const Joi = require('joi');
const path = require('path');
const {mergeStrings} = require('gql-merge');
const debug = require('debug')('talk:graph:typeDefs');
@@ -21,8 +20,6 @@ const typeDefs = mergeStrings([
// Load the plugin definitions from the manager.
...plugins.get('server', 'typeDefs').map(({plugin, typeDefs}) => {
Joi.assert(typeDefs, Joi.string(), `Plugin '${plugin.name}' had an error loading the typeDefs`);
debug(`added plugin '${plugin.name}'`);
return typeDefs;
+24
View File
@@ -2,6 +2,7 @@ const fs = require('fs');
const path = require('path');
const resolve = require('resolve');
const debug = require('debug')('talk:plugins');
const Joi = require('joi');
// Add support for require rewriting.
require('app-module-path').addPath(__dirname);
@@ -21,6 +22,20 @@ try {
}
}
const hookSchemas = {
passport: Joi.func().arity(1),
router: Joi.func().arity(1),
context: Joi.object().pattern(/\w/, Joi.func().maxArity(1)),
hooks: Joi.object({
pre: Joi.func(),
post: Joi.func()
}),
loaders: Joi.object().pattern(/\w/, Joi.object().pattern(/\w/, Joi.func())),
mutators: Joi.object().pattern(/\w/, Joi.object().pattern(/\w/, Joi.func())),
resolvers: Joi.object().pattern(/\w/, Joi.object().pattern(/\w/, Joi.func())),
typeDefs: Joi.string()
};
/**
* isInternal checks to see if a given plugin is internal, and returns true
* if it is.
@@ -122,6 +137,15 @@ class PluginSection {
hook(hook) {
return this.plugins
.filter(({module}) => hook in module)
.filter((plugin) => {
// Validate the hook.
if (hook in hookSchemas) {
Joi.assert(plugin.module[hook], hookSchemas[hook], `Plugin '${plugin.name}' failed validation for the '${hook}' hook`);
}
return true;
})
.map((plugin) => ({
plugin,
[hook]: plugin.module[hook]
-3
View File
@@ -1,6 +1,5 @@
const express = require('express');
const path = require('path');
const Joi = require('joi');
const plugins = require('../services/plugins');
const debug = require('debug')('talk:routes');
@@ -28,8 +27,6 @@ if (process.env.NODE_ENV !== 'production') {
// Inject server route plugins.
plugins.get('server', 'router').forEach((plugin) => {
Joi.assert(plugin.router, Joi.func().arity(1), `Plugin '${plugin.name}' had an error loading the passport router`);
debug(`added plugin '${plugin.plugin.name}'`);
// Pass the root router to the plugin to mount it's routes.