Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
4.0 KB
Raw Blame
import { validateDisplayField } from '@clerum/display-field'
import { RFC1123_RE } from '../../http/rfc1123.js'

/**
 * Application-level validation of resource identifiers and display fields for the
 * generic admin CRUD router (hosts | contexts | communication-channels |
 * mcp-servers).
 *
 * Design (spec alfredo-agent-rename, Decision #7 + F0.3):
 *   - IDENTIFIERS are RFC1123 and enforced HERE, not in the CRD schema. Hardening
 *     an existing field on a live CRD makes the apiserver validate the whole
 *     object on every write (including controller /status patches) and would
 *     reject writes on legacy CRs. So the rules live in control-api instead.
 *   - DISPLAY fields (host.spec.host, context.spec.displayName) are FREE TEXT:
 *     only trimmed length and control characters are constrained.
 *   - RATCHET: on update a field is validated ONLY IF its value changed vs the
 *     current CR, so a legacy resource whose display/identifier is out of norm is
 *     never blocked for unrelated edits.
 */

// Display-field validation (free-text: control/bidi chars + trimmed length) is
// the single source of truth in `@clerum/display-field`, shared browser-safe so
// control-ui's edit preflight applies the same rule without re-deriving the
// regex (D4). Re-exported here under the same names so existing consumers/tests
// that import from `resourceFieldValidation.js` keep working.
export { validateDisplayField, DISPLAY_FIELD_MAX_LENGTH } from '@clerum/display-field'

export interface FieldIssue {
  field: string
  message: string
}

/**
 * Validate a `metadata.name` as an RFC1123 DNS label. Returns the FIX-A1 error
 * envelope (`{error:'invalid_name', field:'metadata.name', message}`) or null.
 * A missing/non-string name is rejected the same way an empty one is: the K8s
 * apiserver requires a valid name and we would otherwise collapse its 422 to 500.
 */
export function validateResourceName(
  name: unknown
): { error: 'invalid_name'; field: 'metadata.name'; message: string } | null {
  if (typeof name === 'string' && RFC1123_RE.test(name)) return null
  return {
    error: 'invalid_name',
    field: 'metadata.name',
    message:
      'metadata.name must be a valid RFC1123 DNS label: lowercase alphanumeric characters or ' +
      "'-', starting and ending with an alphanumeric character, at most 63 characters.",
  }
}

/**
 * Validate an identifier field (present only) as RFC1123. Returns an issue or
 * null. Skips when absent — presence of identifiers is enforced by the CRD
 * schema / apiserver, not here.
 */
export function validateIdentifierField(value: unknown, field: string): FieldIssue | null {
  if (value === undefined) return null
  if (typeof value === 'string' && RFC1123_RE.test(value)) return null
  return {
    field,
    message:
      `${field} must be a valid RFC1123 DNS label: lowercase alphanumeric characters or ` +
      "'-', starting and ending with an alphanumeric character, at most 63 characters.",
  }
}

/**
 * Collect display/identifier field issues for a hosts|contexts spec, applying the
 * ratchet: when `currentSpec` is provided (update), a field is validated only if
 * its value differs from the current CR; when null (create), every present field
 * is validated.
 */
export function collectResourceSpecFieldIssues(
  plural: 'hosts' | 'contexts',
  spec: Record<string, unknown>,
  currentSpec: Record<string, unknown> | null
): FieldIssue[] {
  const issues: FieldIssue[] = []
  const changed = (key: string): boolean => currentSpec === null || spec[key] !== currentSpec[key]

  if (plural === 'hosts') {
    if (changed('host')) {
      const issue = validateDisplayField(spec.host, 'spec.host')
      if (issue) issues.push(issue)
    }
  } else {
    if (changed('displayName')) {
      const issue = validateDisplayField(spec.displayName, 'spec.displayName')
      if (issue) issues.push(issue)
    }
    if (changed('contextId')) {
      const issue = validateIdentifierField(spec.contextId, 'spec.contextId')
      if (issue) issues.push(issue)
    }
  }

  return issues
}