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

100% Statements 28/28
100% Branches 29/29
100% Functions 10/10
100% Lines 28/28

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 8329x 29x   29x 40x   40x   40x 33x 125x 125x       40x 38x 85x 116x 114x 115x 76x                           116x 114x 232x 39x                                   40x 240x 6x 6x         40x       6x 6x         3x                
const each = require('lodash/each');
const MessageCarrier = require('../../../utils/messageCarrier');
 
module.exports.validate = function({ resolvedSpec }) {
  const messages = new MessageCarrier();
 
  const schemas = [];
 
  if (resolvedSpec.definitions) {
    each(resolvedSpec.definitions, (def, name) => {
      def.name = name;
      schemas.push({ schema: def, path: ['definitions', name] });
    });
  }
 
  if (resolvedSpec.paths) {
    each(resolvedSpec.paths, (path, pathName) => {
      each(path, (op, opName) => {
        if (op && op.parameters) {
          op.parameters.forEach((parameter, parameterIndex) => {
            if (parameter.in === 'body' && parameter.schema) {
              schemas.push({
                schema: parameter.schema,
                path: [
                  'paths',
                  pathName,
                  opName,
                  'parameters',
                  parameterIndex.toString(),
                  'schema'
                ]
              });
            }
          });
        }
        if (op && op.responses) {
          each(op.responses, (response, responseName) => {
            if (response && response.schema) {
              schemas.push({
                schema: response.schema,
                path: [
                  'paths',
                  pathName,
                  opName,
                  'responses',
                  responseName,
                  'schema'
                ]
              });
            }
          });
        }
      });
    });
  }
 
  schemas.forEach(({ schema, path }) => {
    if (Array.isArray(schema.properties) && Array.isArray(schema.required)) {
      schema.properties.forEach(() => {
        generateReadOnlyErrors(schema, path, messages);
      });
    }
  });
 
  return messages;
};
 
function generateReadOnlyErrors(schema, contextPath, messages) {
  schema.properties.forEach((property, i) => {
    if (
      property.name &&
      property.readOnly &&
      schema.required.indexOf(property.name) > -1
    ) {
      messages.addMessage(
        contextPath.concat(['required', i.toString()]),
        'Read only properties cannot marked as required by a schema.',
        'error'
      );
    }
  });
}