|
|
|
@ -1,18 +1,19 @@ |
|
|
|
function validate(schema, property) { |
|
|
|
// Return a Koa middleware function
|
|
|
|
return (ctx, next) => { |
|
|
|
if (schema) { |
|
|
|
let params = |
|
|
|
ctx[property] != null |
|
|
|
? ctx[property] |
|
|
|
: ctx.request[property] != null |
|
|
|
? ctx.request[property] |
|
|
|
: null |
|
|
|
const { error } = schema.validate(params) |
|
|
|
if (error) { |
|
|
|
ctx.throw(400, `Invalid ${property} - ${error.message}`) |
|
|
|
return |
|
|
|
} |
|
|
|
if (!schema) { |
|
|
|
return next() |
|
|
|
} |
|
|
|
let params = null |
|
|
|
if (ctx[property] != null) { |
|
|
|
params = ctx[property] |
|
|
|
} else if (ctx.request[property] != null) { |
|
|
|
params = ctx.request[property] |
|
|
|
} |
|
|
|
const { error } = schema.validate(params) |
|
|
|
if (error) { |
|
|
|
ctx.throw(400, `Invalid ${property} - ${error.message}`) |
|
|
|
return |
|
|
|
} |
|
|
|
return next() |
|
|
|
} |
|
|
|
|