@@ -9,26 +9,30 @@ module.exports = function() {
9
9
10
10
return function * ( next ) {
11
11
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
+ } ;
12
18
this . checkBody = function ( key ) {
13
- if ( 'undefined' == typeof this . request . body ) {
19
+ var body = this . request . body ;
20
+
21
+ if ( ! body ) {
14
22
if ( ! this . errors ) {
15
23
this . errors = [ 'no body to check' ] ;
16
24
}
25
+ return new Validator ( this , null , null , false , null , false ) ;
17
26
}
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 ) ;
26
29
} ;
27
30
this . checkFile = function ( key , deleteOnCheckFailed ) {
28
31
if ( 'undefined' == typeof this . request . body || 'undefined' == typeof this . request . body . files ) {
29
32
if ( ! this . errors ) {
30
- this . errors = [ 'no body to check' ] ;
33
+ this . errors = [ 'no file to check' ] ;
31
34
}
35
+ return new Validator ( this , null , null , false , null , false ) ;
32
36
}
33
37
deleteOnCheckFailed = ( 'undefined' == typeof deleteOnCheckFailed ?true :false ) ;
34
38
var files = this . request . body . files ;
@@ -42,17 +46,14 @@ module.exports = function() {
42
46
43
47
var v = require ( 'validator' ) ;
44
48
45
- function Validator ( context , key , value , exists , params ) {
49
+ function Validator ( context , key , value , exists , params , goOn ) {
46
50
debug ( 'validate for %s %s' , key , value ) ;
47
51
this . params = params ;
48
52
this . context = context ;
49
53
this . key = key ;
50
54
this . value = value ;
51
55
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 ) ;
56
57
if ( this . value && this instanceof FileValidator && 'goOn' in this . value ) {
57
58
this . goOn = this . value . goOn ;
58
59
}
@@ -674,7 +675,7 @@ function formatSize(size){
674
675
use koa-body ,file object will be {type:"image/jpeg",path:"",name:"",size:"",mtile:""}
675
676
*/
676
677
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 ) ;
678
679
this . deleteOnCheckFailed = deleteOnCheckFailed ;
679
680
}
680
681
require ( "util" ) . inherits ( FileValidator , Validator ) ;
0 commit comments