kopia lustrzana https://github.com/c9/core
Allow optional parameters to be falsy
rodzic
340a218b6d
commit
58afdf088c
|
@ -137,6 +137,10 @@ module.exports = function Route(route, options, handler, types) {
|
||||||
var key = keys[i];
|
var key = keys[i];
|
||||||
var param = params[key];
|
var param = params[key];
|
||||||
var type = param.type;
|
var type = param.type;
|
||||||
|
if (param.optional && value == null) {
|
||||||
|
match[key] = value;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
value = type.parse(value);
|
value = type.parse(value);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -157,7 +161,7 @@ module.exports = function Route(route, options, handler, types) {
|
||||||
* the decoded and validated parameters are stored in `req.params`
|
* the decoded and validated parameters are stored in `req.params`
|
||||||
* otherwhise an error is returned.
|
* otherwhise an error is returned.
|
||||||
*/
|
*/
|
||||||
function decodeParams(req, res, next) {
|
var decodeParams = this.decodeParams = function(req, res, next) {
|
||||||
var urlParams = req.match;
|
var urlParams = req.match;
|
||||||
if (!urlParams) return;
|
if (!urlParams) return;
|
||||||
|
|
||||||
|
@ -197,10 +201,12 @@ module.exports = function Route(route, options, handler, types) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
value = body[key]; // body is already JSON parsed
|
value = body[key]; // body is already JSON parsed
|
||||||
|
if (param.optional && value == null)
|
||||||
|
break;
|
||||||
isValid = type.check(value);
|
isValid = type.check(value);
|
||||||
break;
|
break;
|
||||||
case "query":
|
case "query":
|
||||||
if (param.optional && !(key in query))
|
if (param.optional && query[key] == null)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -211,7 +217,7 @@ module.exports = function Route(route, options, handler, types) {
|
||||||
isValid = isValid === false ? false : type.check(value);
|
isValid = isValid === false ? false : type.check(value);
|
||||||
break;
|
break;
|
||||||
case "url":
|
case "url":
|
||||||
if (param.optional && !(key in urlParams))
|
if (param.optional && urlParams[key] == null)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
value = urlParams[key]; // is already parsed and checked
|
value = urlParams[key]; // is already parsed and checked
|
||||||
|
|
|
@ -133,7 +133,65 @@ module.exports = {
|
||||||
|
|
||||||
assert.ok(!route.match(req, "/ts/353676299181"));
|
assert.ok(!route.match(req, "/ts/353676299181"));
|
||||||
assert.ok(!route.match(req, "/ts/abc"));
|
assert.ok(!route.match(req, "/ts/abc"));
|
||||||
}
|
},
|
||||||
|
|
||||||
|
"test router: decode parameter in body": function(next) {
|
||||||
|
var route = new Route("/user", {
|
||||||
|
params: {
|
||||||
|
id: {
|
||||||
|
type: "int",
|
||||||
|
optional: true,
|
||||||
|
source: "body"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, sinon.stub());
|
||||||
|
|
||||||
|
var req = {
|
||||||
|
match: "match",
|
||||||
|
parsedUrl: {
|
||||||
|
query: ""
|
||||||
|
},
|
||||||
|
body: { id: 15 }
|
||||||
|
};
|
||||||
|
var res = {};
|
||||||
|
|
||||||
|
// Note: usually optionals would say 'source: "body",'
|
||||||
|
// but this should work
|
||||||
|
route.decodeParams(req, res, function(err, result) {
|
||||||
|
assert.equal(err, null);
|
||||||
|
assert.equal(req.params.id, 15);
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
"test router: optional number argument can be falsy": function(next) {
|
||||||
|
var route = new Route("/user", {
|
||||||
|
params: {
|
||||||
|
id: {
|
||||||
|
type: "int",
|
||||||
|
optional: true,
|
||||||
|
source: "body"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, sinon.stub());
|
||||||
|
|
||||||
|
var req = {
|
||||||
|
match: "match",
|
||||||
|
parsedUrl: {
|
||||||
|
query: ""
|
||||||
|
},
|
||||||
|
body: { id: null }
|
||||||
|
};
|
||||||
|
var res = {};
|
||||||
|
|
||||||
|
// Note: usually optionals would say 'source: "body",'
|
||||||
|
// but this should work
|
||||||
|
route.decodeParams(req, res, function(err, result) {
|
||||||
|
assert.equal(err, null);
|
||||||
|
assert.equal(req.params.id, null);
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
!module.parent && require("asyncjs").test.testcase(module.exports).exec();
|
!module.parent && require("asyncjs").test.testcase(module.exports).exec();
|
||||||
|
|
Ładowanie…
Reference in New Issue