Skip to content

Commit 3445d20

Browse files
committed
Moved more logic into Joi validation; still kind of a mess
1 parent 4b4c83d commit 3445d20

File tree

3 files changed

+72
-29
lines changed

3 files changed

+72
-29
lines changed

examples/uber/swagger-checkstyle.yaml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
swagger: "2.0"
2-
host:
2+
host:
33
- "^api\\.uber\\.com$"
44
- "localhost:8080"
55
basePath: "\/v1"
@@ -16,22 +16,27 @@ paths:
1616
1: sub-resource
1717
operationId:
1818
namingConvention: "camelCase"
19+
tags:
20+
namingConvention: "ProperCase"
21+
verbs: "get"
22+
# post, patch, put, delete
23+
#- get
1924
parameters:
20-
# body, header, path
2125
query:
2226
namingConvention: "snake_case"
2327
items:
2428
- name: limit
2529
schemaRef: "#/parameters/LimitParam"
2630
- name: offset
2731
schemaRef: "#/parameters/OffsetParam"
28-
verbs:
29-
# post, patch, put, delete
30-
- get
31-
status:
32-
200:
33-
default:
34-
schemaRef: "#/definitions/Error"
32+
header:
33+
namingConvention: "CAP-SPINE-CASE"
34+
path:
35+
namingConvention: "snake_case"
36+
status:
37+
200:
38+
default:
39+
schemaRef: "#/definitions/Error"
3540
schema:
3641
properties:
3742
- name: "^.*\\_?id"

examples/uber/swagger.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ parameters:
178178
type: integer
179179
format: int32
180180
description: Number of items to retrieve. Default is 5, maximum is 100.
181+
ApiVersionParam:
182+
name: "Api-Version"
183+
type: string
184+
description: The version of the API
181185

182186
definitions:
183187
Product:

index.js

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
var Promise = require("bluebird");
22
var fs = Promise.promisifyAll(require("fs"));
3-
var Joi = Promise.promisifyAll(require("joi"));
43
var parser = Promise.promisifyAll(require("swagger-parser"));
54
var yaml = Promise.promisifyAll(require("js-yaml"));
65
var _ = Promise.promisifyAll(require("lodash"));
76
var mask = require("json-mask");
87

98
var joiRegex = require("./joi_regex.js");
9+
var Joi = require("joi");
1010

1111
'use strict';
1212

1313
namingConventions = {
1414
"spine-case": /[^\/a-z0-9\-]*/g,
15+
"CAP-SPINE-CASE": /[^\/A-Z0-9\-]*/g,
1516
"snake_case": /[^\/a-z0-9\_]*/g,
16-
"camelCase": /[\/a-z]+[\/A-Z0-9][\/a-z0-9]+[\/A-Za-z0-9]*$/g
17+
"camelCase": /[\/a-z]+[\/A-Z0-9][\/a-z0-9]+[\/A-Za-z0-9]*$/g,
18+
"ProperCase": /^([A-Z][a-z]*)+$/g
1719
//"Train-Case": "[a-z0-9\-]*
1820
}
1921

@@ -86,42 +88,74 @@ function validateConventions(spec, pathNamingConvention, opNamingConvention) {
8688
}
8789

8890
function getSchema(checkStyle) {
91+
pathConvention = namingConventions[checkStyle.paths.namingConvention];
92+
opIdConvention = namingConventions[checkStyle.paths.operationId.namingConvention];
93+
tagConvention= namingConventions[checkStyle.paths.tags.namingConvention];
94+
queryParamConvention = namingConventions[checkStyle.paths.parameters.query.namingConvention];
95+
headerParamConvention = namingConventions[checkStyle.paths.parameters.header.namingConvention];
96+
pathParamConvention = namingConventions[checkStyle.paths.parameters.path.namingConvention];
97+
statuses = new RegExp("^".concat(Object.keys(checkStyle.paths.status).join("$|^")).concat("$"));
98+
8999
schema = Joi.object().keys({
90100
swagger: Joi.any().valid(checkStyle.swagger),
101+
info: Joi.any(),
91102
host: joiRegex(checkStyle.host),
92103
scheme: joiRegex(checkStyle.schemes),
93104
basePath: joiRegex(checkStyle.basePath),
94-
paths: Joi.object().keys({
105+
produces: Joi.array().items(joiRegex(checkStyle.produces)),
106+
consumes: Joi.array().items(joiRegex(checkStyle.consumes)),
107+
schemes: Joi.array().items(joiRegex(checkStyle.schemes)),
108+
paths: Joi.object() .pattern(pathConvention, Joi.object().keys({})
109+
// TODO: pull list of verbs into regexp
110+
.pattern(new RegExp("get"), Joi.object().keys({
111+
summary: Joi.any(),
112+
description: Joi.any(),
113+
parameters: Joi.any(),
114+
operationId: joiRegex(opIdConvention),
115+
tags: Joi.array().items(joiRegex(tagConvention)),
116+
responses: Joi.object()
117+
.pattern(statuses, Joi.object().keys({
118+
description: Joi.any(),
119+
schema: Joi.any()
120+
})
121+
)
122+
}))),
123+
parameters: Joi.object().pattern(queryParamConvention, Joi.object().keys({
124+
name: Joi.string(),
125+
'in': Joi.string().insensitive().regex(/^query$/),
126+
type: Joi.string(),
127+
format: Joi.string(),
128+
description: Joi.string()
129+
})).pattern(pathParamConvention, Joi.object().keys({
130+
name: Joi.string(),
131+
'in': Joi.string().insensitive().regex(/^path/),
132+
type: Joi.string(),
133+
format: Joi.string(),
134+
description: Joi.string()
135+
})),
136+
definitions: Joi.any()
95137

96-
})
97-
//produces: joiRegex(checkStyle.produces), //TODO: Array matching
98138
});
99139

100140
return schema;
101141
}
102142

103143
function validate(checkStyleFile, specFile) {
104-
specPromise = getSpecPromise(specFile);
105-
stylePromise = getCheckStylePromise(checkStyleFile);
106-
107-
Promise.join(specPromise, stylePromise, function(spec, checkStyle) {
144+
Promise.join(getSpecPromise(specFile), getCheckStylePromise(checkStyleFile),
145+
function(spec, checkStyle) {
108146
pathConvention = namingConventions[checkStyle.paths.namingConvention];
109147
opIdConvention = namingConventions[checkStyle.paths.operationId.namingConvention];
110148
errors = validateConventions(spec, pathConvention, opIdConvention);
111-
console.log(errors);
149+
console.log(errors)
112150

113151
return [spec,
114-
getSchema(spec, checkStyle),
115-
{allowUnknown: true}];
152+
getSchema(checkStyle),
153+
{}];//{allowUnknown: true}];
116154
}).spread(function(spec, schema, options) {
117-
Joi.validateAsync(spec, schema, options)
118-
.then(function(result) {
119-
//console.log(result);
120-
}).catch(function(err) {
121-
console.log(err);
122-
}).error(function(err) {
123-
console.log(err);
124-
});
155+
var result = Joi.validate(spec, schema, options);
156+
if (result.error) {
157+
console.log(result.error)
158+
}
125159
});
126160
}
127161

0 commit comments

Comments
 (0)