From 6d1146ee2c19c84eea21483a5f5a48d3f1c97c87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bl=C3=BCmel?= Date: Sat, 6 May 2023 07:41:31 +0200 Subject: [PATCH] Draft: feat(codeclimate): config-file respecting CodeClimate parameters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Blümel --- README.md | 11 ++++++----- .../validator/src/cli-validator/run-validator.js | 5 +++-- .../cli-validator/utils/configuration-manager.js | 9 ++++++--- packages/validator/src/schemas/config-file.yaml | 14 +++++++++++--- .../mock-files/config/invalid-values.json | 3 ++- .../test/cli-validator/tests/run-validator.test.js | 2 +- .../tests/utils/schema-validator.test.js | 5 +---- 7 files changed, 30 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 70b2aa617..7eb7fb753 100644 --- a/README.md +++ b/README.md @@ -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 @@ -271,7 +271,8 @@ module.exports = { The files configuration property corresponds to positional command-line arguments (i.e. [file...]). 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. +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. [](empty list) diff --git a/packages/validator/src/cli-validator/run-validator.js b/packages/validator/src/cli-validator/run-validator.js index 93cacfc98..f5491bfe6 100644 --- a/packages/validator/src/cli-validator/run-validator.js +++ b/packages/validator/src/cli-validator/run-validator.js @@ -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. @@ -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; diff --git a/packages/validator/src/cli-validator/utils/configuration-manager.js b/packages/validator/src/cli-validator/utils/configuration-manager.js index 6a5d58102..3a773beb1 100644 --- a/packages/validator/src/cli-validator/utils/configuration-manager.js +++ b/packages/validator/src/cli-validator/utils/configuration-manager.js @@ -29,6 +29,9 @@ const defaultConfig = { files: [ // 'my-api.yaml' ], + input_paths: [ + // alternative to files for CodeClimate compatibility + ], limits: { warnings: -1, }, @@ -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. diff --git a/packages/validator/src/schemas/config-file.yaml b/packages/validator/src/schemas/config-file.yaml index eb2e2bfb9..ea1b99c10 100644 --- a/packages/validator/src/schemas/config-file.yaml +++ b/packages/validator/src/schemas/config-file.yaml @@ -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 @@ -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 diff --git a/packages/validator/test/cli-validator/mock-files/config/invalid-values.json b/packages/validator/test/cli-validator/mock-files/config/invalid-values.json index 5b5049bf5..b00ca5f45 100644 --- a/packages/validator/test/cli-validator/mock-files/config/invalid-values.json +++ b/packages/validator/test/cli-validator/mock-files/config/invalid-values.json @@ -1,5 +1,6 @@ { "errors": 0, "warnings": "text", - "population": 10 + "population": 10, + "errorsOnly": "yes" } diff --git a/packages/validator/test/cli-validator/tests/run-validator.test.js b/packages/validator/test/cli-validator/tests/run-validator.test.js index 77ed8813b..0fcda123b 100644 --- a/packages/validator/test/cli-validator/tests/run-validator.test.js +++ b/packages/validator/test/cli-validator/tests/run-validator.test.js @@ -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/); }); diff --git a/packages/validator/test/cli-validator/tests/utils/schema-validator.test.js b/packages/validator/test/cli-validator/tests/utils/schema-validator.test.js index 7ceb82c70..3de814797 100644 --- a/packages/validator/test/cli-validator/tests/utils/schema-validator.test.js +++ b/packages/validator/test/cli-validator/tests/utils/schema-validator.test.js @@ -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, }, @@ -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'` ); }); });