Added joi plugin validation

This commit is contained in:
Wyatt Johnson
2017-04-06 10:39:36 -06:00
parent e70cfdfd00
commit 1f367e666b
10 changed files with 54 additions and 0 deletions
+3
View File
@@ -3,6 +3,7 @@ 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
@@ -10,6 +11,8 @@ const debug = require('debug')('talk:graph:context');
* @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,5 +1,6 @@
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
@@ -46,6 +47,10 @@ 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,4 +1,5 @@
const _ = require('lodash');
const Joi = require('joi');
const debug = require('debug')('talk:graph:loaders');
const Actions = require('./actions');
@@ -23,6 +24,8 @@ 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,4 +1,5 @@
const _ = require('lodash');
const Joi = require('joi');
const debug = require('debug')('talk:graph:mutators');
const Comment = require('./comment');
@@ -17,6 +18,8 @@ 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,4 +1,5 @@
const _ = require('lodash');
const Joi = require('joi');
const debug = require('debug')('talk:graph:resolvers');
const ActionSummary = require('./action_summary');
@@ -50,6 +51,8 @@ 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,6 +3,7 @@
// 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');
@@ -20,6 +21,8 @@ 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;