Skip to content

Conversation

@hiteshshetty-dev
Copy link
Contributor

@hiteshshetty-dev hiteshshetty-dev commented Jan 13, 2026

Implement CSLP Value Validation

Summary

This PR introduces a new isValidCslp() function to validate CSLP (ContentStack Live Preview) value strings before processing them throughout the codebase. This improves robustness by preventing errors from invalid or malformed CSLP values and ensures proper handling of both v1 and v2 CSLP formats.

Changes

Core Implementation

  • Added isValidCslp() function in src/cslp/cslpdata.ts:

    • Validates CSLP values for both v1 and v2 formats
    • Uses TypeScript type predicate for type narrowing
    • Validates required parts (content_type_uid, entry_uid/entry_uid_variant_uid, locale) are non-empty
    • For v2 format, ensures entry_uid_variant_uid contains both entry_uid and variant_uid separated by underscore
    • Returns false for null, undefined, empty strings, and malformed values
  • Added comprehensive test suite in src/cslp/__test__/cslpdata.test.ts:

    • 120+ lines of test coverage
    • Tests for valid v1 and v2 formats (with and without field paths)
    • Tests for invalid cases (null, undefined, empty strings, malformed formats, whitespace-only strings)
    • Edge cases for v2 format validation (missing underscores, empty parts)

Integration Updates

Replaced simple truthiness checks (if (cslp)) with proper validation (if (isValidCslp(cslp))) across 17 files

Benefits

  1. Type Safety: TypeScript type predicate ensures validated CSLP values are properly typed as string
  2. Error Prevention: Prevents runtime errors from invalid CSLP values being passed to extractDetailsFromCslp()
  3. Format Validation: Ensures v2 format CSLP values have proper structure (entry_uid_variant_uid with underscore)
  4. Consistency: Standardizes CSLP validation across the entire codebase
  5. Maintainability: Centralized validation logic makes it easier to update validation rules in the future

Testing

  • ✅ Comprehensive unit tests added (120+ lines)
  • ✅ Tests cover both valid and invalid cases
  • ✅ Edge cases for v1 and v2 formats are tested
  • ✅ All existing functionality continues to work with validation in place

Files Changed

  • 17 files modified
  • 242 insertions, 43 deletions
  • 1 new function exported (isValidCslp)
  • 1 test suite expanded (122 new test lines)

Breaking Changes

None - This is a backward-compatible enhancement that adds validation without changing existing behavior for valid CSLP values.

@hiteshshetty-dev hiteshshetty-dev marked this pull request as ready for review January 13, 2026 10:34
@hiteshshetty-dev hiteshshetty-dev requested review from a team as code owners January 13, 2026 10:34
@github-actions
Copy link

github-actions bot commented Jan 13, 2026

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 71.7% 8051 / 11228
🔵 Statements 71.7% 8051 / 11228
🔵 Functions 74.82% 321 / 429
🔵 Branches 85.54% 1213 / 1418
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/cslp/cslpdata.ts 98.65% 95% 100% 98.65% 255-256
src/livePreview/editButton/editButton.ts 97.13% 89.85% 95% 97.13% 107-109, 221-222, 266-267, 400-401, 563-566, 613-614
src/timeline/compare/compare.ts 43.8% 75% 50% 43.8% 31-32, 48-49, 56-57, 63-84, 87-94, 100-116, 122-139
src/visualBuilder/index.ts 60.94% 64.7% 42.85% 60.94% 102-109, 112-116, 119-131, 134-207, 213-248, 256-278, 329-332, 335-336, 389-395
src/visualBuilder/components/fieldLabelWrapper.tsx 76.16% 75.38% 72.72% 76.16% 48-50, 58-60, 144-157, 160-164, 183-212, 233, 237-253, 281-282, 286-291, 295, 360-362, 368-386
src/visualBuilder/eventManager/useRecalculateVariantDataCSLPValues.ts 9.86% 100% 50% 9.86% 23-27, 31-178
src/visualBuilder/eventManager/useRevalidateFieldDataPostMessageEvent.ts 100% 91.3% 80% 100%
src/visualBuilder/eventManager/useVariantsPostMessageEvent.ts 98.71% 92.1% 100% 98.71% 50-51
src/visualBuilder/generators/generateEmptyBlock.tsx 12.19% 100% 0% 12.19% 7-36, 39-48
src/visualBuilder/generators/generateHighlightedComment.tsx 27.45% 100% 33.33% 27.45% 20-72, 80-102, 111-115, 118-120, 143-147, 162
src/visualBuilder/generators/generateOverlay.tsx 91.04% 84% 100% 91.04% 131-140, 198-201, 226-229
src/visualBuilder/listeners/mouseClick.ts 75.59% 66.66% 90.9% 75.59% 89, 101-107, 113-114, 117-122, 135-163, 175-176, 189-190, 235, 294-300, 341-347, 365-375
src/visualBuilder/utils/getCsDataOfElement.ts 100% 100% 100% 100%
src/visualBuilder/utils/getEntryIdentifiersInCurrentPage.ts 100% 100% 100% 100%
src/visualBuilder/utils/getVisualBuilderRedirectionUrl.ts 100% 100% 100% 100%
src/visualBuilder/utils/updateFocussedState.ts 58.24% 64.4% 100% 58.24% 45, 57-59, 61-63, 65, 208-211, 213-244, 334-338, 341-358, 361-371, 374-391, 400-445, 452-474, 480-482
Generated in workflow #736 for commit bbfcf29 by the Vitest Coverage Report Action

Copy link
Contributor

@karancs06 karancs06 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants