Skip to content

Commit

Permalink
fix(validation): Added support for middleware field validation
Browse files Browse the repository at this point in the history
  • Loading branch information
maticzav committed May 13, 2018
1 parent cf63955 commit 54587ae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ function validateMiddleware(
`Field ${type}.${field} exists in middleware but is missing in Schema.`,
)
}

if (!isMiddlewareFunction(middleware[type][field])) {
throw new MiddlewareError(
`Expected ${type}.${field} to be a function but found ` +
typeof middleware[type][field],
)
}
})
}
})
Expand Down
21 changes: 20 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const schemaMiddlewareAfter = async (resolve, parent, args, context, info) => {
return 'changed'
}

// Wrong Middleware
// Middleware Validation

const middlewareWithUndefinedType = {
Wrong: () => ({}),
Expand All @@ -133,6 +133,12 @@ const middlewareWithUndefinedField = {
},
}

const middlewareWithObjectField = {
Query: {
before: false,
},
}

// Test ----------------------------------------------------------------------

// Field
Expand Down Expand Up @@ -519,3 +525,16 @@ test('Middleware Error - Schema undefined field', async t => {
),
)
})

test('Middleware Error - Middleware field is not a function.', async t => {
const schema = getSchema()

const res = t.throws(() => {
applyMiddleware(schema, middlewareWithObjectField)
})

t.deepEqual(
res,
MiddlewareError(`Expected Query.before to be a function but found boolean`),
)
})

0 comments on commit 54587ae

Please sign in to comment.