All files / src/plugins/validation/2and3/semantic-validators operations-shared.js

98.53% Statements 67/68
93.1% Branches 54/58
100% Functions 7/7
98.44% Lines 63/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192                                29x 29x 29x 29x 29x   29x 109x   109x   109x 109x 109x 109x   109x 204x     204x                     204x 256x 1x         255x 1x               255x 255x 255x 510x 277x   97x     97x 10x                   233x     233x 18x                       255x     255x 68x               187x 187x 187x 187x 187x 31x                 255x 255x 132x 132x 1x             131x           255x 255x 24x                   255x 255x 255x 154x 154x 183x 183x 170x 21x     13x 4x                           109x 51x               109x    
// Assertations
 
// Operations must have unique (name + in combination) parameters.
 
// Operations must have a non-empty `operationId`
 
// `operationId` should adhere to a given case convention
 
// Operations must have a non-empty `summary` field.
 
// Arrays MUST NOT be returned as the top-level structure in a response body.
// ref: https://pages.github.ibm.com/CloudEngineering/api_handbook/fundamentals/format.html#object-encapsulation
 
// All required parameters of an operation are listed before any optional parameters.
// http://watson-developer-cloud.github.io/api-guidelines/swagger-coding-style#parameter-order
 
const pick = require('lodash/pick');
const map = require('lodash/map');
const each = require('lodash/each');
const { checkCase, hasRefProperty } = require('../../../utils');
const MessageCarrier = require('../../../utils/messageCarrier');
 
module.exports.validate = function({ jsSpec, resolvedSpec, isOAS3 }, config) {
  const messages = new MessageCarrier();
 
  config = config.operations;
 
  const globalTags = resolvedSpec.tags || [];
  const hasGlobalTags = !!globalTags.length;
  const resolvedTags = globalTags.map(({ name }) => name);
  const unusedTags = new Set(resolvedTags);
 
  map(resolvedSpec.paths, (path, pathKey) => {
    Iif (pathKey.slice(0, 2) === 'x-') {
      return;
    }
    const pathOps = pick(path, [
      'get',
      'head',
      'post',
      'put',
      'patch',
      'delete',
      'options',
      'trace'
    ]);
 
    each(pathOps, (op, opKey) => {
      if (!op || op['x-sdk-exclude'] === true) {
        return;
      }
 
      // check for operations that have a $ref property
      // these are illegal in the spec
      if (hasRefProperty(jsSpec, ['paths', pathKey, opKey])) {
        messages.addMessage(
          `paths.${pathKey}.${opKey}.$ref`,
          '$ref found in illegal location',
          'error'
        );
      }
 
      // Arrays MUST NOT be returned as the top-level structure in a response body.
      const checkStatusArrRes = config.no_array_responses;
      Eif (checkStatusArrRes !== 'off') {
        each(op.responses, (response, name) => {
          if (isOAS3) {
            each(response.content, (content, contentType) => {
              const isArray =
                content.schema &&
                (content.schema.type === 'array' || content.schema.items);
 
              if (isArray) {
                messages.addMessage(
                  `paths.${pathKey}.${opKey}.responses.${name}.content.${contentType}.schema`,
                  'Arrays MUST NOT be returned as the top-level structure in a response body.',
                  checkStatusArrRes,
                  'no_array_responses'
                );
              }
            });
          } else {
            const isArray =
              response.schema &&
              (response.schema.type === 'array' || response.schema.items);
 
            if (isArray) {
              messages.addMessage(
                `paths.${pathKey}.${opKey}.responses.${name}.schema`,
                'Arrays MUST NOT be returned as the top-level structure in a response body.',
                checkStatusArrRes,
                'no_array_responses'
              );
            }
          }
        });
      }
 
      const hasOperationId =
        op.operationId &&
        op.operationId.length > 0 &&
        !!op.operationId.toString().trim();
      if (!hasOperationId) {
        messages.addMessage(
          `paths.${pathKey}.${opKey}.operationId`,
          'Operations must have a non-empty `operationId`.',
          config.no_operation_id,
          'no_operation_id'
        );
      } else {
        // check operationId for case convention
        const checkStatus = config.operation_id_case_convention[0];
        Eif (checkStatus !== 'off') {
          const caseConvention = config.operation_id_case_convention[1];
          const isCorrectCase = checkCase(op.operationId, caseConvention);
          if (!isCorrectCase) {
            messages.addMessage(
              `paths.${pathKey}.${opKey}.operationId`,
              `operationIds must follow case convention: ${caseConvention}`,
              checkStatus,
              'operation_id_case_convention'
            );
          }
        }
      }
      const hasOperationTags = op.tags && op.tags.length > 0;
      if (hasOperationTags && hasGlobalTags) {
        for (let i = 0, len = op.tags.length; i < len; i++) {
          if (!resolvedTags.includes(op.tags[i])) {
            messages.addMessage(
              `paths.${pathKey}.${opKey}.tags`,
              'tag is not defined at the global level: ' + op.tags[i],
              config.undefined_tag,
              'undefined_tag'
            );
          } else {
            unusedTags.delete(op.tags[i]);
          }
        }
      }
 
      const hasSummary =
        op.summary && op.summary.length > 0 && !!op.summary.toString().trim();
      if (!hasSummary) {
        messages.addMessage(
          `paths.${pathKey}.${opKey}.summary`,
          'Operations must have a non-empty `summary` field.',
          config.no_summary,
          'no_summary'
        );
      }
 
      // this should be good with resolved spec, but double check
      // All required parameters of an operation are listed before any optional parameters.
      const checkStatusParamOrder = config.parameter_order;
      Eif (checkStatusParamOrder !== 'off') {
        if (op.parameters && op.parameters.length > 0) {
          let firstOptional = -1;
          for (let indx = 0; indx < op.parameters.length; indx++) {
            const param = op.parameters[indx];
            if (firstOptional < 0) {
              if (!param.required) {
                firstOptional = indx;
              }
            } else {
              if (param.required) {
                messages.addMessage(
                  `paths.${pathKey}.${opKey}.parameters[${indx}]`,
                  'Required parameters should appear before optional parameters.',
                  checkStatusParamOrder,
                  'parameter_order'
                );
              }
            }
          }
        }
      }
    });
  });
 
  unusedTags.forEach(tagName => {
    messages.addMessage(
      `tags`,
      `A tag is defined but never used: ${tagName}`,
      config.unused_tag,
      'unused_tag'
    );
  });
 
  return messages;
};