All files / utils validateSpace.js

100% Statements 41/41
100% Branches 40/40
100% Functions 2/2
100% Lines 36/36

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  2x                 2x   2x   2x     2x   2x 58x                 46x   43x 2x     41x   41x 12x   11x 11x 28x 28x 8x 4x   4x     7x 7x 3x 1x   2x     6x 6x 2x 1x         1x     5x         34x 34x 28x   28x     2x  
// @implements UC-SPACE-001.1, UC-SPACE-003.1, UC-SPACE-017.1
const { validateJoinShape } = require('./spaceJoin')
 
/**
 * A flow's public space (`/space/{slug}` in flows-ui): whether it is published
 * and the theme of its public pages. See docs/architecture/FLOW_SPACE.md.
 *
 * Fonts are generic CSS families only, never a font URL: a public page must
 * not load a stylesheet chosen by a flow member.
 */
const SPACE_FONTS = ['sans', 'serif', 'rounded', 'mono']
 
const COLOR_REGEX = /^#[0-9a-f]{6}$/i
 
const THEME_COLOR_KEYS = ['primaryColor', 'secondaryColor', 'backgroundColor']
 
/** Whether the space follows the reader's dark mode ('auto', the default) or never does */
const DARK_MODES = ['auto', 'off']
 
const isPlainObject = (value) =>
  value !== null && typeof value === 'object' && !Array.isArray(value)
 
/**
 * Validate and normalize a `space` object sent to PUT /v1/flows/{flowId}.
 * Unknown keys are dropped so nothing unbounded can be stored on the flow.
 * @param {unknown} space - The client-supplied value
 * @returns {{ valid: true, value: object } | { valid: false, error: string }}
 */
function validateSpace(space) {
  if (!isPlainObject(space)) return { valid: false, error: 'space must be an object' }
 
  if (typeof space.enabled !== 'boolean') {
    return { valid: false, error: 'space.enabled must be a boolean' }
  }
 
  const value = { enabled: space.enabled }
 
  if (space.theme !== undefined && space.theme !== null) {
    if (!isPlainObject(space.theme)) return { valid: false, error: 'space.theme must be an object' }
 
    const theme = {}
    for (const key of THEME_COLOR_KEYS) {
      const color = space.theme[key]
      if (color === undefined || color === null || color === '') continue
      if (typeof color !== 'string' || !COLOR_REGEX.test(color)) {
        return { valid: false, error: `space.theme.${key} must be a #rrggbb color` }
      }
      theme[key] = color.toLowerCase()
    }
 
    const font = space.theme.font
    if (font !== undefined && font !== null && font !== '') {
      if (!SPACE_FONTS.includes(font)) {
        return { valid: false, error: `space.theme.font must be one of: ${SPACE_FONTS.join(', ')}` }
      }
      theme.font = font
    }
 
    const darkMode = space.theme.darkMode
    if (darkMode !== undefined && darkMode !== null && darkMode !== '') {
      if (!DARK_MODES.includes(darkMode)) {
        return {
          valid: false,
          error: `space.theme.darkMode must be one of: ${DARK_MODES.join(', ')}`
        }
      }
      theme.darkMode = darkMode
    }
 
    value.theme = theme
  }
 
  // Membership requests: only the shape here; the ids are checked against the
  // flow's data by update.js (spaceJoin.validateJoinTargets)
  const join = validateJoinShape(space.join)
  if (!join.valid) return join
  if (join.value) value.join = join.value
 
  return { valid: true, value }
}
 
module.exports = { validateSpace, SPACE_FONTS, DARK_MODES }