Koa2 JSON Schema
Middleware for validate request body by JSON schemas.
Install
$ npm i koa2-json-schema
Tests
$ npm test
Basic usage
const Koa = const Router = const bodyParser = const jsonSchema = const app = const router = router appapp app
Advanced usage
const Koa = const Router = const bodyParser = const jsonSchema = const app = const router = router appapp app
Error handling
By default, by sending the incorrect data in body, the server will return an error. This is the default error handler in koa2-json-schema.
ctxstatus = 400ctxbody = error: errors
But if you want to transfer control to the next middleware you must set the flag.
router
Strict mode
You can enable strict mode which throw errors when client sent extra field in body. So, if you want enable strict mode, set true
in third argument in jsonSchema
function.
router
Locales
You can easily switch locales or create custom.
Native locales:
// import russian localesconst ru = // passed new locales into argument when requireconst jsonSchema = ru
Custom locales:
// %property% and %type% are dynamic variablesconst jsonSchema = propertyIsRequired: '%property% is really required, bro 😞' invalidValueType: '%property% must be %type% type 😊'
License
MIT.