Skip to content

Commit 877b5d9

Browse files
committed
Adding more convention validation
1 parent feb5190 commit 877b5d9

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed

index.js

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ namingConventions = {
1717
//"Train-Case": "[a-z0-9\-]*
1818
}
1919

20+
function ValidationError(type, field) {
21+
this.type = type;
22+
this.field = field;
23+
}
24+
2025
//Promise.onPossiblyUnhandledRejection(function(error){
2126
// throw error;
2227
//});
2328

24-
function getSpec(file) {
29+
function getSpecPromise(file) {
2530
return parser.parseAsync(file)
2631
.spread(function(api, metadata) {
2732
return api;
@@ -31,7 +36,7 @@ function getSpec(file) {
3136
});
3237
}
3338

34-
function getCheckStyle(file) {
39+
function getCheckStylePromise(file) {
3540
return fs.readFileAsync(file)
3641
.then(yaml.safeLoad)
3742
.catch(function(e) {
@@ -40,22 +45,6 @@ function getCheckStyle(file) {
4045
});
4146
}
4247

43-
function ValidationErrors() {
44-
this.errors = []
45-
this.add = add;
46-
47-
function add(error) {
48-
if (error != null) {
49-
this.errors.push(error);
50-
}
51-
}
52-
}
53-
54-
function ValidationError(type, field) {
55-
this.type = type;
56-
this.field = field;
57-
}
58-
5948
function validatePath(path, pathNamingConvention) {
6049
matchPath = path.replace(pathNamingConvention, "") === path
6150
if (!matchPath) {
@@ -75,28 +64,28 @@ function validateOperation(opId, opNamingConvention) {
7564
}
7665

7766
function validateConventions(spec, pathNamingConvention, opNamingConvention) {
78-
validationErrors = new ValidationErrors();
67+
var errors = new Array();
7968

8069
result = mask(spec, "paths/*/*/operationId");
8170
paths = result.paths
8271
_.each(Object.keys(paths), function(path) {
8372

8473
pathError = validatePath(path, pathNamingConvention);
85-
validationErrors.add(pathError);
74+
if (pathError != null) errors.push(pathError);
8675

8776
pathValue = result.paths[path]
8877
_.each(Object.keys(pathValue), function(verb) {
8978

9079
opId = pathValue[verb].operationId;
9180
opError = validateOperation(opId, opNamingConvention);
92-
validationErrors.add(opError);
81+
if (opError != null) errors.push(opError);
9382

9483
});
9584
});
96-
return validationErrors;
85+
return errors;
9786
}
9887

99-
function getSchema(spec, checkStyle) {
88+
function getSchema(checkStyle) {
10089
schema = Joi.object().keys({
10190
swagger: Joi.any().valid(checkStyle.swagger),
10291
host: joiRegex(checkStyle.host),
@@ -111,25 +100,32 @@ function getSchema(spec, checkStyle) {
111100
return schema;
112101
}
113102

103+
function validate(checkStyleFile, specFile) {
104+
specPromise = getSpecPromise(specFile);
105+
stylePromise = getCheckStylePromise(checkStyleFile);
106+
107+
Promise.join(specPromise, stylePromise, function(spec, checkStyle) {
108+
pathConvention = namingConventions[checkStyle.paths.namingConvention];
109+
opIdConvention = namingConventions[checkStyle.paths.operationId.namingConvention];
110+
errors = validateConventions(spec, pathConvention, opIdConvention);
111+
console.log(errors);
112+
113+
return [spec,
114+
getSchema(spec, checkStyle),
115+
{allowUnknown: true}];
116+
}).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+
});
125+
});
126+
}
114127

115-
checkStyleFile = './examples/uber/swagger-checkstyle.yaml';
116-
specFile = './examples/uber/swagger.yaml';
117-
118-
specPromise = getSpec(specFile);
119-
stylePromise = getCheckStyle(checkStyleFile);
120-
121-
Promise.join(specPromise, stylePromise, function(spec, checkStyle) {
122-
pathConvention = namingConventions[checkStyle.paths.namingConvention];
123-
opIdConvention = namingConventions[checkStyle.paths.operationId.namingConvention];
124-
errors = validateConventions(spec, pathConvention, opIdConvention);
125-
console.log(errors);
128+
checkStyle = './examples/uber/swagger-checkstyle.yaml';
129+
spec = './examples/uber/swagger.yaml';
126130

127-
return [spec,
128-
getSchema(spec, checkStyle),
129-
{allowUnknown: true}];
130-
}).spread(function(spec, schema, options) {
131-
Joi.validateAsync(spec, schema, options)
132-
.catch(function(err) {
133-
console.log(err.details);
134-
});
135-
});
131+
validate(checkStyle, spec);

0 commit comments

Comments
 (0)