|
1 | 1 | var Promise = require("bluebird");
|
2 | 2 | var fs = Promise.promisifyAll(require("fs"));
|
3 |
| -var Joi = Promise.promisifyAll(require("joi")); |
4 | 3 | var parser = Promise.promisifyAll(require("swagger-parser"));
|
5 | 4 | var yaml = Promise.promisifyAll(require("js-yaml"));
|
6 | 5 | var _ = Promise.promisifyAll(require("lodash"));
|
7 | 6 | var mask = require("json-mask");
|
8 | 7 |
|
9 | 8 | var joiRegex = require("./joi_regex.js");
|
| 9 | +var Joi = require("joi"); |
10 | 10 |
|
11 | 11 | 'use strict';
|
12 | 12 |
|
13 | 13 | namingConventions = {
|
14 | 14 | "spine-case": /[^\/a-z0-9\-]*/g,
|
| 15 | + "CAP-SPINE-CASE": /[^\/A-Z0-9\-]*/g, |
15 | 16 | "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 |
17 | 19 | //"Train-Case": "[a-z0-9\-]*
|
18 | 20 | }
|
19 | 21 |
|
@@ -86,42 +88,74 @@ function validateConventions(spec, pathNamingConvention, opNamingConvention) {
|
86 | 88 | }
|
87 | 89 |
|
88 | 90 | 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 | + |
89 | 99 | schema = Joi.object().keys({
|
90 | 100 | swagger: Joi.any().valid(checkStyle.swagger),
|
| 101 | + info: Joi.any(), |
91 | 102 | host: joiRegex(checkStyle.host),
|
92 | 103 | scheme: joiRegex(checkStyle.schemes),
|
93 | 104 | 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() |
95 | 137 |
|
96 |
| - }) |
97 |
| - //produces: joiRegex(checkStyle.produces), //TODO: Array matching |
98 | 138 | });
|
99 | 139 |
|
100 | 140 | return schema;
|
101 | 141 | }
|
102 | 142 |
|
103 | 143 | 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) { |
108 | 146 | pathConvention = namingConventions[checkStyle.paths.namingConvention];
|
109 | 147 | opIdConvention = namingConventions[checkStyle.paths.operationId.namingConvention];
|
110 | 148 | errors = validateConventions(spec, pathConvention, opIdConvention);
|
111 |
| - console.log(errors); |
| 149 | + console.log(errors) |
112 | 150 |
|
113 | 151 | return [spec,
|
114 |
| - getSchema(spec, checkStyle), |
115 |
| - {allowUnknown: true}]; |
| 152 | + getSchema(checkStyle), |
| 153 | + {}];//{allowUnknown: true}]; |
116 | 154 | }).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 | + } |
125 | 159 | });
|
126 | 160 | }
|
127 | 161 |
|
|
0 commit comments