IBM/openapi-validator

Incorrect validation results for the ibm-parameter-casing-convention rule with camelCase

Closed this issue · 3 comments

After defining a custom rule to check that the parameters conform to camelCase, the rule seems to display non-existing results for all parameters.

Rule Configuration (via .spectral.js)

const ibmCloudValidationRules = require('@ibm-cloud/openapi-ruleset');
const { parameterCasingConvention } = require('@ibm-cloud/openapi-ruleset/src/functions');
const { schemas } = require('@ibm-cloud/openapi-ruleset-utilities/src/collections');

module.exports = {
  extends: ibmCloudValidationRules,
  rules: {
   'ibm-parameter-casing-convention': {
      description: 'Query parameter names must be camel case',
      message: '{{error}}',
      resolved: true,
      given: schemas,
      severity: 'warn',
      then: {
        function: parameterCasingConvention,
        functionOptions: {
          type: 'camel'
        }
      }
    }
  }
};

Example of results:

 155  (17%) : Parameters must have a name
 155  (17%) : Parameters must have a valid 'in' value


  Message :   Parameters must have a name
  Rule    :   ibm-parameter-casing-convention
  Path    :   paths./........./.patch.parameters.0.schema

  Message :   Parameters must have a valid 'in' value
  Rule    :   ibm-parameter-casing-convention

The parameters have both a name and a valid 'in' value, but according to the validator, ALL of the parameters will raise a problem...

It looks like your custom rule definition is specifying "schemas" for the set of elements to operate on, but the original rule definition specifies "parameters".
Just change the "given" field like this:

given: parameters,

and give that a try.

Edit 1: you'll also need to change the import line at the top.
This line:

const { schemas } = require('@ibm-cloud/openapi-ruleset-utilities/src/collections');

should be changed to:

const { parameters } = require('@ibm-cloud/openapi-ruleset-utilities/src/collections');

Edit 2:
After a closer look, I also realized your definition of "functionOptions" is incorrect for this rule. The configuration specified in the original rule definition is:

    functionOptions: {
      // Allow snake case for query parameter names,
      // but also allow '.' within the name.
      query: {
        type: 'snake',
        separator: {
          char: '.',
        },
      },

      // Allow snake case for path parameter names.
      path: {
        type: 'snake',
      },

      // Allow header parameter names to be in canonical header name form (e.g. X-My-Header).
      header: {
        type: 'pascal',
        separator: {
          char: '-',
        },
      },
    },

So, you'll need to specify an object like this within your custom rule definition. If you want to enforce camel-case only for query params along with snake-case for path params and pascal case for header params, the configuration object would look like this:

    functionOptions: {
      // Allow camel case for query parameter names.
      query: {
        type: 'camel',
      },

      // Allow snake case for path parameter names.
      path: {
        type: 'snake',
      },

      // Allow header parameter names to be in canonical header name form (e.g. X-My-Header).
      header: {
        type: 'pascal',
        separator: {
          char: '-',
        },
      },
    },

Closing this out as the solution has been provided. Please re-open if you encounter other issues.