Skip to content

Commit 1ad5f85

Browse files
committed
2 parents 8c333a0 + 7ff6c14 commit 1ad5f85

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

lib/validate.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,30 @@ module.exports = function() {
99

1010
return function * (next) {
1111
debug('init koa-validate');
12+
this.checkQuery = function(key) {
13+
return new Validator(this, key, this.request.query[key], key in this.request.query,this.request.query);
14+
};
15+
this.checkParams = function(key) {
16+
return new Validator(this, key, this.params[key], key in this.params,this.params);
17+
};
1218
this.checkBody = function(key) {
13-
if('undefined' == typeof this.request.body) {
19+
var body = this.request.body;
20+
21+
if(!body) {
1422
if(!this.errors){
1523
this.errors = ['no body to check'];
1624
}
25+
return new Validator(this, null, null,false, null ,false );
1726
}
18-
var body = this.request.body.fields || this.request.body; // koa-body fileds
19-
return new Validator(this, key, body[key], key in body , body );
20-
};
21-
this.checkQuery = function(key) {
22-
return new Validator(this, key, this.request.query[key], key in this.request.query,this.request.query );
23-
};
24-
this.checkParams = function(key) {
25-
return new Validator(this, key, this.params[key], key in this.params,this.params);
27+
var body = body.fields || body; // koa-body fileds. multipart fields in body.fields
28+
return new Validator(this, key, body[key], key in body , body);
2629
};
2730
this.checkFile = function(key , deleteOnCheckFailed) {
2831
if('undefined' == typeof this.request.body || 'undefined' == typeof this.request.body.files ) {
2932
if(!this.errors){
30-
this.errors = ['no body to check'];
33+
this.errors = ['no file to check'];
3134
}
35+
return new Validator(this, null, null,false, null,false );
3236
}
3337
deleteOnCheckFailed = ('undefined' == typeof deleteOnCheckFailed?true :false);
3438
var files = this.request.body.files;
@@ -42,17 +46,14 @@ module.exports = function() {
4246

4347
var v = require('validator');
4448

45-
function Validator(context, key, value, exists, params) {
49+
function Validator(context, key, value, exists, params , goOn) {
4650
debug('validate for %s %s', key, value);
4751
this.params = params;
4852
this.context = context;
4953
this.key = key;
5054
this.value = value;
5155
this.exists = exists;
52-
// this.status = 0;
53-
//this.context.errors = [];
54-
//this.isNumber = false;
55-
this.goOn = true;
56+
this.goOn = (false===goOn?false:true);
5657
if(this.value && this instanceof FileValidator && 'goOn' in this.value ){
5758
this.goOn = this.value.goOn;
5859
}
@@ -674,7 +675,7 @@ function formatSize(size){
674675
use koa-body ,file object will be {type:"image/jpeg",path:"",name:"",size:"",mtile:""}
675676
*/
676677
function FileValidator(context, key, value, exists, params,deleteOnCheckFailed){
677-
Validator.call(this,context, key, value, exists, params);
678+
Validator.call(this,context, key, value, exists, params ,true);
678679
this.deleteOnCheckFailed = deleteOnCheckFailed;
679680
}
680681
require("util").inherits(FileValidator, Validator);

test/test_nobody.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
var koa = require('koa'),
4+
request = require('supertest');
5+
require('should');
6+
7+
8+
function create_app(){
9+
var app = koa();
10+
// app.use(require('koa-body')({multipart:true , formidable:{keepExtensions:true}}));
11+
app.use(require('../lib/validate.js')());
12+
app.use(require('koa-router')(app));
13+
return app;
14+
}
15+
16+
describe('koa-validate' , function(){
17+
it("nobody to check" , function(done){
18+
var app = create_app();
19+
app.post('/nobody',function*(){
20+
this.checkBody('body').notEmpty();
21+
if(this.errors) {
22+
this.status=500;
23+
}else{
24+
this.status=200;
25+
}
26+
});
27+
var req = request(app.listen());
28+
req.post('/nobody')
29+
.send({body:"no"})
30+
.expect(500 , done);
31+
});
32+
33+
});

0 commit comments

Comments
 (0)