index.js
function validate (validationObj) {
  // Return a Koa middleware function
  return (ctx, next) => {
    try {
      // Validate each request data object in the Koa context object
      validateObject(ctx.headers, 'Headers', validationObj.headers, { allowUnknown: true })
      validateObject(ctx.params, 'URL Parameters', validationObj.params)
      validateObject(ctx.query, 'URL Query', validationObj.query)

      if (ctx.request.body) {
        validateObject(ctx.request.body, 'Request Body', validationObj.body)
      }

      return next()
    } catch (err) {
      // If any of the objects fails validation, send an HTTP 400 response.
      ctx.throw(400, err.message)
    }
  }
}
function validate (validationObj) {
  // Return a Koa middleware function
  return (ctx, next) => {
    try {
      // Validate each request data object in the Koa context object
      validateObject(ctx.headers, 'Headers', validationObj.headers, { allowUnknown: true })
      validateObject(ctx.params, 'URL Parameters', validationObj.params)
      validateObject(ctx.query, 'URL Query', validationObj.query)

      if (ctx.request.body) {
        validateObject(ctx.request.body, 'Request Body', validationObj.body)
      }

      return next()
    } catch (err) {
      // If any of the objects fails validation, send an HTTP 400 response.
      ctx.throw(400, err.message)
    }
  }
}
function validate ( validationObj ) { // Return a Koa middleware function return ( ctx , next ) => { try { // Validate each request data object in the Koa context object validateObject ( ctx . headers , 'Headers' , validationObj . headers , { allowUnknown : true } ) validateObject ( ctx . params , 'URL Parameters' , validationObj . params ) validateObject ( ctx . query , 'URL Query' , validationObj . query ) if ( ctx . request . body ) { validateObject ( ctx . request . body , 'Request Body' , validationObj . body ) } return next ( ) } catch ( err ) { // If any of the objects fails validation, send an HTTP 400 response. ctx . throw ( 400 , err . message ) } } }
Generate a Koa middleware function to validate a request using
the provided validation objects.

@param {Object} validationObj
@param {Object} validationObj.headers The request headers schema
@param {Object} validationObj.params The request params schema
@param {Object} validationObj.query The request query schema
@param {Object} validationObj.body The request body schema
@returns A validation middleware function.
Generate a Koa middleware function to validate a request using the provided validation objects .