
Security News
/Research
Popular node-ipc npm Package Infected with Credential Stealer
Socket detected malicious node-ipc versions with obfuscated stealer/backdoor behavior in a developing npm supply chain attack.
validate-chain
Advanced tools
表单验证链,以及中文验证错误反馈信息
npm install --save validate-chain
// es5
var VC = require("validate-chain")
// es6
import VC form "validate-chain"
bower install --save validate-chain
<script src="路径/validate-chain-browser.js"></script>
// VC 为全局变量
var vc = new VC( objectData )
vc.check("name").required("名字为必填项");
//console.log( vc.errors )
以NODE作为后端开发单页应用时,前后端对表单的验证和消毒的逻辑以及返回的提示信息其实是一样的,应该做到前后端共用验证逻辑。目前后端可以使用的koa-validate,express-validate等验证中间件不能在前端使用。验证器应该去检查多个字段并在最后返回一个提示信息的数组,和已经消毒后的数据;
import VC from "validate-chain";
var objectData = {age:22,name:"eisneim",gender:"guy",email:"ss.kjk",nested:{a:{b:{v:33}}}}
validator.loginForm() => {
var vc = new VC( objectData )
vc.check("email").email()
.check("desc").alias("描述").required()
.check("opt").optional().max(2,"must not bigger than 2");
//或者
vc.check("name").required("名字为必填项").$apply(function(value){
// 自定义的判断逻辑
return value && value.length>2
},"名字的长度至少两个字符");
vc.check("age").required().min(23).numeric().in([22,33,44])//可以一直链下去
vc.check("gender").alias("性别").regx(/male|female/).in(["男","女"])
// 多层结构
vc.check("nested.a.b.v").max(30)
console.log( vc.errors )
// 如果没有任何错误,vc.errors === null
//["描述: 为必填字段", "age: 最小值为23", "性别: 不合格/male|female/的格式", "ss.kjk不是常规的email"]
console.log( vc.sanitized )
// {name: "eisneim"}
// vc.errorFields: [ "desc", "age", ... ]
}
const mock = {
sub: {
age: 10,
name: 'terry'
}
}
it('compose and check sub doc', function() {
var vc = new VC(mock)
vc.compose('sub', function(child) {
child.check('age').min(18)
child.check('name').min(2)
})
expect(vc.errors).to.have.length(1)
expect(vc.sanitized.sub.name).to.equal('terry')
})
it('use stand alone vc function', function() {
var childChecker = function(){
var vv = new VC(mock.sub)
vv.check('age').min(18)
.check('name').min(2)
return vv
}
var vc = new VC(mock)
vc.compose('sub', childChecker)
expect(vc.errors).to.have.length(1)
expect(vc.sanitized.sub.name).to.equal('terry')
})
describe('vc.flatedArray', function() {
var flated = {
'id1':{name:'eisneim',age:17},
'id2':{name:'terry',age:22},
}
var data = {
flated: flated,
}
it('check flatedArray for each child', function() {
var vc = new VC(flated)
vc.flatedArray(function(each) {
each.check('name').required()
.check('age').min(18)
})
expect(vc.errors).to.have.length(1)
expect(vc.sanitized.id2.age).to.equal(22)
})
it('deal with flatedArray as a child', function() {
var vc = new VC(data)
vc.flatedArray('flated', function(each) {
each.check('name').required()
.check('age').min(18)
})
expect(vc.errors).to.have.length(1)
expect(vc.sanitized.flated.id2.age).to.equal(22)
})
it('accept stand alone function', function() {
var childChecker = function(checker, eachData){
var vv = new VC(eachData)
return vv
.check('name').required()
.check('age').min(18)
}
var vc = new VC(data)
vc.flatedArray('flated', childChecker)
expect(vc.errors).to.have.length(1)
expect(vc.sanitized.flated.id2.age).to.equal(22)
})
})
如果一个字段的值为数组,可以使用array(callback)来进行检查
var data = {
levels:[ 1,3,4,5],
posts:[
{title:"some title ",date:"2014-20-3 12:22" },//错误日期
{title:"不和谐的标题",date:"2014-12-3 12:22" }],
}
var vc = new VC( data );
vc.check("levels").alias("等级").array(function(item,index){
// 对于数组里的没一个值都检查一遍,如果fail,错误信息将被记录
item.max(3)
})
vc.check("posts").array( function( item,index ){
// 如果数组了的元素是一个对象,则使用.check来进行检查(目前不支持数组内部元素的消毒);
item.check("date").date();
item.check("name").required();
})
expect(vc.errors).to.have.length(5); // -> pass
/**
[ '等级.2: 最大值为3',
'等级.3: 最大值为3',
'posts.0.date: 2014-20-3 12:22不符合日期格式',
'posts.0: 为必填字段',
'posts.1: 为必填字段'
] **/
想要被保存到vc.sanitized对象中的字段,必须使用vc.check("name"),如果该字段为可选的,则应该使用vc.check("name").optional(); 目前版本只支持第一层数组的元素的消毒
var data = {
name:" 小明 ",
nested:{
name:" 小明 "
},
array:[" 小明 "],
objOfArray:[
{name:" 小明 "},
]
}
vc.check("name").required().trim();
vc.check("nested.name").required().trim();
vc.check("array").required().array(function(item,index){
item.trim()
})
vc.check("objOfArray").required().array(function(item,index){
item.check("name").required().trim();
})
console.log( vc.sanitized ) // {name:"小明",....}
// 测试
gulp test
// build
gulp build
FAQs
The npm package validate-chain receives a total of 20 weekly downloads. As such, validate-chain popularity was classified as not popular.
We found that validate-chain demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
/Research
Socket detected malicious node-ipc versions with obfuscated stealer/backdoor behavior in a developing npm supply chain attack.

Security News
TeamPCP and BreachForums are promoting a Shai-Hulud supply chain attack contest with a $1,000 prize for the biggest package compromise.

Security News
Packagist urges PHP projects to update Composer after a GitHub token format change exposed some GitHub Actions tokens in CI logs.