From 9d5c38570316a9f555a19365636ad98e114704b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Marques?= Date: Sat, 18 Oct 2014 00:11:59 -0300 Subject: [PATCH 1/3] Nested validation support --- lib/validate.js | 22 +++++++++++++++++++--- test/test_validate.js | 43 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/lib/validate.js b/lib/validate.js index ca7ef07..60fca44 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -10,7 +10,21 @@ module.exports = function() { return function * (next) { debug('init koa-validate'); this.checkBody = function(key) { - return new Validator(this, key, this.request.body[key], key in this.request.body , this.request.body ); + + var body = this.request.body; + var keyPath; + + if (key.indexOf('.') >= 0) { + var keys = key.split('.'); + key = keys.pop(); + keyPath = keys.join('.'); + + keys.forEach(function (_key) { + body = body[_key] || {}; + }); + } + + return new Validator(this, key, body[key], key in body, body, keyPath ); }; this.checkQuery = function(key) { return new Validator(this, key, this.request.query[key], key in this.request.query,this.request.query ); @@ -31,13 +45,14 @@ module.exports = function() { var v = require('validator'); -function Validator(context, key, value, exists, params) { +function Validator(context, key, value, exists, params, keyPath) { debug('validate for %s %s', key, value); this.params = params; this.context = context; this.key = key; this.value = value; this.exists = exists; + this.keyPath = keyPath; // this.status = 0; //this.context.errors = []; //this.isNumber = false; @@ -59,7 +74,8 @@ Validator.prototype.addError = function(tip) { this.context.errors = []; } var e = {}; - e[this.key] = tip; + var key = this.keyPath ? this.keyPath + '.' + this.key : this.key; + e[key] = tip; this.context.errors.push(e); }; diff --git a/test/test_validate.js b/test/test_validate.js index fe6b42e..fa4f2d9 100644 --- a/test/test_validate.js +++ b/test/test_validate.js @@ -67,6 +67,9 @@ describe('koa-validate' , function(){ this.checkBody('hw').isHalfWidth(); this.checkBody('vw').isVariableWidth(); this.checkBody('sp').isSurrogatePair(); + this.checkBody('object.property').notEmpty().len(3,20); + this.checkBody('object.child.property').notEmpty().len(3,20); + this.checkBody('object.child.child.property').notEmpty().len(3,20); if(this.errors){ this.body = this.errors; return; @@ -122,7 +125,16 @@ describe('koa-validate' , function(){ fw:"宽字节", hw:"a字节", vw:"v多字节", - sp:'ABC千𥧄1-2-3' + sp:'ABC千𥧄1-2-3', + object: { + property: 'property test', + child: { + property: 'child property test', + child: { + property: 'child property test' + } + } + } }) .expect(200) .expect('ok' ,done); @@ -179,8 +191,11 @@ describe('koa-validate' , function(){ this.checkBody('hw').isHalfWidth(); this.checkBody('vw').isVariableWidth(); this.checkBody('sp').isSurrogatePair(); - if(this.errors.length === 48){ - this.body = this.errors; + this.checkBody('object.property').notEmpty().len(3,20); + this.checkBody('object.child.property').notEmpty().len(3,20); + this.checkBody('object.child.child.property').notEmpty().len(3,20); + if(this.errors.length === 51){ + //this.body = this.errors; this.body = 'ok'; return ; } @@ -232,7 +247,10 @@ describe('koa-validate' , function(){ fw:"43", hw:"你好", vw:"aa", - sp:'fdfd' + sp:'fdfd', + object: { + property: '' + } }) .expect(200) .expect('ok' ,done); @@ -302,6 +320,8 @@ describe('koa-validate' , function(){ this.checkBody('hash').clone('sha1').sha1(); this.checkBody('hash').clone('num1' ,1); this.checkBody('json').toJson(); + this.checkBody('object.int_').toInt(); + this.checkBody('object.child.date').toDate(); //console.log(this.request.body) if(this.errors){ this.body = this.errors; @@ -383,6 +403,14 @@ describe('koa-validate' , function(){ if(1!=body.json.a){ this.throw(500); } + + if(20 !== body.object.int_ ){ + this.throw(500); + } + if(new Date('2014-01-01').getTime() !== body.object.child.date.getTime() ){ + this.throw(500); + } + this.body = 'ok'; }); request(app.listen()) @@ -410,7 +438,12 @@ describe('koa-validate' , function(){ debase64:"hello", base64:'aGVsbG8=', //hello hash:"hello", //md5 should be 5d41402abc4b2a76b9719d911017c592 , shal should be aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d - + object: { + int_: 20, + child: { + date:'2014-01-01' + } + } }).expect(200) .expect('ok' , done); From 55f6ee367f9b2ab110c7003526cca14d2d38cb72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Marques?= Date: Sat, 18 Oct 2014 00:24:50 -0300 Subject: [PATCH 2/3] Indentation --- lib/validate.js | 24 ++++++++-------- test/test_validate.js | 64 +++++++++++++++++++++---------------------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/lib/validate.js b/lib/validate.js index 60fca44..844da8f 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -11,18 +11,18 @@ module.exports = function() { debug('init koa-validate'); this.checkBody = function(key) { - var body = this.request.body; - var keyPath; + var body = this.request.body; + var keyPath; - if (key.indexOf('.') >= 0) { - var keys = key.split('.'); - key = keys.pop(); - keyPath = keys.join('.'); + if (key.indexOf('.') >= 0) { + var keys = key.split('.'); + key = keys.pop(); + keyPath = keys.join('.'); - keys.forEach(function (_key) { - body = body[_key] || {}; - }); - } + keys.forEach(function (_key) { + body = body[_key] || {}; + }); + } return new Validator(this, key, body[key], key in body, body, keyPath ); }; @@ -52,7 +52,7 @@ function Validator(context, key, value, exists, params, keyPath) { this.key = key; this.value = value; this.exists = exists; - this.keyPath = keyPath; + this.keyPath = keyPath; // this.status = 0; //this.context.errors = []; //this.isNumber = false; @@ -74,7 +74,7 @@ Validator.prototype.addError = function(tip) { this.context.errors = []; } var e = {}; - var key = this.keyPath ? this.keyPath + '.' + this.key : this.key; + var key = this.keyPath ? this.keyPath + '.' + this.key : this.key; e[key] = tip; this.context.errors.push(e); }; diff --git a/test/test_validate.js b/test/test_validate.js index fa4f2d9..c871d4f 100644 --- a/test/test_validate.js +++ b/test/test_validate.js @@ -67,9 +67,9 @@ describe('koa-validate' , function(){ this.checkBody('hw').isHalfWidth(); this.checkBody('vw').isVariableWidth(); this.checkBody('sp').isSurrogatePair(); - this.checkBody('object.property').notEmpty().len(3,20); - this.checkBody('object.child.property').notEmpty().len(3,20); - this.checkBody('object.child.child.property').notEmpty().len(3,20); + this.checkBody('object.property').notEmpty().len(3,20); + this.checkBody('object.child.property').notEmpty().len(3,20); + this.checkBody('object.child.child.property').notEmpty().len(3,20); if(this.errors){ this.body = this.errors; return; @@ -126,15 +126,15 @@ describe('koa-validate' , function(){ hw:"a字节", vw:"v多字节", sp:'ABC千𥧄1-2-3', - object: { - property: 'property test', - child: { - property: 'child property test', - child: { - property: 'child property test' - } - } - } + object: { + property: 'property test', + child: { + property: 'child property test', + child: { + property: 'child property test' + } + } + } }) .expect(200) .expect('ok' ,done); @@ -191,9 +191,9 @@ describe('koa-validate' , function(){ this.checkBody('hw').isHalfWidth(); this.checkBody('vw').isVariableWidth(); this.checkBody('sp').isSurrogatePair(); - this.checkBody('object.property').notEmpty().len(3,20); - this.checkBody('object.child.property').notEmpty().len(3,20); - this.checkBody('object.child.child.property').notEmpty().len(3,20); + this.checkBody('object.property').notEmpty().len(3,20); + this.checkBody('object.child.property').notEmpty().len(3,20); + this.checkBody('object.child.child.property').notEmpty().len(3,20); if(this.errors.length === 51){ //this.body = this.errors; this.body = 'ok'; @@ -248,9 +248,9 @@ describe('koa-validate' , function(){ hw:"你好", vw:"aa", sp:'fdfd', - object: { - property: '' - } + object: { + property: '' + } }) .expect(200) .expect('ok' ,done); @@ -320,8 +320,8 @@ describe('koa-validate' , function(){ this.checkBody('hash').clone('sha1').sha1(); this.checkBody('hash').clone('num1' ,1); this.checkBody('json').toJson(); - this.checkBody('object.int_').toInt(); - this.checkBody('object.child.date').toDate(); + this.checkBody('object.int_').toInt(); + this.checkBody('object.child.date').toDate(); //console.log(this.request.body) if(this.errors){ this.body = this.errors; @@ -404,12 +404,12 @@ describe('koa-validate' , function(){ this.throw(500); } - if(20 !== body.object.int_ ){ - this.throw(500); - } - if(new Date('2014-01-01').getTime() !== body.object.child.date.getTime() ){ - this.throw(500); - } + if(20 !== body.object.int_ ){ + this.throw(500); + } + if(new Date('2014-01-01').getTime() !== body.object.child.date.getTime() ){ + this.throw(500); + } this.body = 'ok'; }); @@ -438,12 +438,12 @@ describe('koa-validate' , function(){ debase64:"hello", base64:'aGVsbG8=', //hello hash:"hello", //md5 should be 5d41402abc4b2a76b9719d911017c592 , shal should be aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d - object: { - int_: 20, - child: { - date:'2014-01-01' - } - } + object: { + int_: 20, + child: { + date:'2014-01-01' + } + } }).expect(200) .expect('ok' , done); From 8babc4d06c9931edef1aced823afc644b3d84420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Marques?= Date: Tue, 1 Sep 2015 20:35:54 -0300 Subject: [PATCH 3/3] Indentation --- lib/validate.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/validate.js b/lib/validate.js index eb7fd0f..a82a186 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -27,15 +27,15 @@ module.exports = function() { var body = body.fields || body; // koa-body fileds. multipart fields in body.fields var keyPath; - if (key.indexOf('.') >= 0) { - var keys = key.split('.'); - key = keys.pop(); - keyPath = keys.join('.'); - - keys.forEach(function (_key) { - body = body[_key] || {}; - }); - } + if (key.indexOf('.') >= 0) { + var keys = key.split('.'); + key = keys.pop(); + keyPath = keys.join('.'); + + keys.forEach(function (_key) { + body = body[_key] || {}; + }); + } return new Validator(this, key, body[key], key in body, body, null, keyPath); };