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

100% Statements 29/29
100% Branches 12/12
100% Functions 2/2
100% Lines 29/29

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                        29x 29x 29x 29x 29x   29x 40x   40x   40x   40x 131x 8x     8x 2x         2x       6x 6x 1x         1x       5x   5x 1x         1x       4x 2x         2x       2x 1x               40x    
// Assertation 1:
// if discriminator exist inside schema object, it must be of type string
 
// Assertation 2:
// properties inside a schema object must include property defined by discriminator
 
// Assertation 3:
// required inside a schema must be an array
 
// Assertation 4:
// the schema must also list discriminator value as part of required
 
const each = require('lodash/each');
const has = require('lodash/has');
const get = require('lodash/get');
const includes = require('lodash/includes');
const MessageCarrier = require('../../../utils/messageCarrier');
 
module.exports.validate = function({ jsSpec }) {
  const messages = new MessageCarrier();
 
  const schemas = get(jsSpec, ['definitions'], []);
 
  const basePath = ['definitions'];
 
  each(schemas, (schema, schemaName) => {
    if (has(schema, 'discriminator')) {
      const { discriminator } = schema;
 
      // If discriminator is not an string, error out and return
      if (typeof discriminator !== 'string') {
        messages.addMessage(
          basePath.concat([schemaName, 'discriminator']).join('.'),
          'Discriminator must be of type string',
          'error'
        );
        return;
      }
 
      // If the schema's property doesn't include property defined in discriminator, error out and return
      const { properties } = schema;
      if (!has(properties, discriminator)) {
        messages.addMessage(
          basePath.concat([schemaName, 'discriminator']).join('.'),
          'The discriminator defined must also be defined as a property in this schema',
          'error'
        );
        return;
      }
 
      // required must exist
      const { required } = schema;
 
      if (!required) {
        messages.addMessage(
          basePath.concat([schemaName]).join('.'),
          'Required array not found. The discriminator defined must also be part of required properties',
          'error'
        );
        return;
      }
 
      // required must be an array
      if (!(required instanceof Array)) {
        messages.addMessage(
          basePath.concat([schemaName, 'required']).join('.'),
          'Required must be an array',
          'error'
        );
        return;
      }
 
      // discriminator must be in required
      if (!includes(required, discriminator)) {
        messages.addMessage(
          basePath.concat([schemaName, 'required']).join('.'),
          'Discriminator is not listed as part of required',
          'error'
        );
      }
    }
  });
  return messages;
};