check-data
Advanced tools
Comparing version 1.3.2 to 1.3.3
63
index.js
@@ -5,17 +5,3 @@ "use strict"; | ||
// 自定义类型 | ||
let customize = { | ||
// mongoDB ID | ||
ObjectId(data, key) { | ||
if (!validator.isMongoId(data + '')) { | ||
return `${key}参数必须为ObjectId` | ||
} | ||
}, | ||
// 手机号 | ||
MobilePhone(data, key) { | ||
if (!validator.isMobilePhone(data + '', 'zh-CN')) { | ||
return `${key}参数必须为手机号` | ||
} | ||
} | ||
} | ||
let customize = require('./customize') | ||
@@ -33,6 +19,6 @@ /** | ||
// 选项为对象(引用型数据) | ||
// 选项为对象 | ||
if (typeof options === 'object') { | ||
// 选项为数组结构 | ||
// 选项为数组(数据结构) | ||
if (Array.isArray(options)) { | ||
@@ -62,3 +48,3 @@ | ||
// 选项为验证器(type作为保留关键字,禁止作为参数名使用,且只允许定义数据类型,不能定义数据结构) | ||
// 选项为验证表达式(type作为保留关键字,只允许定义数据类型,不能用作参数名) | ||
if (options.type) { | ||
@@ -241,2 +227,24 @@ | ||
// 自定义构建方法 | ||
if (options.method) { | ||
let result = options.method.call(input, data) | ||
// 对象空值过滤 | ||
if (typeof result === 'object') { | ||
let start = true | ||
for (let key in result) { | ||
if (result[key]) { | ||
start = false | ||
} else { | ||
delete result[key] | ||
} | ||
} | ||
if (start) return | ||
} | ||
data = result | ||
} | ||
// 重命名 | ||
@@ -251,3 +259,3 @@ if (options.rename) { | ||
// 选项为对象结构 | ||
// 选项为对象(数据结构) | ||
else { | ||
@@ -275,3 +283,3 @@ | ||
// 指定验证器 | ||
// 子集递归验证 | ||
else { | ||
@@ -291,6 +299,6 @@ for (let subKey in options) { | ||
// 选项为非对象(赋值型数据) | ||
// 选项为非对象 | ||
else { | ||
// 选项为函数(JS内置数据类型) | ||
// 选项为函数 | ||
if (typeof options === 'function') { | ||
@@ -361,7 +369,8 @@ | ||
// 自定义构造方法 | ||
// 自定义构建方法 | ||
else { | ||
// 空值过滤 | ||
let result = options.call(input, output) | ||
// 对象空值过滤 | ||
if (typeof result === 'object') { | ||
@@ -376,5 +385,3 @@ let start = true | ||
} | ||
if (start) { | ||
return | ||
} | ||
if (start) return | ||
} | ||
@@ -405,3 +412,2 @@ | ||
// 通过Path获取数据 | ||
@@ -420,3 +426,2 @@ function pathGetData(data, path) { | ||
/** | ||
@@ -423,0 +428,0 @@ * 验证器 |
{ | ||
"name": "check-data", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "JS数据验证器", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -71,3 +71,6 @@ let Verify = require('../index') | ||
"type": String, | ||
"allowNull": true | ||
"allowNull": true, | ||
method(value) { | ||
return [value, "7777"] | ||
} | ||
}, | ||
@@ -74,0 +77,0 @@ "ooooo": function () { |
20859
6
530