Skip to content

Commit e15a539

Browse files
committed
add type tests
1 parent 3792fee commit e15a539

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

test/test_jsonPath.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ var data = {
66
{ category: "reference",
77
author: "Nigel Rees",
88
title: "Sayings of the Century",
9-
price: 8.95
9+
price: 8.95,
10+
publishDate:"2015-01-01"
1011
},
1112
{ category: "fiction",
1213
author: "Evelyn Waugh",
@@ -62,4 +63,47 @@ describe('koa-validate' , function(){
6263
.expect(200 , done);
6364
});
6465

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+
});
65109
});

0 commit comments

Comments
 (0)