check-data
Advanced tools
Comparing version 1.7.8 to 1.8.0
31
index.js
@@ -5,12 +5,10 @@ "use strict" | ||
let schema = require('./schema') | ||
let methods = require('./methods') | ||
let { methods, extend } = require('./methods') | ||
class Parser { | ||
class validator { | ||
constructor(data, options, key) { | ||
constructor(data, options) { | ||
this.data = data | ||
this.options = options | ||
return this.recursion(data, options, key) | ||
return this.recursion(data, options, '') | ||
} | ||
@@ -202,3 +200,3 @@ | ||
else if (methods[options]) { | ||
if (data === undefined || data === '') { | ||
@@ -230,3 +228,3 @@ return { data } | ||
let output = new validator(data, options, '') | ||
let output = new Parser(data, options) | ||
@@ -253,9 +251,16 @@ if (output.error) { | ||
// 预定义数据模型表达式 | ||
Validator.schema = schema | ||
// 自定义扩展方法 | ||
Validator.use = function (type, options) { | ||
methods[type] = options | ||
} | ||
// 验证类型扩展 | ||
Validator.use = extend | ||
// 通过将静态的options放入函数作用域,使options持久化驻留在内存 | ||
// 避免同一个对象被重复的创建和销毁,实现options跨接口复用,提升性能的同时,也增加了代码复用率 | ||
Validator.schema = function (name, options) { | ||
Validator[name] = function (json) { | ||
return Validator(json, options) | ||
} | ||
return Validator[name] | ||
} | ||
module.exports = Validator | ||
@@ -225,10 +225,2 @@ "use strict" | ||
// 自定义扩展 | ||
function extend(type, options) { | ||
methods[type] = options | ||
} | ||
module.exports = { | ||
methods, | ||
extend, | ||
} | ||
module.exports = methods |
{ | ||
"name": "check-data", | ||
"version": "1.7.8", | ||
"description": "JS对象验证器", | ||
"main": "index.js", | ||
"scripts": { | ||
"dev": "nodemon test/example" | ||
}, | ||
"author": "https://github.com/xiangle/verify.git", | ||
"license": "ISC", | ||
"dependencies": { | ||
"filter-null": "^1.2.0", | ||
"validator": "^8.0.0" | ||
}, | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"devDependencies": { | ||
"nodemon": "^1.11.0" | ||
}, | ||
"keywords": [ | ||
"JSON", | ||
"schema", | ||
"validator", | ||
"validation", | ||
"jsonschema", | ||
"json-schema", | ||
"json-schema-validator", | ||
"json-schema-validation" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/xiangle/verify.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/xiangle/verify/issues" | ||
}, | ||
"homepage": "https://github.com/xiangle/verify#readme" | ||
} | ||
"name": "check-data", | ||
"version": "1.8.0", | ||
"description": "JS对象验证器", | ||
"main": "index.js", | ||
"scripts": { | ||
"dev": "nodemon test/schema" | ||
}, | ||
"author": "https://github.com/xiangle/verify.git", | ||
"license": "ISC", | ||
"dependencies": { | ||
"filter-null": "^1.2.0", | ||
"validator": "^8.0.0" | ||
}, | ||
"devDependencies": { | ||
"nodemon": "^1.11.0" | ||
}, | ||
"keywords": [ | ||
"JSON", | ||
"schema", | ||
"validator", | ||
"validation", | ||
"jsonschema", | ||
"json-schema", | ||
"json-schema-validator", | ||
"json-schema-validation" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/xiangle/verify.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/xiangle/verify/issues" | ||
}, | ||
"homepage": "https://github.com/xiangle/verify#readme" | ||
} |
@@ -19,3 +19,3 @@ ### Installation | ||
* `customize.$` *Function* - 自定义数据导出方法,使用验证后的数据创建新的数据结构,this和函数第一个参数指向已验证数据。该函数返回值中支持多层内嵌函数表达式,用于动态生成返回值。 | ||
* `customize.$` *Function* - 自定义数据导出方法,使用已验证数据创建新的数据结构,函数中this和第一个参数指向已验证数据。函数返回对象中支持多层嵌套函数表达式,用于添加自定义业务逻辑,满足特定应用场景。 | ||
@@ -26,3 +26,3 @@ ### 输出 | ||
* `data` *Objcte* - 经过验证、处理后导出数据(带空值过滤,用于剔除对象中的空数组、空字符串、undefind、null等无效数据) | ||
* `data` *Objcte* - 经过验证、处理后导出数据,内置空值过滤,自动剔除对象、数组中的空字符串、undefind值。(更多空值过滤特性请参考[filter-null模块](https://github.com/xiangle/filter-null)) | ||
@@ -103,9 +103,49 @@ * `msg` *String* - 验证失败后返回的错误信息,相对于error而言,msg对用户更加友好,可直接在客户端显示 | ||
### 自定义扩展 | ||
### 自定义数据类型 | ||
使用Validator.use(typename, options) | ||
> 可添加自定义数据类型验证 | ||
Validator.use(name, options) | ||
### 数组验证 | ||
### schema验证 | ||
> 通过预定义schema,实现options复用,性能更优 | ||
Validator.schema(name, options) | ||
### 参考示例 | ||
#### schema验证 | ||
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, | ||
} | ||
// let { error, data } = schema(json) | ||
let { error, data } = Validator.demo(json) | ||
#### 数组验证 | ||
let { error, data } = Validator(["a", "b", "c"], [String]) | ||
@@ -128,3 +168,3 @@ | ||
### 对象验证 | ||
#### 对象验证 | ||
@@ -144,3 +184,3 @@ let { error, data } = Validator({ | ||
### and验证 | ||
#### and验证 | ||
@@ -174,3 +214,3 @@ let { error, data } = Validator({ | ||
### or验证 | ||
#### or验证 | ||
@@ -196,3 +236,3 @@ let { error, data } = Validator({ | ||
### 扩展类型验证 | ||
#### 扩展类型验证 | ||
@@ -207,3 +247,3 @@ let { error, data } = Validator({ | ||
### 自定义类型 | ||
#### 自定义扩展类型 | ||
@@ -220,3 +260,3 @@ Validator.use('Int', { | ||
### 实例 | ||
#### 完整示例 | ||
@@ -226,3 +266,3 @@ # 输入数据 | ||
"username": "测试", | ||
"tenderNum": "123456789987", | ||
"num": "123456789987", | ||
"time": "2017-07-07T09:53:30.000Z", | ||
@@ -270,3 +310,3 @@ "files": ["abc.js", "334", "null", "666", , , "kkk.js"], | ||
}, | ||
"tenderNum": String, | ||
"num": String, | ||
"time": { | ||
@@ -273,0 +313,0 @@ "type": Date, |
"use strict" | ||
let Validator = require('../index') | ||
let Validator = require('..') | ||
@@ -5,0 +5,0 @@ |
"use strict" | ||
let Validator = require('../index') | ||
let Validator = require('..') | ||
let json = { | ||
"tenderName": "测试", | ||
"tenderNum": "123456789987", | ||
"name": "测试", | ||
"num": "123456789987", | ||
"ObjectId": "59c8aea808deec3fc8da56b6", | ||
@@ -50,9 +50,9 @@ "tenderEndTime": "2017-07-07T09:53:30.000Z", | ||
{ | ||
"tenderName": { | ||
"name": { | ||
"type": String, | ||
"name": "标书名称", | ||
"name": "名称", | ||
"allowNull": false, | ||
"default": "默认值" | ||
}, | ||
"tenderNum": { | ||
"num": { | ||
"type": Number, | ||
@@ -150,9 +150,14 @@ "value": 666, | ||
}, | ||
where() { | ||
where({ search, email, integral }) { | ||
return { | ||
"email": undefined, | ||
"integral": '', | ||
"t": 888, | ||
"j": undefined, | ||
"c": '8', | ||
"email": email, | ||
"integral": integral, | ||
"test": { | ||
v1: 1, | ||
v2: undefined, | ||
v3: "", | ||
v4: null, | ||
v5: NaN, | ||
v6: 0, | ||
} | ||
} | ||
@@ -159,0 +164,0 @@ } |
"use strict" | ||
let Validator = require('../index') | ||
let Validator = require('..') | ||
@@ -11,3 +11,3 @@ | ||
} else { | ||
return { err: '必须为Int类型' } | ||
return { error: '必须为Int类型' } | ||
} | ||
@@ -20,3 +20,3 @@ }, | ||
{ | ||
"name": 666, | ||
"name": 666.5, | ||
}, | ||
@@ -26,3 +26,3 @@ { | ||
"type": 'Int', | ||
"name": "标书名称", | ||
"name": "名称", | ||
"allowNull": false, | ||
@@ -29,0 +29,0 @@ "default": "默认值", |
"use strict" | ||
let Validator = require('../index') | ||
let Validator = require('..') | ||
let { error, data } = Validator( | ||
@@ -12,3 +11,3 @@ { | ||
}, | ||
b: "xx", | ||
b: "666", | ||
}, | ||
@@ -15,0 +14,0 @@ { |
"use strict" | ||
let Validator = require('../index') | ||
let Validator = require('..') | ||
@@ -5,0 +5,0 @@ |
"use strict" | ||
let Validator = require('../index') | ||
let Validator = require('..') | ||
let Test = Validator.schema('Test', | ||
{ | ||
"tenderName": { | ||
"type": String, | ||
"name": "标书名称", | ||
"allowNull": false, | ||
"default": "默认值" | ||
let schema = Validator.schema('demo', { | ||
a: { | ||
a1: { | ||
type: Number, | ||
allowNull: false | ||
}, | ||
"tenderNum": { | ||
"type": Number, | ||
"value": 666, | ||
}, | ||
"user": { | ||
"username": String, | ||
"age": Number, | ||
"address": [ | ||
{ | ||
"city": String, | ||
}, | ||
{ | ||
"city": String, | ||
} | ||
], | ||
a2: { | ||
type: Number, | ||
allowNull: false | ||
} | ||
} | ||
) | ||
}, | ||
b: Number, | ||
}) | ||
// console.log(Test) | ||
let data1 = { | ||
a: { | ||
a1: "jj", | ||
a2: "12", | ||
}, | ||
b: 2, | ||
c: 888, | ||
} | ||
let o = { | ||
o1: 16 | ||
let data2 = { | ||
a: { | ||
a1: 666, | ||
a2: "12", | ||
}, | ||
b: 2, | ||
c: 888, | ||
} | ||
function a() { | ||
console.log(this) | ||
// let { error, data } = schema(data1) | ||
let result1 = Validator.demo(data1) | ||
if (result1.error) { | ||
console.log(result1.error) | ||
} else { | ||
console.log(result1.data) | ||
} | ||
let oa = a.bind(o) | ||
let result2 = Validator.demo(data2) | ||
a() | ||
oa() | ||
if (result2.error) { | ||
console.log(result2.error) | ||
} else { | ||
console.log(result2.data) | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29550
728
374
11