Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

check-data

Package Overview
Dependencies
Maintainers
1
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

check-data - npm Package Compare versions

Comparing version 1.1.7 to 1.2.0

183

index.js

@@ -21,33 +21,10 @@ let validator = require('validator')

* 递归验证器
* @param {*} data 验证数据
* @param {*} options 验证数据表达式
*/
global.Verify = function (data, options) {
// 数据导出容器
let Output = {
error: null,//错误信息
data: {},//验证容器
}
// 递归验证
let result = recursionVerify(null, data, options, Output.data, Output, data)
if (result) {
Output.error = result
}
return Output
}
/**
* 递归验证器
* @param {*} key 数据索引
* @param {*} data 验证数据
* @param {*} options 验证规则选项
* @param {*} clone 克隆容器
* @param {*} output 数据导出容器
* @param {*} origin 原始数据
* @param {*} parent 当前父级对象
* @param {*} input 原始输入数据
* @param {*} output 处理输出数据
*/
function recursionVerify(key, data, options, clone, output, origin) {
function recursionVerify(key, data, options, parent, input, output) {

@@ -64,6 +41,6 @@ // 选项为对象(引用型数据)

// 非根级时创建数组结构
// 非根对象时创建数组结构
if (key) {
clone[key] = []
clone = clone[key]
parent[key] = []
parent = parent[key]
}

@@ -74,3 +51,3 @@

let itemOptions = options[0]
let result = recursionVerify(subKey, itemData, itemOptions, clone, output, origin)
let result = recursionVerify(subKey, itemData, itemOptions, parent, input, output)
if (result) return `${key}数组Key:${result}`

@@ -253,3 +230,3 @@ }

else if (typeof options.type === 'object') {
let result = recursionVerify(key, data, options.type, clone, output, origin)
let result = recursionVerify(key, data, options.type, parent, input, output)
if (result) {

@@ -264,28 +241,6 @@ if (Array.isArray(data)) {

// 关联参数绑定
if (options["&"]) {
for (let name of options["&"]) {
if (origin[name] === undefined) {
return `${key}与${name}参数必须同时存在`
}
}
}
// 分组数据(不管data是否为空,只要定义了分组就创建对应的分组对象)
if (options.export) {
if (!output[options.export]) {
output[options.export] = {}
}
}
// 导出
if (data || data === 0) {
// 导出验证数据
clone[key] = data
// 导出分组数据
if (options.export) {
output[options.export][key] = data
}
parent[key] = data

@@ -303,6 +258,6 @@ }

// 非根级时创建对象结构
// 非根对象时创建对象结构
if (key) {
clone[key] = {}
clone = clone[key]
parent[key] = {}
parent = parent[key]
}

@@ -315,3 +270,3 @@

let itemOptions = options.$
let result = recursionVerify(subKey, itemData, itemOptions, clone, output, origin)
let result = recursionVerify(subKey, itemData, itemOptions, parent, input, output)
if (result) return result

@@ -326,3 +281,3 @@ }

let itemOptions = options[subKey]
let result = recursionVerify(subKey, itemData, itemOptions, clone, output, origin)
let result = recursionVerify(subKey, itemData, itemOptions, parent, input, output)
if (result) return result

@@ -420,6 +375,112 @@ }

//导出验证数据
clone[key] = data
parent[key] = data
}
}
/**
* 验证器
* @param {*} data 验证数据
* @param {*} options 验证数据表达式
*/
function Verify(data, options, handler = {}) {
// 数据导出容器
let output = {
error: null,//错误信息
data: {},//验证数据
}
// 预定义变量
if (handler.define) {
for (let name of handler.define) {
if (!output[name]) {
output[name] = {}
}
}
}
// 递归验证
let result = recursionVerify(null, data, options, output.data, data, output)
if (result) {
output.error = result
return output
}
// 自定义方法
if (handler.method) {
for (let path in handler.method) {
let data = output.data
let dataPath = path.split('.')
for (let name of dataPath) {
if (data[name]) {
data = data[name]
}
}
handler.method[path].call(output, data)
}
}
// 参数依赖
if (handler.depend) {
let data = output.data
for (let name in handler.depend) {
let dependArray = handler.depend[name]
for (let key of dependArray) {
if (data[key] === undefined) {
output.error = `${name}与${key}参数必须同时存在`
return output
}
}
}
}
// 按指定路径迁移数据
if (handler.path) {
for (let name in handler.path) {
let data = output.data[name]
let path = handler.path[name].split('.')
let target = output.data
// for (let key of path) {
// if (key === "$") {
// } else {
// if (target[key]) {
// target = target[key]
// } else {
// target = undefined
// break
// }
// }
// }
// target = data
}
}
// 导出参数至指定对象
if (handler.export) {
let data = output.data
for (let name in handler.export) {
if (!output[name]) {
output[name] = {}
}
let exportArray = handler.export[name]
for (let path of exportArray) {
if (data[path]) {
output[name][path] = data[path]
}
}
}
}
return output
}
// 中间件
Verify.middleware = []
Verify.use = function (fn) {
this.middleware.push(fn)
}
module.exports = Verify
{
"name": "check-data",
"version": "1.1.7",
"version": "1.2.0",
"description": "JS数据验证器",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -34,3 +34,3 @@ ## Installation

#### 验证器表达式
#### 表达式

@@ -71,3 +71,3 @@ let verify = Verify(data, {

#### 验证器表达式
#### 表达式

@@ -88,3 +88,3 @@ let verify = Verify(data, {

#### 验证器表达式
#### 表达式

@@ -103,3 +103,3 @@ let verify = Verify(data, [String])

#### 验证器表达式
#### 表达式

@@ -129,3 +129,3 @@ let verify = Verify(data, {

#### 验证器表达式
#### 表达式

@@ -147,3 +147,3 @@ let verify = Verify(data, {

#### 验证器表达式
#### 表达式

@@ -150,0 +150,0 @@ let verify = Verify(data, {

@@ -20,41 +20,63 @@ let Verify = require('../index')

let verify = Verify(data, {
"tenderName": String,
"tenderNum": String,
"tenderEndTime": Date,
"companyName": {
"type": {
"typeId": {
"type": String,
"allowNull": true
let verify = Verify(data,
{
"tenderName": String,
"tenderNum": String,
"tenderEndTime": Date,
"companyName": {
"type": {
"typeId": {
"type": String,
"allowNull": true
}
},
"allowNull": true
},
"beneficiariesName": String,
"guaranteeMoney": Number,
"customizeGuaranteeFormat": {
"type": [String],
"allowNull": true
},
"guaranteeFormat": {
"type": Number
},
"addressee": {
"type": String
},
"phone": String,
"receiveAddress": String,
"coupon": {
"type": String
},
"integral": {
"type": Number,
"allowNull": true
},
"email": {
"type": String,
"allowNull": true
}
},
{
"define": ["filter", "uu"],
"method": {
"addressee": function (value) {
this.data.xxx = value
},
"coupon": function (value) {
this.data.kkk = value
}
},
"allowNull": true
},
"beneficiariesName": String,
"guaranteeMoney": Number,
"customizeGuaranteeFormat": {
"type": [String],
"allowNull": true
},
"guaranteeFormat": {
"type": Number,
"&": ["addressee", "phone", "receiveAddress"]
},
"addressee": String,
"phone": String,
"receiveAddress": String,
"coupon": {
"type": String,
"export": 'filter'
},
"integral": {
"type": Number,
"allowNull": true
},
"email": {
"type": String,
"allowNull": true
"depend": {
"guaranteeFormat": ["addressee", "phone"]
},
// "path": {
// "addressee": "data.xx.$.sss"
// },
"export": {
"filter": ["coupon", "companyName", "phone"]
}
}
})
)

@@ -61,0 +83,0 @@ if (verify.error) {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc