3.0.1 • Published 4 years ago

koa-json-schema v3.0.1

Weekly downloads
21
License
MIT
Repository
github
Last release
4 years ago

koa-json-schema

CircleCI

json schema validation middleware for koajs using AJV.

install

npm install --save koa-json-schema

usage

validate input as

const input = Object.assign({}, ctx.request.query, ctx.request.body, ctx.params);
const koa=require('koa')
const {middleware: validator} = require('koa-json-schema')

koa()
    .use(validator(schema, options))
    .use(function (ctx){
       // do something with safe input
    });
  • schema: a valid json schema
  • options: options to pass to AJV

if the input is not valid 422 error is thrown. the validation errors can be found as error.error_description

koa()
    .use(async function (ctx, next){
        try {
            await next();
        } catch(e){
           if(e.status === 422){
                console.log(e.error_description);
           }
        }
    })
    .use(validator(schema, options))
    .use(function (ctx){
       // do something with safe input
    });
3.0.1

4 years ago

3.0.0

5 years ago

2.0.1

6 years ago

2.0.0

7 years ago

1.0.0

8 years ago