Coder Social home page Coder Social logo

koa-struct's Introduction

koa-struct

Koa struct middleware

Installation

koa-struct requires

  • koa2
  • koa-body
  • koa-router
npm install koa-struct --save

Example

Basic usage

const struct = require('koa-struct');
const body = require('koa-body');
const Router = require('koa-router');
const koa = require('koa');

const app = new koa();
const router = new Router();

app
    .use(body())
    .use(struct())
    .use(router.routes())
    .use(router.allowedMethods());

router.post('/user/update', ctx => {

    ctx.struct({
        username: 'string',
        email: 'email',
        age: 'number'
    });

    ctx.body = 'ok';
});

app.listen(3000);

Validate params

router.post('/user/update/:id', ctx => {

    ctx.structParam({
        id: 'number'
    });

    ctx.body = 'ok';
});

Validate query

router.get('/user/?id=255', ctx => {

    ctx.structQuery({
        id: 'number'
    });

    ctx.body = 'ok';
});

Validation

koa-struct uses Valify to validating data, so consider it for documentation and options.

Valify options

// Globals
app.use(struct({
    autoCast: false
));

// Locals
router.post('/user/update', ctx => {
    ctx.struct({
        username: 'string',
        email: 'email',
        age: 'number'
    }, {
        autoCast: false
    });
    ctx.body = 'ok';
});

By default autoCast is set to true.

For more info about Valify click here

Changelog

You can view the changelog here

License

koa-struct is open-sourced software licensed under the MIT license

Authors

Fabio Ricali

koa-struct's People

Contributors

fabioricali avatar

Stargazers

香饽饽zizizi avatar  avatar Davide Polano avatar

Watchers

James Cloos avatar  avatar

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.