0.0.2 • Published 5 years ago

koa-router-json-schema v0.0.2

Weekly downloads
544
License
MIT
Repository
-
Last release
5 years ago

JSON schema validator for koa-router

It's middleware for koa-router that allows to use json schema validation for a request query and body and a response body.

Example

import Router from 'koa-router';
import createValidationMdw from 'koa-router-json-schema';
import { getSomeObject } from './someController';

const router = new Router();

router.get(
  '/reverse-geocode',
  createValidationMdw({
    query: {
      type: 'object',
      properties: {
        place_id: {
          type: 'string',
        },
      },
      required: [ 'place_id' ],
    },
    response: {
      200: {
        type: 'object',
        properties: {
          status: { type: 'string', pattern: 'OK' },
          data: {
            type: 'array',
            items: {
              type: 'object',
              additionalProperties: true,
            },
          },
        },
        required: [ 'status', 'data' ],
      },
    },
  }),
  getSomeObject,
);