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.8.1 to 2.0.0

test/null.js

43

index.js

@@ -16,2 +16,14 @@ "use strict"

/**
* 判断是否允许为空值(需要忽略的数据)
* @param {*} data 需要校验空值的数据
*/
isNull(data, ignore = [undefined, ""]) {
if (ignore.indexOf(data) > -1) {
return true
}
}
/**
* 递归验证器

@@ -32,4 +44,4 @@ * @param {*} data 验证数据

// 空值拦截
if (data === undefined || data === '') {
// 前置空值拦截
if (this.isNull(data, options.ignore)) {

@@ -46,3 +58,3 @@ // 默认

// 允许为空
// 允许空值
else if (options.allowNull === false) {

@@ -107,3 +119,3 @@ return {

if (data === undefined || data === '') {
if (this.isNull(data)) {
if (allowNull === false) {

@@ -140,3 +152,3 @@ return {

if (itemOptions.allowNull === false) {
if (subData === undefined || subData === '') {
if (this.isNull(subData)) {
return {

@@ -161,5 +173,3 @@ error: `数组${key}中key:${itemKey}值不能为空`

if (data === undefined) {
return {
data: undefined
}
return { data: undefined }
}

@@ -176,2 +186,3 @@

for (let subKey in options) {
let itemData = data[subKey]

@@ -194,2 +205,3 @@ let itemOptions = options[subKey]

}
}

@@ -208,4 +220,4 @@

if (data === undefined || data === '') {
return { data }
if (this.isNull(data)) {
return { data: undefined }
}

@@ -257,16 +269,21 @@

// 自定义扩展方法
// 自定义数据类型扩展方法
Validator.use = function (type, options) {
methods[type] = options
}
// 通过将静态的options放入函数作用域,使options持久化驻留在内存
// 避免同一个对象被重复的创建和销毁,实现options跨接口复用,提升性能的同时,也增加了代码复用率
// 通过预处理方式,将提前处理好的静态options持久化驻留在内存中
// 避免同一个对象被多次重复的创建和销毁,实现options跨接口复用,在节省资源的同时,也增加了代码复用率
Validator.schema = function (name, options, handler) {
Validator[name] = function (data) {
return Validator(data, options, handler)
}
return Validator[name]
}
module.exports = Validator

@@ -8,3 +8,3 @@ "use strict"

// 参数自定义转换方法
method({ data, option: fun, input }) {
handle({ data, option: fun, input }) {
return { data: fun.call(input, data) }

@@ -11,0 +11,0 @@ },

{
"name": "check-data",
"version": "1.8.1",
"version": "2.0.0",
"description": "JS对象验证器",

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

@@ -1,11 +0,13 @@

### Installation
### Install
npm install check-data --save
$ npm install check-data --save
### 使用方法
let Validator = require('check-data')
let { error, data } = Validator(data, options, customize)
```js
let Validator = require('check-data')
let { error, data } = Validator(data, options, customize)
```
### 输入

@@ -30,4 +32,6 @@

### options - 公共选项
### options
#### 通用选项
* `type` *String, Number, Object, Array, Date, Boolean, "MongoId", "MobilePhone", 'Email'* - 数据类型,扩展类型用字符串表示

@@ -41,4 +45,6 @@

* `allowNull` *Boolean* - 是否允许为空,默认为true
* `allowNull` *Boolean* - 是否允许为空(默认将undefined和空字符串被视为空),缺省值为true。当值为false时,必须正确匹配指定的数据类型,否则会提示数据类型错误。
* `ignore` *Array* - 忽略指定值,在字段级覆盖对默认空值的定义,如将某个指定字段的空值定义为[null, ""]
* `and` *Array、Function* - 声明依赖的参数名数组,支持数组和函数两种表达式,函数表达式用于声明指定值的依赖关系。要求依赖的所有参数都不能为空

@@ -48,7 +54,5 @@

* `method` *Function* - 参数自定义转换方法,非空值时执行
* `handle` *Function* - 参数自定义转换方法,非空值时执行(原method方法更名为handle)。
### options - 针对指定数据类型的私有选项
#### String

@@ -89,3 +93,3 @@

### 扩展类型
### 其它类型

@@ -105,26 +109,29 @@ #### 'MongoId'

### 自定义数据类型
### 扩展自定义数据类型
> 通过Validator.use()方法添加自定义的数据类型,使用方法和扩展类型一样,用字符串声明数据类型
Validator.use(name, options)
```js
Validator.use(name, options)
# 示例
Validator.use('Int', {
type({ data }) {
if (Number.isInteger(data)) {
return { data }
} else {
return { err: '必须为Int类型' }
}
},
})
# 示例
Validator.use('Int', {
type({ data }) {
if (Number.isInteger(data)) {
return { data }
} else {
return { error: '必须为Int类型' }
}
},
})
```
### schema验证
> 通过预定义schema,实现options复用,性能更优
> 通过预定义schema,实现options单例复用(option为静态数据),避免频繁创建重复的实例,可节省内存和减少计算开销。
Validator.schema(name, options)
```js
Validator.schema(name, options)
```
### 参考示例

@@ -134,247 +141,256 @@

let schema = Validator.schema('demo', {
a: {
a1: {
type: Number,
allowNull: false
},
a2: {
type: Number,
allowNull: false
}
},
b: Number,
})
let json = {
a: {
a1: "jj",
a2: "12",
},
b: 2,
c: 888,
```js
let schema = Validator.schema('demo', {
a: {
a1: {
type: Number,
allowNull: false
},
a2: {
type: Number,
allowNull: false
}
},
b: Number,
})
// let { error, data } = schema(json)
let json = {
a: {
a1: "jj",
a2: "12",
},
b: 2,
c: 888,
}
let { error, data } = Validator.demo(json)
// let { error, data } = schema(json)
let { error, data } = Validator.demo(json)
```
#### 数组验证
let { error, data } = Validator(["a", "b", "c"], [String])
```js
let { error, data } = Validator(["a", "b", "c"], [String])
let { error, data } = Validator([{
"a":1,
"b":"bibi",
"c":"test"
},{
"a":1,
"b":"bibi",
"c":"test"
}], [{
a: Number,
b: String,
c: String
}])
let { error, data } = Validator([{
"a":1,
"b":"bibi",
"c":"test"
},{
"a":1,
"b":"bibi",
"c":"test"
}], [{
a: Number,
b: String,
c: String
}])
```
#### 对象验证
let { error, data } = Validator({
"a": 1,
"b": "xx",
"c": [1,32,34],
"d": 666
}, {
"a": Number,
"b": String,
"c": [String],
"d": Number
})
```js
let { error, data } = Validator({
"a": 1,
"b": "xx",
"c": [1,32,34],
"d": 666
}, {
"a": Number,
"b": String,
"c": [String],
"d": Number
})
```
#### and验证
let { error, data } = Validator({
"username": "莉莉",
"addressee": "嘟嘟",
}, {
"username": {
"type": String,
// 使用数组表达式
"and": ["addressee", "address"],
},
"addressee": {
"type": String,
"allowNull": true
},
"address": {
"type": String,
"allowNull": true,
// 使用函数表达式,表示特定值的依赖关系
and(value){
if (value === 1) {
return ["addressee", "address"]
} else if (value === 2) {
return ["username", "xx"]
}
},
```js
let { error, data } = Validator({
"username": "莉莉",
"addressee": "嘟嘟",
}, {
"username": {
"type": String,
// 使用数组表达式
"and": ["addressee", "address"],
},
"addressee": {
"type": String,
"allowNull": true
},
"address": {
"type": String,
"allowNull": true,
// 使用函数表达式,表示特定值的依赖关系
and(value){
if (value === 1) {
return ["addressee", "address"]
} else if (value === 2) {
return ["username", "xx"]
}
})
},
}
})
```
#### or验证
let { error, data } = Validator({
"username": "莉莉",
"addressee": "嘟嘟",
}, {
"username": {
"type": String,
"or": ["addressee", "address"]
},
"addressee": {
"type": String,
"allowNull": true
},
"address": {
"type": String,
"allowNull": true
}
})
```js
let { error, data } = Validator({
"username": "莉莉",
"addressee": "嘟嘟",
}, {
"username": {
"type": String,
"or": ["addressee", "address"]
},
"addressee": {
"type": String,
"allowNull": true
},
"address": {
"type": String,
"allowNull": true
}
})
```
#### 扩展类型验证
let { error, data } = Validator({
"id": "5968d3b4956fe04299ea5c18",
"mobilePhone": "18555555555",
}, {
"id": "MongoId",
"mobilePhone": "MobilePhone"
})
```js
let { error, data } = Validator({
"id": "5968d3b4956fe04299ea5c18",
"mobilePhone": "18555555555",
}, {
"id": "MongoId",
"mobilePhone": "MobilePhone"
})
```
#### 完整示例
# 输入数据
let json = {
"username": "测试",
"num": "123456789987",
"time": "2017-07-07T09:53:30.000Z",
"files": ["abc.js", "334", "null", "666", , , "kkk.js"],
"user": {
"username": "莉莉",
"age": 18,
},
"list": [
{
"username": "吖吖",
"age": 16,
},
{
"username": "可可",
"age": 15,
}
],
"auth": {
"weixin": "abc",
},
"beneficiariesName": "莉莉",
"guaranteeMoney": 2,
"guaranteeFormat": 0,
"addressee": "嘟嘟",
"receiveAddress": "北京市",
"phone": "18666666666",
"coupon": "uuuu",
"integral": {
"lala": 168,
"kaka": "3"
},
"search": "深圳",
"email": "xxx@xx.xx"
```js
# 输入数据
let json = {
"username": "测试",
"num": "123456789987",
"time": "2017-07-07T09:53:30.000Z",
"files": ["abc.js", "334", "null", "666", , , "kkk.js"],
"user": {
"username": "莉莉",
"age": 18,
},
"list": [
{
"username": "吖吖",
"age": 16,
},
{
"username": "可可",
"age": 15,
}
],
"auth": {
"weixin": "abc",
},
"beneficiariesName": "莉莉",
"guaranteeMoney": 2,
"guaranteeFormat": 0,
"addressee": "嘟嘟",
"receiveAddress": "北京市",
"phone": "18666666666",
"coupon": "uuuu",
"integral": {
"lala": 168,
"kaka": "3"
},
"search": "深圳",
"email": "xxx@xx.xx"
}
# 验证表达式
let { error, data } = Validator(json,
{
"username": {
"type": String,
"name": "用户名",
"allowNull": false
},
"num": String,
"time": {
"type": Date,
"name": "时间",
"allowNull": false,
},
"user": {
"username": String,
"age": Number,
},
"list": [{
"username": String,
"age": Number,
}],
"auth": {
"weixin": String,
},
"beneficiariesName": String,
"guaranteeMoney": {
"type": Number,
"in": [1, 2]
},
"files": [{
"type": String,
"allowNull": false,
}],
"guaranteeFormat": {
"type": Number,
"conversion": Boolean
},
"addressee": {
"type": String,
"value": "直接通过表达式赋值"
},
"search": String,
"phone": {
"type": "MobilePhone"
},
"receiveAddress": String,
"coupon": {
"type": String,
method(value) {
return { "$gt": new Date() }
}
},
"integral": {
"lala": {
"type": Number,
},
"kaka": {
"type": Number,
"in": [1, 2, 3],
}
},
"email": {
"type": 'Email',
},
# 验证表达式
let { error, data } = Validator(json,
{
"username": {
"type": String,
"name": "用户名",
"allowNull": false
},
"num": String,
"time": {
"type": Date,
"name": "时间",
"allowNull": false,
},
"user": {
"username": String,
"age": Number,
},
"list": [{
"username": String,
"age": Number,
}],
"auth": {
"weixin": String,
},
"beneficiariesName": String,
"guaranteeMoney": {
"type": Number,
"in": [1, 2]
},
"files": [{
"type": String,
"allowNull": false,
}],
"guaranteeFormat": {
"type": Number,
"conversion": Boolean
},
"addressee": {
"type": String,
"value": "直接通过表达式赋值"
},
"search": String,
"phone": {
"type": "MobilePhone"
},
"receiveAddress": String,
"coupon": {
"type": String,
handle(value) {
return { "$gt": new Date() }
}
},
"integral": {
"lala": {
"type": Number,
},
{
filter({ search, email, integral }) {
return {
"email": email,
"integral": integral,
"test": {
a: 1,
b: undefined,
c: "",
d: null,
e: NaN,
e: 0,
},
}
}
"kaka": {
"type": Number,
"in": [1, 2, 3],
}
)
},
"email": {
"type": 'Email',
},
},
{
filter({ search, email, integral }) {
return {
"email": email,
"integral": integral,
"test": {
a: 1,
b: undefined,
c: "",
d: null,
e: NaN,
e: 0,
},
}
}
}
)
```
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