1.3.0 • Published 6 years ago

egg-input-regex-filter v1.3.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

egg-input-regex-filter

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install 安装

$ npm i egg-input-regex-filter --save

Usage 启用

// {app_root}/config/plugin.js
exports.inputRegexFilter = {
  enable: true,
  package: 'egg-input-regex-filter',
};

Configuration 配置

// {app_root}/config/config.default.js
exports.inputRegexFilter = {
};

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

Example 例子

/**
 * use this plugin in egg.js,  this plugin map its func to both app and ctx
 * ,so you can use it like this: 
 * app/ctx.filterReg(dataObj, regexObj, quickRtn = true, rtnFn = null) , 
 * app/ctx.jsonRtn(code, data = {}, errMsg = '', okCode = 0 ), 
 * app/ctx.errRtn(errMsgObj )
 * 
 * use in controller/service , see below: 
 * 在eggjs中的使用方法: 
 */

var postDataObject = {
    "username": "xiaoming",
    "password": "12345678",
    "nickname": "apples"
}
//postDtaObj = this.ctx.request.body 

//if filter defined a field ,but it was not be seted in dataObj 
//it will be seted automaticly by filter , default value is '' 
filterObject  = {
    username: [
        {re:/^\w{5,15}$/, errCode: -1, errMsg: 'length must between 5 to 15'},
        {re:/^[a-zA-Z]+\w*$/, errCode: -1, errMsg: 'must starts  with char'},
    ],
    "password": [{re:/^\w{5,15}$/, errCode: -1, errMsg: 'length must between 5 to 15'},],
    //we will not check nickname field 
    //"nickname": [{re:/^\w{5,15}$/, errCode: -1, errMsg: 'length must between 5 to 15'},]
}

//fRtn is an obj has two attr , retData and err
let fRtn = this.app.filterReg(postDataObject, filterObject )

//so you can do it like this 
if(JSON.stringify(fRtn.err ) != "{}" ){     //fRtn.err not empty
    var errMsgJson = this.app.errRtn(fRtn.err )
    this.ctx.body = errMsgJson 
    return 
}
//if you also is a phper , you can use phpjs through: const pfn = require("phpjs") 
//it has many powerful funcs ,you will like it. above "if" can be write as:
// if(!pfn.empty(fRtn.err ) ) 

/**
    that's all above . if you like it has an extra method named jsonRtn()
    look as this :  jsonRtn(code, data = {}, errMsg = '', okCode = 0 )
    if code not eq okCode ,errMsg must be  defined , and the param data must be always an Object
    you can set it as {} if you do not want to set it anything. 
    normal return is : {code: int, data:{some data object here} }
    when err return param errMsg will be seted in data ,like this: 
    {code: int, data:{some data object here, errMsg: 'some err info'} }
    call it like this:  this.ctx.body = this.app.jsonRtn(-1, {}, 'some err happened' ) 
    
    attention: all the method above called by this.app  ,  also can be call by this.ctx 
enjoy!
*/

Questions & Suggestions

Please open an issue here.

License

MIT

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago