Skip to content

Commit 92f4798

Browse files
committed
Completed Joi conversion, turned into node module, added mocha tests
1 parent 700e868 commit 92f4798

File tree

10 files changed

+106
-65
lines changed

10 files changed

+106
-65
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ Some areas of concern in governing standards:
88
* Restrict the use of verb, status
99
* Enforce the use of schema and formats in parameters and fields
1010

11-
`swagger-api-checkstyle` is a specification meant to designate these governance criteria. This should provide a specification for implementing the validation of API spec files against a checkstyle definition.
11+
`swagger-api-checkstyle` is a specification meant to designate these governance criteria. This should provide a specification for implementing the validation of API spec files against a checkstyle definition.
12+
13+
Node library in development
14+

examples/uber/swagger-errors.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ basePath: /v1
1515
produces:
1616
- application/json
1717
paths:
18-
/Pr_oducts:
18+
/Prod_ucts:
1919
get:
20-
operationId: getProducts
20+
operationId: G_etProducts
2121
summary: Product Types
2222
description: The Products endpoint returns information about the Uber products offered at a given location. The response includes the display name and other details about each product, and lists the products in the proper display order.
2323
parameters:
24-
- name: latitude
24+
- name: Latitude
2525
in: query
2626
description: Latitude component of location.
2727
required: true

lib/conventions.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
var exports = module.exports = {};
4+
5+
exports.path = {
6+
"spine-case": /^[\/a-z0-9\-]*$/g,
7+
"CAP-SPINE-CASE": /^[\/A-Z0-9\-]*$/g,
8+
"snake_case": /^[\/a-z0-9\_]*$/g,
9+
"camelCase": /^[\/a-z]+[\/A-Z0-9][\/a-z0-9]+[\/A-Za-z0-9]*$/g,
10+
"ProperCase": /^(\/[A-Z][a-z]*)+$/g
11+
};
12+
13+
exports.naming = {
14+
"spine-case": /^[a-z0-9\-]*$/g,
15+
"CAP-SPINE-CASE": /^[A-Z0-9\-]*$/g,
16+
"snake_case": /^[a-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
19+
//"Train-Case": "[a-z0-9\-]*
20+
};

lib/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
3+
module.exports.conventions = require('./conventions');
4+
module.exports.validate = require('./validate');

joi_regex.js renamed to lib/joi_regex.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var _ = require("lodash");
22
var Joi = require("joi");
3+
34
var exports = module.exports = {};
45

56
module.exports = function(any) {
Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,17 @@
1+
'use strict';
2+
3+
module.exports = validate;
4+
5+
var joiRegex = require('./joi_regex');
6+
var conventions = require('./conventions');
7+
8+
var Joi = require("joi");
19
var Promise = require("bluebird");
210
var fs = Promise.promisifyAll(require("fs"));
311
var parser = Promise.promisifyAll(require("swagger-parser"));
412
var yaml = Promise.promisifyAll(require("js-yaml"));
513
var _ = Promise.promisifyAll(require("lodash"));
6-
var mask = require("json-mask");
7-
8-
var joiRegex = require("./joi_regex.js");
9-
var Joi = require("joi");
10-
11-
'use strict';
1214

13-
pathNamingConventions = {
14-
"spine-case": /^[\/a-z0-9\-]*$/g,
15-
"CAP-SPINE-CASE": /^[\/A-Z0-9\-]*$/g,
16-
"snake_case": /^[\/a-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
19-
}
20-
21-
namingConventions = {
22-
"spine-case": /^[a-z0-9\-]*$/g,
23-
"CAP-SPINE-CASE": /^[A-Z0-9\-]*$/g,
24-
"snake_case": /^[a-z0-9\_]*$/g,
25-
"camelCase": /^[a-z]+[\/A-Z0-9][\/a-z0-9]+[\/A-Za-z0-9]*$/g,
26-
"ProperCase": /^([A-Z][a-z]*)+$/g
27-
//"Train-Case": "[a-z0-9\-]*
28-
}
29-
30-
function ValidationError(type, field) {
31-
this.type = type;
32-
this.field = field;
33-
}
34-
35-
//Promise.onPossiblyUnhandledRejection(function(error){
36-
// throw error;
37-
//});
3815

3916
function getSpecPromise(file) {
4017
return parser.parseAsync(file)
@@ -56,16 +33,16 @@ function getCheckStylePromise(file) {
5633
}
5734

5835
function getSchema(checkStyle) {
59-
pathConvention = pathNamingConventions[checkStyle.paths.namingConvention];
60-
opIdConvention = namingConventions[checkStyle.paths.operationId.namingConvention];
61-
tagConvention= namingConventions[checkStyle.paths.tags.namingConvention];
62-
queryParamConvention = namingConventions[checkStyle.paths.parameters.query.namingConvention];
63-
headerParamConvention = namingConventions[checkStyle.paths.parameters.header.namingConvention];
64-
pathParamConvention = namingConventions[checkStyle.paths.parameters.path.namingConvention];
65-
statuses = new RegExp("^".concat(Object.keys(checkStyle.paths.status).join("$|^")).concat("$"));
66-
verbs = new RegExp("^".concat(checkStyle.paths.verbs.join("$|^")).concat("$"));
36+
var pathConvention = conventions.path[checkStyle.paths.namingConvention];
37+
var opIdConvention = conventions.naming[checkStyle.paths.operationId.namingConvention];
38+
var tagConvention= conventions.naming[checkStyle.paths.tags.namingConvention];
39+
var queryParamConvention = conventions.naming[checkStyle.paths.parameters.query.namingConvention];
40+
var headerParamConvention = conventions.naming[checkStyle.paths.parameters.header.namingConvention];
41+
var pathParamConvention = conventions.naming[checkStyle.paths.parameters.path.namingConvention];
42+
var statuses = new RegExp("^".concat(Object.keys(checkStyle.paths.status).join("$|^")).concat("$"));
43+
var verbs = new RegExp("^".concat(checkStyle.paths.verbs.join("$|^")).concat("$"));
6744

68-
parameterKeys = {
45+
var parameterKeys = {
6946
'in': Joi.string().valid('query', 'header'),
7047
format: Joi.string(),
7148
type: Joi.string(),
@@ -76,7 +53,7 @@ function getSchema(checkStyle) {
7653
.when('in', {is: 'header', then: Joi.string().regex(headerParamConvention)})
7754
}
7855

79-
schema = Joi.object().keys({
56+
var schema = Joi.object().keys({
8057
swagger: Joi.any().valid(checkStyle.swagger),
8158
info: Joi.object().keys({
8259
title: Joi.string(),
@@ -110,19 +87,15 @@ function getSchema(checkStyle) {
11087
return schema;
11188
}
11289

113-
function validate(checkStyleFile, specFile) {
114-
Promise.join(getSpecPromise(specFile), getCheckStylePromise(checkStyleFile),
115-
function(spec, checkStyle) {
116-
return [spec, getSchema(checkStyle)];
117-
}).spread(function(spec, schema) {
118-
var result = Joi.validate(spec, schema);
119-
if (result.error) {
120-
console.log(result.error)
121-
}
90+
function validate(checkStyleFile, specFile, callback) {
91+
Promise.join(getSpecPromise(specFile), getCheckStylePromise(checkStyleFile), callback,
92+
function(spec, checkStyle, callback) {
93+
return [spec, getSchema(checkStyle), callback];
94+
}).spread(function(spec, schema, callback) {
95+
return Joi.validate(spec, schema);
96+
}).then(function(result) {
97+
callback(result, null);
98+
}).catch(function(e) {
99+
callback(null, e);
122100
});
123101
}
124-
125-
checkStyle = './examples/uber/swagger-checkstyle.yaml';
126-
spec = './examples/uber/swagger.yaml';
127-
128-
validate(checkStyle, spec);

package.json

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"license": "ISC",
1111
"dependencies": {
1212
"bluebird": "^2.9.30",
13+
"commander": "^2.8.1",
1314
"joi": "^6.5.0",
1415
"js-yaml": "^3.3.1",
1516
"json-mask": "^0.3.4",
@@ -19,5 +20,22 @@
1920
"devDependencies": {
2021
"mocha": "^2.2.5",
2122
"should": "^7.0.1"
22-
}
23+
},
24+
"directories": {
25+
"example": "examples",
26+
"test": "test"
27+
},
28+
"repository": {
29+
"type": "git",
30+
"url": "git+https://github.com/jasonh-n-austin/swagger-api-checkstyle.git"
31+
},
32+
"keywords": [
33+
"swagger",
34+
"api",
35+
"checkstyle"
36+
],
37+
"bugs": {
38+
"url": "https://github.com/jasonh-n-austin/swagger-api-checkstyle/issues"
39+
},
40+
"homepage": "https://github.com/jasonh-n-austin/swagger-api-checkstyle#readme"
2341
}

test/convention_validation.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1-
var should = require("should");
1+
var should = require("should");
2+
var assert = require("assert");
3+
var swaggerCheckStyle = require("../lib")
24

3-
describe('validateConventions', function() {
4-
it('should validate paths', function() {
5-
//validateConventions(spec, pathNamingConvention, opNamingConvention)
5+
describe('validate', function(done) {
6+
it('should validate swagger', function(done) {
7+
checkStyle = './examples/uber/swagger-checkstyle.yaml';
8+
spec = './examples/uber/swagger.yaml';
9+
result = null;
610

11+
swaggerCheckStyle.validate(checkStyle, spec, function(result, err) {
12+
if (err) throw err;
13+
should.not.exist(result.error)
14+
done();
15+
});
16+
});
17+
it('should not validate bad swagger', function(done) {
18+
checkStyle = './examples/uber/swagger-checkstyle.yaml';
19+
spec = './examples/uber/swagger-errors.yaml';
20+
21+
swaggerCheckStyle.validate(checkStyle, spec, function(result, err) {
22+
if (err) throw err;
23+
result.error.name.should.eql("ValidationError");
24+
done();
25+
});
726
});
827
});

test/joi_regex_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var joiRegex = require("../joi_regex.js");
1+
var joiRegex = require("../lib/joi_regex");
22
var Joi = require("joi");
33
var _ = require("lodash");
44
var should = require('should');

test/mocha.opts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--require should
2+
--reporter spec
3+
--ui bdd

0 commit comments

Comments
 (0)