Skip to content

Commit

Permalink
fix:
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardong committed Aug 18, 2019
1 parent 73f6d1f commit 8fcd9de
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
### wec-tools
### 简介

``wec-tools`` 是基于[koa2](https://koa.bootcss.com/)[kou-router](https://github.com/ZijianHe/koa-router#readme)开发的工具类,目前包含验证器模块(WecValidator),用于API参数校验

![](https://img.shields.io/badge/node-%3E%3D%20v6.10.2-brightgreen)

![](https://img.shields.io/badge/npm-%3E%3D%20v6.10.3-brightgreen)

![](https://img.shields.io/npm/v/npm?label=npm)


### 快速开始

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wec-tools",
"version": "0.0.6",
"version": "0.0.7",
"description": "wec-tools",
"main": "src/index.js",
"files": [
Expand Down
4 changes: 2 additions & 2 deletions src/pakages/exception/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Exception extends Error {
class WecException extends Error {

constructor(msg, errorCode) {
super()
Expand All @@ -8,4 +8,4 @@ class Exception extends Error {
}
}

module.exports = Exception
module.exports = WecException
33 changes: 21 additions & 12 deletions src/pakages/wec-validator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class WecValidator {
*/
async validate(ctx, alias) {
// 获取所有参数,组成一个集合
this.data = Object.assign(ctx.request.header, ctx.params, ctx.query, ctx.body)
this.data = Object.assign(ctx.request.header, ctx.params, ctx.request.query, ctx.request.body)
// 所有的自定义验证方法
this.validators.funs = this.getAllMethodNames(this)
// 所有的自定义属性
Expand All @@ -61,14 +61,26 @@ class WecValidator {
// 获取到rules
let rules = _this[key] // 获取所有需要验证的规则
let ruleValue = _this.data[key] //获取该字段的输入值
if (!ruleValue) {
_this.setErrors(key, ruleValue + '不能为空')
let isOptional = rules[0].valiateFunction === 'isOptional'
if (isOptional) {
rules.splice(0, 1)
if (ruleValue !== null && ruleValue !== undefined && ruleValue !== '') {
rules.forEach(rule => {
if (!validator[rule.valiateFunction](ruleValue, rule.options)) {
_this.setErrors(key, rule.msg)
}
})
}
} else {
rules.forEach(rule => {
if (!validator[rule.valiateFunction](ruleValue, rule.options)) {
_this.setErrors(key, rule.msg)
}
})
if (ruleValue === null || ruleValue === undefined || ruleValue === '') {
_this.setErrors(key, ruleValue + '不能为空')
} else {
rules.forEach(rule => {
if (!validator[rule.valiateFunction](ruleValue, rule.options)) {
_this.setErrors(key, rule.msg)
}
})
}
}
})
}
Expand All @@ -79,7 +91,7 @@ class WecValidator {
funs.forEach(fun => {
let key = fun.replace(prefix, '')
try {
_this[fun](_this.data)
this[fun](_this.data)
} catch (error) {
_this.setErrors(key, error.message)
}
Expand Down Expand Up @@ -144,7 +156,4 @@ class WecValidator {

}




module.exports = WecValidator
18 changes: 14 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,26 @@ function getAllMethodNames(obj) {
return keys
}

function getAllProperties(obj){
let vals= Object.keys(obj)

function getAllProperties(obj) {
let vals = Object.keys(obj)
}

let cc = new C()
// console.log(Object.getOwnPropertyNames(cc))
// console.log(Object.keys(new C()))
// let cm = getAllMethodNames(cc)
console.log(Object.keys(cc))
// console.log(Object.keys(cc))

for (let t in new A()) {
console.log(t)
}
console.log('------------------------')
console.log(Object.getOwnPropertyNames(new A()))
console.log(Object.getPrototypeOf(new A()))
console.log(Object.getOwnPropertySymbols(new A()))
console.log(Reflect.ownKeys(new A()))
console.log(Reflect.ownKeys(Object.getPrototypeOf(new A())))
console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(new A())))

// cm.forEach(c => {
// console.log(cc[c]())
Expand Down

0 comments on commit 8fcd9de

Please sign in to comment.