@@ -6,7 +6,8 @@ var data = {
6
6
{ category : "reference" ,
7
7
author : "Nigel Rees" ,
8
8
title : "Sayings of the Century" ,
9
- price : 8.95
9
+ price : 8.95 ,
10
+ publishDate :"2015-01-01"
10
11
} ,
11
12
{ category : "fiction" ,
12
13
author : "Evelyn Waugh" ,
@@ -62,4 +63,47 @@ describe('koa-validate' , function(){
62
63
. expect ( 200 , done ) ;
63
64
} ) ;
64
65
66
+ } ) ;
67
+
68
+ describe ( 'koa-validate type' , function ( ) {
69
+ it ( "type check" , function ( done ) {
70
+ var app = appFactory . create ( 1 ) ;
71
+ app . router . post ( '/json' , function * ( ) {
72
+ this . checkBody ( '/' ) . notEmpty ( ) ;
73
+ this . checkBody ( '/store/book[0]/price' ) . get ( 0 ) . type ( 'number' ) . type ( "primitive" )
74
+ this . checkBody ( '/store/book[0]/price' ) . get ( 0 ) . type ( 'hello' ) // should warn
75
+ this . checkBody ( '#/store/book[0]/category' ) . first ( ) . type ( 'string' ) ;
76
+ this . checkBody ( '/store/book[*]/price' ) . type ( 'array' )
77
+ this . checkBody ( '/store/book[0]/publishDate' ) . get ( 0 ) . toDate ( ) . type ( 'date' ) . type ( 'object' )
78
+ if ( this . errors ) {
79
+ this . status = 500 ;
80
+ } else {
81
+ this . status = 200 ;
82
+ }
83
+ } ) ;
84
+ var req = request ( app . listen ( ) ) ;
85
+ req . post ( '/json' )
86
+ . send ( data )
87
+ . expect ( 200 , done ) ;
88
+ } ) ;
89
+ it ( "type fail check" , function ( done ) {
90
+ var app = appFactory . create ( 1 ) ;
91
+ app . router . post ( '/json' , function * ( ) {
92
+ this . checkBody ( '/' ) . type ( 'null' ) ;
93
+ this . checkBody ( '/store/book[0]/price' ) . get ( 0 ) . type ( 'string' ) ;
94
+ this . checkBody ( '#/store/book[0]/category' ) . first ( ) . type ( 'null' ) ;
95
+ this . checkBody ( '/store/book[*]/price' ) . type ( 'nullorundefined' )
96
+ this . checkBody ( '/store/book[0]/publishDate' ) . first ( ) . toDate ( ) . type ( 'array' )
97
+ console . log ( this . errors )
98
+ if ( this . errors && 5 == this . errors . length ) {
99
+ this . status = 200 ;
100
+ } else {
101
+ this . status = 500 ;
102
+ }
103
+ } ) ;
104
+ var req = request ( app . listen ( ) ) ;
105
+ req . post ( '/json' )
106
+ . send ( data )
107
+ . expect ( 200 , done ) ;
108
+ } ) ;
65
109
} ) ;
0 commit comments