UNPKG

egg-input-regex-filter

Version:

filter data which from user side use regex, and returns a result data whose format is declared by coder.

121 lines (98 loc) 4.1 kB
# egg-input-regex-filter [![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Test coverage][codecov-image]][codecov-url] [![David deps][david-image]][david-url] [![Known Vulnerabilities][snyk-image]][snyk-url] [![npm download][download-image]][download-url] [npm-image]: https://img.shields.io/npm/v/egg-input-regex-filter.svg?style=flat-square [npm-url]: https://npmjs.org/package/egg-input-regex-filter [travis-image]: https://img.shields.io/travis/eggjs/egg-input-regex-filter.svg?style=flat-square [travis-url]: https://travis-ci.org/eggjs/egg-input-regex-filter [codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-input-regex-filter.svg?style=flat-square [codecov-url]: https://codecov.io/github/eggjs/egg-input-regex-filter?branch=master [david-image]: https://img.shields.io/david/eggjs/egg-input-regex-filter.svg?style=flat-square [david-url]: https://david-dm.org/eggjs/egg-input-regex-filter [snyk-image]: https://snyk.io/test/npm/egg-input-regex-filter/badge.svg?style=flat-square [snyk-url]: https://snyk.io/test/npm/egg-input-regex-filter [download-image]: https://img.shields.io/npm/dm/egg-input-regex-filter.svg?style=flat-square [download-url]: https://npmjs.org/package/egg-input-regex-filter <!-- Description here. --> ## Install 安装 ```bash $ npm i egg-input-regex-filter --save ``` ## Usage 启用 ```js // {app_root}/config/plugin.js exports.inputRegexFilter = { enable: true, package: 'egg-input-regex-filter', }; ``` ## Configuration 配置 ```js // {app_root}/config/config.default.js exports.inputRegexFilter = { }; ``` see [config/config.default.js](config/config.default.js) for more detail. ## Example 例子 ```js /** * 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](https://github.com/eggjs/egg/issues). ## License [MIT](LICENSE)