Skip to content

Commit

Permalink
fix: get koa request body from correct prop (#7)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: if you were passing something that looked like a koa request, we now get the `body` from `ctx.request.body`
  • Loading branch information
ForbesLindesay authored Jan 28, 2019
1 parent ed7fb77 commit f8d0277
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/__tests__/output/ComplexExample.valiator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ export function validateKoaRequest(
validator: any,
ctx: Context,
): any => {
const data = (ctx as any)[prop];
const data =
prop === 'body'
? ctx.request && (ctx.request as any).body
: (ctx as any)[prop];
if (validator) {
const valid = validator(data);

Expand Down
8 changes: 5 additions & 3 deletions src/__tests__/printValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ test('validateRequest', () => {
id: 0,
value: 'hello',
},
body: {
id: 1,
value: '2019-01-25T19:09:28.179Z',
request: {
body: {
id: 1,
value: '2019-01-25T19:09:28.179Z',
},
},
throw: (number: number, message: string) => {
throw new Error(`${number} <KOA ERROR> ${message}`);
Expand Down
2 changes: 1 addition & 1 deletion src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const VALIDATE_KOA_REQUEST_IMPLEMENTATION = `export function validateKoaR
validator: any,
ctx: Context,
): any => {
const data = (ctx as any)[prop];
const data = prop === 'body' ? ctx.request && (ctx.request as any).body : (ctx as any)[prop];
if (validator) {
const valid = validator(data);
Expand Down

0 comments on commit f8d0277

Please sign in to comment.