feat: add camelCase checking rule

This commit is contained in:
knidarkness
2019-10-24 00:38:17 +03:00
parent beece979d1
commit a67f7efdf2
4 changed files with 72 additions and 0 deletions

View File

@@ -94,6 +94,7 @@ rules:
operation-operationId-unique: on
path-declarations-must-exist: on
camel-case-names: off
api-servers: off
license-url: off
no-extra-fields: off
@@ -102,6 +103,9 @@ rules:
operation-tags: off
provide-contact: off
servers-no-trailing-slash: off
bundler: off
debug-info: off
```
Here is an example of a modified use `.openapi-cli.yaml` file:

View File

@@ -5,6 +5,9 @@ All supported rules are listed below. To change your settings for any given rule
### api-servers
OpenAPI servers must be present and be a non-empty array.
### camel-case-names
Schemas and parameters names should be in camelCase. This rule does a lot of string comparison and matching operations, so it may increase time of validation significantly.
### path-param-exists
Each path parameter in the `parameters` section must be present in the path string.

View File

@@ -11,6 +11,7 @@ rules:
operation-operationId-unique: on
path-declarations-must-exist: on
camel-case-names: off
api-servers: off
license-url: off
no-extra-fields: off

View File

@@ -0,0 +1,64 @@
/* eslint-disable class-methods-use-this */
import AbstractVisitor from '../../utils/AbstractVisitor';
import createError from '../../../error';
// this method uses regexps, so is very sloooow,
// for now we are just checking if there are any '_'
// not in the beginning of the name
const validateNodeRegexp = (node, ctx, name) => {
const errors = [];
const names = Object.keys(node);
for (let i = 0; i < names.length; i++) {
const matches = names[i].match(this.pattern);
if (!matches || matches.filter((e) => typeof e === 'string').indexOf(names[i]) === -1) {
ctx.path.push(names[i]);
const error = createError(`${name} names should be in camelCase.`, node, ctx, { severity: this.config.level, target: 'key', fromRule: this.rule });
errors.push(error);
ctx.path.pop();
}
}
return errors;
};
const validateNode = (node, ctx, name, rule) => {
const errors = [];
const names = Object.keys(node);
for (let i = 0; i < names.length; i++) {
if (names[i].indexOf('_') > 0) {
ctx.path.push(names[i]);
const error = createError(`${name}s names should be in camelCase.`, node, ctx, { severity: rule.config.level, target: 'key', fromRule: rule.rule });
errors.push(error);
ctx.path.pop();
}
}
return errors;
};
class CamelCaseNames extends AbstractVisitor {
static get ruleName() {
return 'camel-case-names';
}
constructor(config) {
super(config);
this.pattern = new RegExp('^_?[a-zA-Z](([^_]*[a-zA-Z]*)*)');
}
get rule() {
return 'camel-case-names';
}
OpenAPISchemaMap() {
return {
onEnter: (node, _, ctx) => validateNode(node, ctx, 'Schema', this),
};
}
OpenAPIParameterMap() {
return {
onEnter: (node, _, ctx) => validateNode(node, ctx, 'Parameter', this),
};
}
}
module.exports = CamelCaseNames;