1
0
Fork 0

Merge branch 'devel' of github.com:arangodb/ArangoDB into devel

This commit is contained in:
Willi Goesgens 2015-08-11 16:13:58 +02:00
commit 85d1d2b718
2 changed files with 69 additions and 3 deletions

View File

@ -94,11 +94,11 @@ function isJoi(schema) {
});
}
function validateOrThrow(raw, schema, allowInvalid) {
function validateOrThrow(raw, schema, allowInvalid, validateOptions) {
if (!isJoi(schema)) {
return raw;
}
var result = joi.validate(raw, schema);
var result = joi.validate(raw, schema, validateOptions);
if (result.error && !allowInvalid) {
throw new UnprocessableEntity(result.error.message.replace(/^"value"/, 'Request body'));
}
@ -439,6 +439,7 @@ class RequestContext {
var type = attributes.type,
description = attributes.description,
allowInvalid = attributes.allowInvalid,
validateOptions = {},
cfg, construct;
if (attributes.isJoi) {
@ -476,8 +477,16 @@ class RequestContext {
}
});
}
if (cfg.options) {
if (!is.array(cfg.options)) {
cfg.options = [cfg.options];
}
_.each(cfg.options, function (options) {
_.extend(validateOptions, options);
});
}
construct = function (raw) {
return validateOrThrow(raw, type, allowInvalid);
return validateOrThrow(raw, type, allowInvalid, validateOptions);
};
}

View File

@ -829,6 +829,63 @@ function DocumentationAndConstraintsSpec () {
assertTrue(called);
},
testSetParamForJoiBodyParamWithAllowUnknownTrue: function () {
var req = { parameters: {} },
res = {},
paramName = 'flurb',
description = stub(),
requestBody = {x: 1, y: 2},
schema = {x: joi.number().integer().required()},
called = false;
schema = joi.object().keys(schema).options({allowUnknown: true});
allow(req)
.toReceive("body")
.andReturn(requestBody);
app.get('/foxx', function (providedReq) {
called = _.isEqual(providedReq.parameters[paramName], {x: 1, y: 2});
}).bodyParam(paramName, {
description: description,
type: schema
});
var callback = transformRoute(routes[0].action);
callback(req, res);
assertTrue(called);
},
testSetParamForJoiBodyParamWithAllowUnknownFalse: function () {
var req = { parameters: {} },
res = {},
paramName = 'flurb',
description = stub(),
requestBody = {x: 1, y: 2},
schema = {x: joi.number().integer().required()},
called = false;
schema = joi.object().keys(schema).options({allowUnknown: false});
allow(req)
.toReceive("body")
.andReturn(requestBody);
app.get('/foxx', function (providedReq) {
called = true;
}).bodyParam(paramName, {
description: description,
type: schema
});
var callback = transformRoute(routes[0].action);
callback(req, res);
assertFalse(called);
assertEqual(res.responseCode, 422);
},
testSetParamForPureJoiBodyParam: function () {
var req = { parameters: {} },
res = {},