mirror of https://gitee.com/bigwinds/arangodb
Merge pull request #1021 from triAGENS/easy-params
Allow passing joi params directly
This commit is contained in:
commit
a2e56535f1
|
@ -220,13 +220,23 @@ extend(RequestContext.prototype, {
|
||||||
///
|
///
|
||||||
/// You can also provide a description of this parameter.
|
/// You can also provide a description of this parameter.
|
||||||
///
|
///
|
||||||
/// @EXAMPLES
|
/// *Examples*
|
||||||
|
///
|
||||||
|
/// ```js
|
||||||
|
/// app.get("/foxx/:id", function {
|
||||||
|
/// // Do something
|
||||||
|
/// }).pathParam("id", type: joi.number().integer().required().description("Id of the Foxx"));
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// You can also pass in a configuration object instead:
|
||||||
///
|
///
|
||||||
/// ```js
|
/// ```js
|
||||||
/// app.get("/foxx/:id", function {
|
/// app.get("/foxx/:id", function {
|
||||||
/// // Do something
|
/// // Do something
|
||||||
/// }).pathParam("id", {
|
/// }).pathParam("id", {
|
||||||
/// type: joi.number().integer().required().description("Id of the Foxx")
|
/// type: joi.number().integer(),
|
||||||
|
/// required: true,
|
||||||
|
/// description: "Id of the Foxx"
|
||||||
/// });
|
/// });
|
||||||
/// ```
|
/// ```
|
||||||
/// @endDocuBlock
|
/// @endDocuBlock
|
||||||
|
@ -239,9 +249,16 @@ extend(RequestContext.prototype, {
|
||||||
type = attributes.type,
|
type = attributes.type,
|
||||||
required = attributes.required,
|
required = attributes.required,
|
||||||
description = attributes.description,
|
description = attributes.description,
|
||||||
constraint = type,
|
constraint, regexType, cfg;
|
||||||
regexType = type,
|
|
||||||
cfg;
|
if (attributes.isJoi) {
|
||||||
|
type = attributes;
|
||||||
|
required = undefined;
|
||||||
|
description = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
constraint = type;
|
||||||
|
regexType = type;
|
||||||
|
|
||||||
// deprecated: assume type.describe is always a function
|
// deprecated: assume type.describe is always a function
|
||||||
if (type && typeof type.describe === 'function') {
|
if (type && typeof type.describe === 'function') {
|
||||||
|
@ -306,6 +323,19 @@ extend(RequestContext.prototype, {
|
||||||
/// ```js
|
/// ```js
|
||||||
/// app.get("/foxx", function {
|
/// app.get("/foxx", function {
|
||||||
/// // Do something
|
/// // Do something
|
||||||
|
/// }).queryParam("id",
|
||||||
|
/// joi.number().integer()
|
||||||
|
/// .required()
|
||||||
|
/// .description("Id of the Foxx")
|
||||||
|
/// .meta({allowMultiple: false})
|
||||||
|
/// });
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// You can also pass in a configuration object instead:
|
||||||
|
///
|
||||||
|
/// ```js
|
||||||
|
/// app.get("/foxx", function {
|
||||||
|
/// // Do something
|
||||||
/// }).queryParam("id", {
|
/// }).queryParam("id", {
|
||||||
/// type: joi.number().integer().required().description("Id of the Foxx"),
|
/// type: joi.number().integer().required().description("Id of the Foxx"),
|
||||||
/// allowMultiple: false
|
/// allowMultiple: false
|
||||||
|
@ -319,8 +349,17 @@ extend(RequestContext.prototype, {
|
||||||
var type = attributes.type,
|
var type = attributes.type,
|
||||||
required = attributes.required,
|
required = attributes.required,
|
||||||
description = attributes.description,
|
description = attributes.description,
|
||||||
constraint = type,
|
allowMultiple = attributes.allowMultiple,
|
||||||
cfg;
|
constraint, cfg;
|
||||||
|
|
||||||
|
if (attributes.isJoi) {
|
||||||
|
type = attributes;
|
||||||
|
required = undefined;
|
||||||
|
description = undefined;
|
||||||
|
allowMultiple = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
constraint = type;
|
||||||
|
|
||||||
// deprecated: assume type.describe is always a function
|
// deprecated: assume type.describe is always a function
|
||||||
if (type && typeof type.describe === 'function') {
|
if (type && typeof type.describe === 'function') {
|
||||||
|
@ -330,6 +369,9 @@ extend(RequestContext.prototype, {
|
||||||
if (typeof description === 'string') {
|
if (typeof description === 'string') {
|
||||||
constraint = constraint.description(description);
|
constraint = constraint.description(description);
|
||||||
}
|
}
|
||||||
|
if (typeof allowMultiple === 'boolean') {
|
||||||
|
constraint = constraint.meta({allowMultiple: allowMultiple});
|
||||||
|
}
|
||||||
this.constraints.queryParams[paramName] = constraint;
|
this.constraints.queryParams[paramName] = constraint;
|
||||||
cfg = constraint.describe();
|
cfg = constraint.describe();
|
||||||
if (Array.isArray(cfg)) {
|
if (Array.isArray(cfg)) {
|
||||||
|
@ -338,8 +380,18 @@ extend(RequestContext.prototype, {
|
||||||
} else {
|
} else {
|
||||||
type = cfg.type;
|
type = cfg.type;
|
||||||
}
|
}
|
||||||
required = Boolean(cfg.flags && cfg.flags.presense === 'required');
|
required = Boolean(cfg.flags && cfg.flags.presence === 'required');
|
||||||
description = cfg.description;
|
description = cfg.description;
|
||||||
|
if (cfg.meta) {
|
||||||
|
if (!Array.isArray(cfg.meta)) {
|
||||||
|
cfg.meta = [cfg.meta];
|
||||||
|
}
|
||||||
|
_.each(cfg.meta, function (meta) {
|
||||||
|
if (meta && typeof meta.allowMultiple === 'boolean') {
|
||||||
|
allowMultiple = meta.allowMultiple;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
if (
|
if (
|
||||||
type === 'number' &&
|
type === 'number' &&
|
||||||
_.isArray(cfg.rules) &&
|
_.isArray(cfg.rules) &&
|
||||||
|
@ -356,7 +408,7 @@ extend(RequestContext.prototype, {
|
||||||
description,
|
description,
|
||||||
type,
|
type,
|
||||||
required,
|
required,
|
||||||
attributes.allowMultiple
|
Boolean(allowMultiple)
|
||||||
);
|
);
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
|
@ -514,6 +514,21 @@ function DocumentationAndConstraintsSpec () {
|
||||||
assertEqual(context.constraints.urlParams, {id: constraint});
|
assertEqual(context.constraints.urlParams, {id: constraint});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
testDefinePathParamShorthand: function () {
|
||||||
|
var constraint = joi.number().integer().description("Id of the Foxx"),
|
||||||
|
context = app.get('/foxx/:id', function () {
|
||||||
|
//nothing
|
||||||
|
}).pathParam("id", constraint);
|
||||||
|
|
||||||
|
assertEqual(routes.length, 1);
|
||||||
|
assertEqual(routes[0].url.constraint.id, "/[0-9]+/");
|
||||||
|
assertEqual(routes[0].docs.parameters[0].paramType, "path");
|
||||||
|
assertEqual(routes[0].docs.parameters[0].name, "id");
|
||||||
|
assertEqual(routes[0].docs.parameters[0].description, "Id of the Foxx");
|
||||||
|
assertEqual(routes[0].docs.parameters[0].dataType, "integer");
|
||||||
|
assertEqual(context.constraints.urlParams, {id: constraint});
|
||||||
|
},
|
||||||
|
|
||||||
testDefinePathCaseParam: function () {
|
testDefinePathCaseParam: function () {
|
||||||
var constraint = joi.number().integer().description("Id of the Foxx"),
|
var constraint = joi.number().integer().description("Id of the Foxx"),
|
||||||
context = app.get('/foxx/:idParam', function () {
|
context = app.get('/foxx/:idParam', function () {
|
||||||
|
@ -592,10 +607,56 @@ function DocumentationAndConstraintsSpec () {
|
||||||
context = app.get('/foxx', function () {
|
context = app.get('/foxx', function () {
|
||||||
//nothing
|
//nothing
|
||||||
}).queryParam("a", {
|
}).queryParam("a", {
|
||||||
type: constraint,
|
type: constraint
|
||||||
allowMultiple: true
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
assertEqual(routes.length, 1);
|
||||||
|
assertEqual(routes[0].docs.parameters[0].paramType, "query");
|
||||||
|
assertEqual(routes[0].docs.parameters[0].name, "a");
|
||||||
|
assertEqual(routes[0].docs.parameters[0].description, "The value of an a");
|
||||||
|
assertEqual(routes[0].docs.parameters[0].dataType, "integer");
|
||||||
|
assertEqual(routes[0].docs.parameters[0].required, false);
|
||||||
|
assertEqual(routes[0].docs.parameters[0].allowMultiple, false);
|
||||||
|
assertEqual(context.constraints.queryParams, {a: constraint});
|
||||||
|
},
|
||||||
|
|
||||||
|
testDefineQueryParamWithOverrides: function () {
|
||||||
|
var constraint = joi.number().integer(),
|
||||||
|
context = app.get('/foxx', function () {
|
||||||
|
//nothing
|
||||||
|
}).queryParam("a", {
|
||||||
|
type: constraint,
|
||||||
|
description: "The value of an a",
|
||||||
|
allowMultiple: true,
|
||||||
|
required: true
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEqual(routes.length, 1);
|
||||||
|
assertEqual(routes[0].docs.parameters[0].paramType, "query");
|
||||||
|
assertEqual(routes[0].docs.parameters[0].name, "a");
|
||||||
|
assertEqual(routes[0].docs.parameters[0].description, "The value of an a");
|
||||||
|
assertEqual(routes[0].docs.parameters[0].dataType, "integer");
|
||||||
|
print(0)
|
||||||
|
assertEqual(routes[0].docs.parameters[0].required, true);
|
||||||
|
print(1)
|
||||||
|
assertEqual(routes[0].docs.parameters[0].allowMultiple, true);
|
||||||
|
print(2)
|
||||||
|
assertEqual(context.constraints.queryParams, {
|
||||||
|
a: constraint
|
||||||
|
.description("The value of an a")
|
||||||
|
.meta({allowMultiple: true})
|
||||||
|
.required()
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
testDefineQueryParamShorthand: function () {
|
||||||
|
var constraint = joi.number().integer()
|
||||||
|
.description("The value of an a")
|
||||||
|
.meta({allowMultiple: true}),
|
||||||
|
context = app.get('/foxx', function () {
|
||||||
|
//nothing
|
||||||
|
}).queryParam("a", constraint);
|
||||||
|
|
||||||
assertEqual(routes.length, 1);
|
assertEqual(routes.length, 1);
|
||||||
assertEqual(routes[0].docs.parameters[0].paramType, "query");
|
assertEqual(routes[0].docs.parameters[0].paramType, "query");
|
||||||
assertEqual(routes[0].docs.parameters[0].name, "a");
|
assertEqual(routes[0].docs.parameters[0].name, "a");
|
||||||
|
|
Loading…
Reference in New Issue