Skip to content

Refactor: Unify field type transformation logic by aligning resolutionValue and getFinalCastedValue #31911

@nicobytes

Description

@nicobytes

Task

Currently, field value transformation logic is handled in two places:

  • resolutionValue, a dictionary mapping field types to transformation functions
  • getFinalCastedValue, which performs an additional pass of validation and transformation

This results in duplicated logic and potentially inconsistent behavior. The transformation logic should be consolidated, ideally leveraging the resolutionValue dictionary as the single source of truth for type-specific transformations. Then, getFinalCastedValue should become a simpler function or be removed altogether.

Expected Changes

  • Ensure all transformations based on field types are defined in resolutionValue.
  • Move casting logic (currently in getFinalCastedValue) into specific functions mapped in resolutionValue.
  • Simplify getInitialFieldValue to only call the function from resolutionValue.
  • Eliminate redundant checks and casting logic from getFinalCastedValue.

Current Sample (Redundant)

const resolutionFn = resolutionValue[field.fieldType as FIELD_TYPES];
const value = resolutionFn(contentlet, field);
return getFinalCastedValue(value, field); // <- this part should be included in resolutionFn instead

Proposed Structure

const resolutionValue: Record<FIELD_TYPES, FnResolutionValue<unknown>> = {
  [FIELD_TYPES.DATE]: (contentlet, field) => {
    const raw = contentlet?.[field.variable];
    const parsed = new Date(raw as string);
    return isNaN(parsed.getTime()) ? raw : parsed;
  },
  [FIELD_TYPES.JSON]: (contentlet, field) => {
    const raw = contentlet?.[field.variable];
    return JSON.stringify(raw, null, 2);
  },
  // ...other types with their casting logic
};

Proposed Objective

Code Maintenance

Proposed Priority

Priority 4 - Trivial

Acceptance Criteria

  • All casting logic is moved to field-type-specific functions inside resolutionValue
  • getFinalCastedValue is either removed or refactored into simpler helpers reused in resolutionValue
  • No duplicate checks for field type casting in both resolution and casting stages
  • Unit tests updated/added for each field type logic

External Links... Slack Conversations, Support Tickets, Figma Designs, etc.

No response

Assumptions & Initiation Needs

No response

Quality Assurance Notes & Workarounds

No response

Sub-Tasks & Estimates

No response

Metadata

Metadata

Assignees

Type

Projects

Status

Next 2-4 Sprints

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions