Parameters validator plugin for koa-oai-router.
npm i koa-oai-router-parameters --save
field | type | info |
---|---|---|
name | string |
parameters |
evoked fields | string |
parameters |
evoked value | object |
OpenApi parameter object |
options | object |
header ,path ,query ,formData ,body ,ajv ,handler |
options
{object}
header
{boolean}
Enable header validator. defaulttrue
path
{boolean}
Enable path validator. defaulttrue
query
{boolean}
Enable query validator. defaulttrue
formData
{boolean}
Enable formData validator. defaulttrue
body
{boolean}
Enable body validator. defaulttrue
ajv
{object|function}
object
is options of Ajv.function
is a factory with arguments(Ajv)
and must return a ajv instance.handler
{function}
Response handler when validate fail. Having arguments(ctx, next, {errors, endpoint, field, fieldValue, operation, operationValue})
. Default handler response Ajv Error to body. e.g.[{ "keyword":"type", "dataPath":".size", "schemaPath":"#/properties/size/type", "params":{ "type":"number" }, "message":"should be number" }]
Simple code:
const Koa = require('koa');
const Router = require('koa-oai-router');
const middlewareLoader = require('koa-oai-router-middleware');
const parametersHandler = require('koa-oai-router-parameters');
const app = new Koa();
const router = new Router({
apiDoc: './api',
});
router.mount(parametersHandler());
router.mount(middlewareLoader('./controllers'));
app.use(bodyParser());
app.use(router.routes());