All files / src/plugins/validation/swagger2/semantic-validators operations.js

85.71% Statements 18/21
62.5% Branches 5/8
100% Functions 3/3
85.71% Lines 18/21

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      28x 28x 28x 28x 28x 28x   28x 34x   34x 81x                 81x 112x         112x 112x 112x               112x 112x                   34x    
// Assertation 1: Operations cannot have both a 'body' parameter and a 'formData' parameter.
// Assertation 2: Operations must have only one body parameter.
 
const pick = require('lodash/pick');
const map = require('lodash/map');
const each = require('lodash/each');
const findIndex = require('lodash/findIndex');
const findLastIndex = require('lodash/findLastIndex');
const MessageCarrier = require('../../../utils/messageCarrier');
 
module.exports.validate = function({ resolvedSpec }) {
  const messages = new MessageCarrier();
 
  map(resolvedSpec.paths, (path, pathKey) => {
    const pathOps = pick(path, [
      'get',
      'head',
      'post',
      'put',
      'patch',
      'delete',
      'options'
    ]);
    each(pathOps, (op, opKey) => {
      Iif (!op) {
        return;
      }
 
      // Assertation 1
      const bodyParamIndex = findIndex(op.parameters, ['in', 'body']);
      const formDataParamIndex = findIndex(op.parameters, ['in', 'formData']);
      Iif (bodyParamIndex > -1 && formDataParamIndex > -1) {
        messages.addMessage(
          `paths.${pathKey}.${opKey}.parameters`,
          'Operations cannot have both a "body" parameter and "formData" parameter',
          'error'
        );
      }
      // Assertation 2
      const lastBodyParamIndex = findLastIndex(op.parameters, ['in', 'body']);
      Iif (bodyParamIndex !== lastBodyParamIndex) {
        messages.addMessage(
          `paths.${pathKey}.${opKey}.parameters`,
          'Operations must have no more than one body parameter',
          'error'
        );
      }
    });
  });
 
  return messages;
};