Coder Social home page Coder Social logo

koa-oai-router-parameters's Introduction

Koa-OAI-Router-Parameters

License Node Version NPM Version Build Status Test Coverage Downloads Dependency Status

Parameters validator plugin for koa-oai-router.

Installation

npm i koa-oai-router-parameters --save

Info

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. default true
    • path {boolean} Enable path validator. default true
    • query {boolean} Enable query validator. default true
    • formData {boolean} Enable formData validator. default true
    • body {boolean} Enable body validator. default true
    • 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());

koa-oai-router-parameters's People

Contributors

amazing-gao avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

koa-oai-router-parameters's Issues

Parameters are not validating?

Hi,

I am new to this plugin, but have managed to get it up and running using the example provided. However it doesn't seem to validate the incoming data.

Code related to koa/router is as below;

const app = new koa();
const router = new Router({
  apiDoc: path.resolve(__dirname, 'api'),
});

router.mount(routerMiddleware, path.resolve(__dirname, 'controllers'));
router.mount(new parametersHandler());

app
.use(bodyParser())
.use(router.routes())

A sample swagger piece for /user/{id} route is defined as(I have remove some parts to make it short);

'/user/{id}':
get:
operationId: getUserById
x-oai-middleware:
- file: users
handler: getUserById
parameters:
- name: access_token
in: header
description: JWT token
required: true
type: string
- name: id
in: path
description: 'The name that needs to be fetched. Use user1 for testing. '
required: true
type: integer
responses:
'200':
description: OK
schema:
$ref: '#/definitions/ApiResponse'

And the controller method for about in /controllers directory is;

async function getUserById(ctx:koa.Context, next:Function) {
try {
ctx.response.status = 200;
ctx.response.body = {}
} catch (err) {
console.log(err)
}
}

Then I used developer tools to compose a request and in that I didn't provide the token and also i gave the {id} parameter a string (which should be a number). But the response is always 200 and I didn't receive any error related to invalid/missing parameter(s)

What am i missing here?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.