Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zorro-del-caribe/koa-json-schema
json schema validation middleware for koajs
https://github.com/zorro-del-caribe/koa-json-schema
Last synced: 24 days ago
JSON representation
json schema validation middleware for koajs
- Host: GitHub
- URL: https://github.com/zorro-del-caribe/koa-json-schema
- Owner: zorro-del-caribe
- License: mit
- Created: 2016-08-03T18:42:59.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-23T20:38:02.000Z (almost 2 years ago)
- Last Synced: 2024-04-14T04:27:50.850Z (7 months ago)
- Language: TypeScript
- Size: 61.5 KB
- Stars: 4
- Watchers: 3
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# koa-json-schema
[![CircleCI](https://circleci.com/gh/zorro-del-caribe/koa-json-schema.svg?style=svg)](https://circleci.com/gh/zorro-del-caribe/koa-json-schema)
[json schema](http://json-schema.org/) validation middleware for [koajs](http://koajs.com/) using [AJV](https://github.com/epoberezkin/ajv).
## install
`` npm install --save koa-json-schema``
## usage
validate input as
```Javascript
const input = Object.assign({}, ctx.request.query, ctx.request.body, ctx.params);
``````Javascript
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 AJVif the input is not valid 422 error is thrown. the validation errors can be found as error.error_description
```Javascript
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
});
```