All files / src/plugins/validation/oas3/semantic-validators parameters.js

100% Statements 23/23
90% Branches 9/10
100% Functions 3/3
100% Lines 23/23

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                          29x 29x 29x     29x 60x   60x 60x   60x 2309x 2309x     2309x 57x 57x 57x 57x     57x 7x 8x 6x         6x                   57x 4x                   60x    
// Assertation 1:
// "Parameters MUST have an `in` property."
// `in` is REQUIRED. Possible values are "query", "header", "path" or "cookie".
 
// Assertation 2:
// A parameter MUST contain either a schema property, or a content property, but not both.
 
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#parameterObject
 
// Assertation 3:
// A paramater should not use schema type: string, format: binary because there is now well-
// defined way to encode an octet sequence in a URL.
 
const { isParameterObject, walk } = require('../../../utils');
const MessageCarrier = require('../../../utils/messageCarrier');
const findOctetSequencePaths = require('../../../utils/findOctetSequencePaths')
  .findOctetSequencePaths;
 
module.exports.validate = function({ jsSpec }, config) {
  const messages = new MessageCarrier();
 
  const configSchemas = config.schemas;
  config = config.parameters;
 
  walk(jsSpec, [], function(obj, path) {
    const isContentsOfParameterObject = isParameterObject(path, true); // 2nd arg is isOAS3
    const isRef = !!obj.$ref;
 
    // obj is a parameter object
    if (isContentsOfParameterObject && !isRef) {
      const binaryStringStatus = configSchemas.json_or_param_binary_string;
      Eif (binaryStringStatus !== 'off') {
        const octetSequencePaths = [];
        octetSequencePaths.push(
          ...findOctetSequencePaths(obj.schema, path.concat(['schema']))
        );
        if (obj.content) {
          Object.keys(obj.content).forEach(function(mimeType) {
            if (mimeType === 'application/json') {
              const paramContentPath = path.concat([
                'content',
                mimeType,
                'schema'
              ]);
              octetSequencePaths.push(
                ...findOctetSequencePaths(
                  obj.content[mimeType].schema,
                  paramContentPath
                )
              );
            }
          });
        }
 
        for (const p of octetSequencePaths) {
          messages.addMessage(
            p,
            'Parameters should not contain binary (type: string, format: binary) values.',
            binaryStringStatus,
            'json_or_param_binary_string'
          );
        }
      }
    }
  });
  return messages;
};