All files / src/plugins/validation/2and3/semantic-validators walker-ibm.js

100% Statements 19/19
95% Branches 19/20
100% Functions 3/3
100% Lines 19/19

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            29x 29x 29x     29x 97x   97x   97x   5150x 1613x     1613x 2x                       1613x 2x 2x         2x                     5150x 11532x         3x                 97x    
// Assertation 1:
// The description, when present, should not be empty or contain empty space
 
// Assertation 2:
// Description siblings to $refs should not exist if identical to referenced description
 
const at = require('lodash/at');
const { walk } = require('../../../utils');
const MessageCarrier = require('../../../utils/messageCarrier');
 
// Walks an entire spec.
module.exports.validate = function({ jsSpec, resolvedSpec }, config) {
  const messages = new MessageCarrier();
 
  config = config.walker;
 
  walk(jsSpec, [], function(obj, path) {
    // check for empty descriptions
    if (obj.description !== undefined && obj.description !== null) {
      const description = obj.description.toString();
 
      // verify description is not empty
      if (description.length === 0 || !description.trim()) {
        messages.addMessage(
          [...path, 'description'],
          'Items with a description must have content in it.',
          config.no_empty_descriptions,
          'no_empty_descriptions'
        );
      }
 
      // check description siblings to $refs
      // note: in general, siblings to $refs are discouraged and validated elsewhere.
      // this is a more specific check to flag duplicated descriptions for referenced schemas
      // (probably most useful when users turn of the $ref sibling validation)
      if (obj.$ref) {
        const referencedSchema = at(resolvedSpec, [path])[0];
        Eif (
          referencedSchema &&
          referencedSchema.description &&
          referencedSchema.description === description
        ) {
          messages.addMessage(
            [...path, 'description'],
            'Description sibling to $ref matches that of the referenced schema. This is redundant and should be removed.',
            config.duplicate_sibling_description,
            'duplicate_sibling_description'
          );
        }
      }
    }
 
    // check for and flag null values - they are not allowed by the spec and are likely mistakes
    Object.keys(obj).forEach(key => {
      if (
        key !== 'default' &&
        obj[key] === null &&
        path[path.length - 1] !== 'enum'
      ) {
        messages.addMessage(
          [...path, key],
          'Null values are not allowed for any property.',
          'error'
        );
      }
    });
  });
 
  return messages;
};