Skip to content

Commit

Permalink
Draft: feat(codeclimate): config-file respecting CodeClimate parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Blümel <[email protected]>
  • Loading branch information
Blaimi committed May 25, 2023
1 parent 0333639 commit 6d1146e
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 19 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,18 @@ The validator supports OpenAPI documents in either JSON or YAML format, using th
.yaml
.yml
```
Assuming your command shell supports the use of wildcards, you can use wildcards when specifying the names of files to be validated.
If the string ends with '/', it will be searched recursively for supported files.
For example, to run the validator on all `.yaml` files contained in the `/my/apis` directory, you could use
this command:
```bash
lint-openapi /my/apis/*.yaml
lint-openapi /my/apis/
```

Note that the `-i`/`--ignore` option can be particularly useful when using wildcards because it allows you to skip the
Note that the `-i`/`--ignore` option can be particularly useful when using wildcards or directories because it allows you to skip the
validation of specific files which might otherwise be included in a validation run.
For example, to validate all `.yaml` files in the `/my/apis` directory, except for `/my/apis/broken-api.yaml` use the command:
```bash
lint-openapi /my/apis/*.yaml -i /my/apis/broken-api.yaml
lint-openapi /my/apis/ -i /my/apis/broken-api.yaml
```

### Configuration
Expand Down Expand Up @@ -271,7 +271,8 @@ module.exports = {
<tr>
<td>The <code>files</code> configuration property corresponds to positional command-line arguments (i.e. <code>[file...]</code>).
You can set this property to the names of the OpenAPI documents to be validated. If any filenames are also entered as positional arguments
on the command-line, they will override any values specified in this configuration property.</td>
on the command-line, they will override any values specified in this configuration property.
`input_path` is an alternative key for `files`. If both are set, `input_paths` will be used.</td>
<td><code>[]</code>(empty list)</td>
</tr>
</table>
Expand Down
5 changes: 3 additions & 2 deletions packages/validator/src/cli-validator/run-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ async function runValidator(cliArgs, parseOptions = {}) {
// Run the validator on the files specified via command-line or config file.
//

// TODO: implement recursive filesearch when arg ends with '/'
// must be done before filtering

// Ignore files listed in the config object's "ignoreFiles" field
// by comparing absolute paths.
// "filteredArgs" will be "args" minus any ignored files.
Expand All @@ -98,8 +101,6 @@ async function runValidator(cliArgs, parseOptions = {}) {

// At this point, "args" is an array of file names passed in by the user,
// but with the ignored files removed.
// Nothing in "args" will be a glob type, as glob types are automatically
// converted to arrays of matching file names by the shell.
const supportedFileTypes = ['json', 'yml', 'yaml'];
const filesWithValidExtensions = [];
let unsupportedExtensionsFound = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const defaultConfig = {
files: [
// 'my-api.yaml'
],
input_paths: [
// alternative to files for CodeClimate compatibility
],
limits: {
warnings: -1,
},
Expand Down Expand Up @@ -169,9 +172,9 @@ async function processArgs(args, cliParseOptions) {
// so overlay CLI options onto our config object.

// Filenames specified on the command-line will be in the "args" field.
const cliFiles = command.args || [];
if (cliFiles.length) {
configObj.files = cliFiles;
const prioFiles = command.args || configObj.input_paths || [];
if (prioFiles.length) {
configObj.files = prioFiles;
}

// Process each loglevel entry supplied on the command line.
Expand Down
14 changes: 11 additions & 3 deletions packages/validator/src/schemas/config-file.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ title: IBM OpenAPI Validator Configuration File Schema
description: >
The structure of the configuration file supported by the IBM OpenAPI Validator
type: object
additionalProperties: false
# allow additionalProperties on root-level to be compatible with CodeClimate config.json
additionalProperties: true
properties:
colorizeOutput:
description: A flag that indicates whether the validator should colorize its output
Expand All @@ -16,9 +17,16 @@ properties:
files:
description: >
The names of the files to be validated.
Each element of the array is a glob-like string that will be evaluated relative
Each element of the array is a string that will be evaluated relative
to the current directory at the time the validator is being run.
Examples: 'a.yaml', '../b.json', '../../c/foo.yml'
If the string ends with '/', it will be searched recursively for supported files.
Each file must end with the extension '.json', '.yaml' or '.yml' and contain a key 'openapi' in it's root level.
Examples: 'a.yaml', '../b.json', '../../c/foo/'
type: array
items:
type: string
include_paths:
description: replaces 'files' when used
type: array
items:
type: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"errors": 0,
"warnings": "text",
"population": 10
"population": 10,
"errorsOnly": "yes"
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('run-validator tests', function () {

expect(allOutput).toMatch(/Invalid configuration file/);
expect(allOutput).toMatch(
/schema validation error: must NOT have additional properties/
/schema validation error: '\/errorsOnly': must be boolean/
);
expect(allOutput).toMatch(/validator will use a default config/);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ describe('Schema validator tests', function () {

it('invalid config object should return errors', function () {
const configObj = {
xlogLevels: {
root: 'debug',
},
limits: {
xwarnings: 10,
},
Expand All @@ -88,7 +85,7 @@ describe('Schema validator tests', function () {
const results = validate(configObj, configFileSchema);
expect(results).toHaveLength(1);
expect(results[0]).toBe(
`schema validation error: must NOT have additional properties`
`schema validation error: '/limits': must have required property 'warnings'`
);
});
});

0 comments on commit 6d1146e

Please sign in to comment.