Git Product home page Git Product logo

egg-super-validator's Introduction

egg-super-validator

egg controller 参数验证中间件

Install

$ npm i egg-super-validator --save

Usage

// {app_root}/config/plugin.js
exports.superValidator = {
  enable: true,
  package: 'egg-super-validator',
};

Configuration

exports.superValidator = {
    // 默认作为中间件, 如果不需要则设置为 false
    // 可以在 config.onerror 中统一错误处理
    middleware: true,
    // 自定义 superstruct types
    types: {},
    // 出现错误时的处理
    // middleware 设置为 false 时不生效
    formatError (ctx, err) {
        ctx.status = 200;
        ctx.body = {
            code: 400,
            desc: err.message
        }
    },
};
// {app_root}/config/config.default.js
import isNumber from 'is-number';
exports.superValidator = {
    types: {
        customType: isNumber
    }
};

// {app_root}/app/controller/home.js
const query = ctx.verify({
    id: 'customType',
    name: 'string?'
}, ctx.query);

// 更多关于 superstruct 可以参见: https://github.com/ianstormtaylor/superstruct

see config/config.default.js for more detail.

Thanks

egg-y-validator

Questions & Suggestions

Please open an issue here.

License

MIT

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.