mirror of
https://github.com/zhigang1992/deployd.git
synced 2026-06-03 19:42:30 +08:00
merged validation
This commit is contained in:
@@ -20,6 +20,16 @@ var router = module.exports = function (req, res, next) {
|
||||
, method = req.method
|
||||
, path = '/'
|
||||
;
|
||||
|
||||
// query sugar for JSON based query strings
|
||||
// eg ?q={"foo": {"bar": true}}
|
||||
if(req.query && req.query.q && req.query.q[0] === '{') {
|
||||
req.query = JSON.parse(req.query.q);
|
||||
if (req.query.$orderby) {
|
||||
req.sort = req.query.$orderby;
|
||||
delete req.query.$orderby;
|
||||
}
|
||||
}
|
||||
|
||||
// sanitize parts
|
||||
if(!parts[0]) parts.shift();
|
||||
|
||||
@@ -38,11 +38,6 @@ middleware.listen = function (callback) {
|
||||
// by default pause the request
|
||||
req.pause();
|
||||
|
||||
// query sugar for JSON based query strings
|
||||
// eg ?q={"foo": {"bar": true}}
|
||||
if(req.query && req.query.q && req.query.q[0] === '{') {
|
||||
req.query = JSON.parse(req.query.q);
|
||||
}
|
||||
|
||||
if(req.method === 'GET' || req.method === 'DELETE') return next();
|
||||
|
||||
@@ -146,10 +141,10 @@ middleware.listen = function (callback) {
|
||||
if(err instanceof Error) {
|
||||
res.statusCode = 500;
|
||||
res.send({message: err.message});
|
||||
} else {
|
||||
res.statusCode = err.status || 400;
|
||||
res.send(err);
|
||||
}
|
||||
|
||||
res.statusCode = err.status || 400;
|
||||
res.send(err);
|
||||
});
|
||||
|
||||
// start the server
|
||||
|
||||
@@ -54,9 +54,10 @@ module.exports = function (req, res, next) {
|
||||
try {
|
||||
sanitized[key] = new Date(req.body[key]).toISOString();
|
||||
} catch(e) {
|
||||
sanitized[key] = false;
|
||||
sanitized[key] = 'invalid date';
|
||||
}
|
||||
|
||||
} else if (resource.properties[key].type === 'boolean' && !req.body[key]) {
|
||||
sanitized[key] = false;
|
||||
} else {
|
||||
sanitized[key] = req.body[key];
|
||||
}
|
||||
@@ -134,7 +135,8 @@ function transform(validation) {
|
||||
}
|
||||
|
||||
// rename and add human readable errors
|
||||
validation.validation = validation.errors;
|
||||
// validation.validation = validation.errors;
|
||||
delete validation.valid;
|
||||
validation.errors = err;
|
||||
|
||||
return validation;
|
||||
|
||||
@@ -37,8 +37,7 @@ data = {
|
||||
optional: false
|
||||
},
|
||||
completed: {
|
||||
type: "boolean",
|
||||
optional: true
|
||||
type: "boolean"
|
||||
},
|
||||
order: {
|
||||
type: "number",
|
||||
|
||||
@@ -8,14 +8,35 @@ describe('Resource Actions', function(){
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should support the $orderby flag', function(done) {
|
||||
var query = {
|
||||
$orderby: {title: 1}
|
||||
};
|
||||
var queryJson = encodeURI(JSON.stringify(query));
|
||||
|
||||
todos.post({title: 'c'}, function(e1) {
|
||||
todos.post({title: 'b'}, function(e2) {
|
||||
todos.post({title: 'a'}, function(e3) {
|
||||
todos.use('?q=' + queryJson).get(function(err, body, req, res) {
|
||||
expect(body).to.exist;
|
||||
expect(body.length).to.equal(3);
|
||||
expect(body[0].title).to.equal('a');
|
||||
expect(body[1].title).to.equal('b');
|
||||
expect(body[2].title).to.equal('c');
|
||||
done(e1, e2, e3, err);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
})
|
||||
|
||||
describe('POST /todos', function(){
|
||||
it('should return an error when provided invalid data', function(done) {
|
||||
todos.post({foo: 123, completed: 'flarg'}, function (err, todo, req, res) {
|
||||
expect(err).to.exist;
|
||||
expect(err.valid).to.equal(false);
|
||||
expect(err.validation).to.have.length(2);
|
||||
expect(err.errors).to.be.a('object');
|
||||
expect(todo).to.not.exist;
|
||||
done();
|
||||
|
||||
Reference in New Issue
Block a user