All files / items reference-validation.js

97.36% Statements 37/38
95.55% Branches 43/45
100% Functions 1/1
97.36% Lines 37/38

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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 1353x                   49x 24x       25x 25x   25x 11x 1x                 10x 3x 3x 3x 1x             2x 1x                 8x 9x           9x 4x                   18x 8x 1x               7x 7x 1x                 6x 2x 2x 2x               2x 1x                 5x 6x           6x 1x                   14x     3x      
const { GetCommand } = require('@aws-sdk/lib-dynamodb')
 
async function validateFieldReference({
  docClient,
  field,
  value,
  flowId,
  flowMembershipsTableName,
  itemsTableName
}) {
  if (value === undefined || value === null || value === '') {
    return { valid: true }
  }
 
  // Normalise to array to handle both single and multi-value fields
  const isMultiple = field.reference?.multiple === true
  const values = isMultiple && Array.isArray(value) ? value : [value]
 
  if (field.type === 'user') {
    if (!flowMembershipsTableName) {
      return {
        valid: false,
        error: 'INVALID_REFERENCE_VALUE',
        fieldId: field.fieldId,
        referenceType: 'user'
      }
    }
 
    // Validate multi-value count constraints
    if (isMultiple) {
      const min = field.reference?.minValues
      const max = field.reference?.maxValues
      if (min !== undefined && values.length < min) {
        return {
          valid: false,
          error: 'REFERENCE_MIN_VALUES',
          fieldId: field.fieldId,
          referenceType: 'user'
        }
      }
      if (max !== undefined && values.length > max) {
        return {
          valid: false,
          error: 'REFERENCE_MAX_VALUES',
          fieldId: field.fieldId,
          referenceType: 'user'
        }
      }
    }
 
    for (const userId of values) {
      const membershipResult = await docClient.send(
        new GetCommand({
          TableName: flowMembershipsTableName,
          Key: { userId, flowId }
        })
      )
      if (!membershipResult.Item || membershipResult.Item.status !== 'active') {
        return {
          valid: false,
          error: 'INVALID_REFERENCE_VALUE',
          fieldId: field.fieldId,
          referenceType: 'user'
        }
      }
    }
  }
 
  if (field.type === 'entity_ref') {
    if (!itemsTableName) {
      return {
        valid: false,
        error: 'INVALID_REFERENCE_VALUE',
        fieldId: field.fieldId,
        referenceType: 'entity_ref'
      }
    }
 
    const entityId = field.reference?.entityId
    if (!entityId) {
      return {
        valid: false,
        error: 'MISSING_REFERENCE_ENTITY',
        fieldId: field.fieldId,
        referenceType: 'entity_ref'
      }
    }
 
    // Validate multi-value count constraints
    if (isMultiple) {
      const min = field.reference?.minValues
      const max = field.reference?.maxValues
      Iif (min !== undefined && values.length < min) {
        return {
          valid: false,
          error: 'REFERENCE_MIN_VALUES',
          fieldId: field.fieldId,
          referenceType: 'entity_ref'
        }
      }
      if (max !== undefined && values.length > max) {
        return {
          valid: false,
          error: 'REFERENCE_MAX_VALUES',
          fieldId: field.fieldId,
          referenceType: 'entity_ref'
        }
      }
    }
 
    for (const itemId of values) {
      const itemResult = await docClient.send(
        new GetCommand({
          TableName: itemsTableName,
          Key: { entityId, id: itemId }
        })
      )
      if (!itemResult.Item) {
        return {
          valid: false,
          error: 'INVALID_REFERENCE_VALUE',
          fieldId: field.fieldId,
          referenceType: 'entity_ref'
        }
      }
    }
  }
 
  return { valid: true }
}
 
module.exports = {
  validateFieldReference
}