All files / utils/schemas field-definition-schema.js

100% Statements 37/37
100% Branches 71/71
100% Functions 3/3
100% Lines 35/35

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      7x                                               7x                                       207x   207x 4x     203x 14x   14x       15x           14x     198x 30x 88x 30x 1x   29x 2x   27x 2x       193x 111x 1x   110x         1x       191x 6x 6x 3x                     188x 11x 2x   9x         2x       184x     7x  
// Canonical field type enum — mirrors EntityField.type in api-definition.yaml.
// This is the wire-format source of truth; the flows-ui EntityField TS union
// is expected to match it (see flows-ui/src/stores/entities.ts).
const FIELD_TYPES = [
  'text',
  'textarea',
  'number',
  'email',
  'date',
  'boolean',
  'select',
  'multiselect',
  'image',
  'image_set',
  'file',
  'file_set',
  'geo_location',
  'workflow',
  'relation',
  'user',
  'entity_ref'
]
 
// A unique-field reservation's DynamoDB partition key embeds the raw value
// (entityId#fieldId#lang#value — see utils/uniqueFieldValue.js), so it must
// stay comfortably under the 2048-byte key size limit even for multi-byte
// UTF-8 values.
const MAX_UNIQUE_FIELD_LENGTH = 500
 
// Validates the shape of a single entity field *definition* (an element of
// body.fields on entity create/update) — deliberately narrow in scope: only
// the four gaps confirmed to have zero validation today (unknown type,
// malformed select/multiselect options, unsound number min/max/step, and
// non-positive length/maxLines). Everything else (name, mandatory, unique,
// reference, image constraints, workflowDefinition, ...) is left untouched —
// see validateFieldNames.js, validateWorkflowFields.js, etc. for those.
//
// Hand-rolled (no schema library): Lambdas here are deployed as a plain zip
// of this directory (see infrastructure/lib/api-stack.ts — Code.fromAsset,
// no bundler), which does not include node_modules, so a runtime dependency
// on a third-party package like Zod isn't deployable without switching every
// Lambda to esbuild-based bundling (aws-lambda-nodejs). Matches the style of
// every other validator in this directory.
//
// Returns the first violation found, matching the order these checks used to
// run in when this was a single Zod superRefine.
function validateFieldDefinition(field) {
  const type = field.type
 
  if (type !== undefined && !FIELD_TYPES.includes(type)) {
    return { valid: false, code: 'INVALID_FIELD_TYPE' }
  }
 
  if (type === 'select' || type === 'multiselect') {
    const options = field.options
    const validShape =
      Array.isArray(options) &&
      options.length > 0 &&
      options.every(
        (opt) =>
          (typeof opt === 'string' && opt.trim().length > 0) ||
          (opt &&
            typeof opt === 'object' &&
            typeof opt.value === 'string' &&
            opt.value.trim().length > 0)
      )
    if (!validShape) return { valid: false, code: 'INVALID_SELECT_OPTIONS' }
  }
 
  if (type === 'number') {
    const { min, max, step } = field
    const numericOrUndefined = (v) => v === undefined || typeof v === 'number'
    if (!numericOrUndefined(min) || !numericOrUndefined(max) || !numericOrUndefined(step)) {
      return { valid: false, code: 'INVALID_NUMBER_CONSTRAINTS' }
    }
    if (typeof min === 'number' && typeof max === 'number' && min > max) {
      return { valid: false, code: 'INVALID_NUMBER_CONSTRAINTS' }
    }
    if (typeof step === 'number' && step <= 0) {
      return { valid: false, code: 'INVALID_NUMBER_CONSTRAINTS' }
    }
  }
 
  if (type === 'text' || type === 'textarea' || type === 'email') {
    if (field.length !== undefined && (typeof field.length !== 'number' || field.length <= 0)) {
      return { valid: false, code: 'INVALID_NUMBER_CONSTRAINTS' }
    }
    if (
      type === 'textarea' &&
      field.maxLines !== undefined &&
      (typeof field.maxLines !== 'number' || field.maxLines <= 0)
    ) {
      return { valid: false, code: 'INVALID_NUMBER_CONSTRAINTS' }
    }
  }
 
  if (type === 'file_set') {
    const { maxFiles } = field
    if (maxFiles !== undefined && (typeof maxFiles !== 'number' || maxFiles <= 0)) {
      return { valid: false, code: 'INVALID_FILE_SET_CONSTRAINTS' }
    }
  }
 
  // `unique: true` is enforced via a reservation row keyed by the raw value
  // (see utils/uniqueFieldValue.js) — restricted to text/email, the only
  // types that are (a) meaningfully "one exact value per item" and (b)
  // guaranteed to have a bounded, string-shaped value once `length` is set.
  // Only applies to newly-saved field definitions — an entity that already
  // has `unique: true` on some other type keeps working as-is (not in
  // production yet, so no existing data to migrate).
  if (field.unique === true) {
    if (type !== 'text' && type !== 'email') {
      return { valid: false, code: 'INVALID_UNIQUE_FIELD_TYPE' }
    }
    if (
      typeof field.length !== 'number' ||
      field.length <= 0 ||
      field.length > MAX_UNIQUE_FIELD_LENGTH
    ) {
      return { valid: false, code: 'UNIQUE_FIELD_REQUIRES_MAX_LENGTH' }
    }
  }
 
  return { valid: true }
}
 
module.exports = { validateFieldDefinition, FIELD_TYPES }