mirror of https://gitee.com/bigwinds/arangodb
Merge branch 'devel' of github.com:arangodb/ArangoDB into devel
This commit is contained in:
commit
85d1d2b718
|
@ -94,11 +94,11 @@ function isJoi(schema) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateOrThrow(raw, schema, allowInvalid) {
|
function validateOrThrow(raw, schema, allowInvalid, validateOptions) {
|
||||||
if (!isJoi(schema)) {
|
if (!isJoi(schema)) {
|
||||||
return raw;
|
return raw;
|
||||||
}
|
}
|
||||||
var result = joi.validate(raw, schema);
|
var result = joi.validate(raw, schema, validateOptions);
|
||||||
if (result.error && !allowInvalid) {
|
if (result.error && !allowInvalid) {
|
||||||
throw new UnprocessableEntity(result.error.message.replace(/^"value"/, 'Request body'));
|
throw new UnprocessableEntity(result.error.message.replace(/^"value"/, 'Request body'));
|
||||||
}
|
}
|
||||||
|
@ -439,6 +439,7 @@ class RequestContext {
|
||||||
var type = attributes.type,
|
var type = attributes.type,
|
||||||
description = attributes.description,
|
description = attributes.description,
|
||||||
allowInvalid = attributes.allowInvalid,
|
allowInvalid = attributes.allowInvalid,
|
||||||
|
validateOptions = {},
|
||||||
cfg, construct;
|
cfg, construct;
|
||||||
|
|
||||||
if (attributes.isJoi) {
|
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) {
|
construct = function (raw) {
|
||||||
return validateOrThrow(raw, type, allowInvalid);
|
return validateOrThrow(raw, type, allowInvalid, validateOptions);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -829,6 +829,63 @@ function DocumentationAndConstraintsSpec () {
|
||||||
assertTrue(called);
|
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 () {
|
testSetParamForPureJoiBodyParam: function () {
|
||||||
var req = { parameters: {} },
|
var req = { parameters: {} },
|
||||||
res = {},
|
res = {},
|
||||||
|
|
Loading…
Reference in New Issue