diff --git a/js/server/modules/org/arangodb/foxx/request_context.js b/js/server/modules/org/arangodb/foxx/request_context.js index e66d711bec..a68381b394 100644 --- a/js/server/modules/org/arangodb/foxx/request_context.js +++ b/js/server/modules/org/arangodb/foxx/request_context.js @@ -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); }; } diff --git a/js/server/tests/shell-foxx.js b/js/server/tests/shell-foxx.js index a648f8582c..de79fdebe1 100644 --- a/js/server/tests/shell-foxx.js +++ b/js/server/tests/shell-foxx.js @@ -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 = {},