Skip to content

chore: improve session validation logic by conditionally checking localization flag - #3296

Merged
eduardoformiga merged 1 commit into
canary-v2from
chore/check-localization-flag
May 5, 2026
Merged

eduardoformiga merged 1 commit into
canary-v2from
chore/check-localization-flag

Conversation

@eduardoformiga

@eduardoformiga eduardoformiga commented May 5, 2026 •

Copy link
Copy Markdown
Member

What's the purpose of this pull request?

This pull request introduces a conditional check for localization settings in the validateSession function to ensure that localization-related session updates only occur when localization is enabled. This helps prevent unnecessary or incorrect modifications to session data when localization is not in use.

Session validation improvements:

  • Added a check for storeConfig.localization?.enabled before updating session currency and channel based on localization settings in validateSession in session/index.ts. [1] [2]

Summary by CodeRabbit

  • Refactor
    • Enhanced session validation performance by optimizing localization enrichment logic to execute conditionally based on configuration settings.

@eduardoformiga
eduardoformiga requested a review from a team as a code owner May 5, 2026 19:58
@eduardoformiga
eduardoformiga requested review from emersonlaurentino and lariciamota and removed request for a team May 5, 2026 19:58
@coderabbitai

coderabbitai Bot commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

The validateSession function now conditionally applies locale, currency, and channel enrichment only when storeConfig.localization?.enabled is truthy. Previously this logic ran unconditionally; the change gates the entire enrichment block behind a localization-enabled check.

Changes

Session Localization Conditional Guard

Layer / File(s) Summary
Core Logic
packages/core/src/sdk/session/index.ts
The locale/channel/currency enrichment block (lines 143–158) is now wrapped in if (storeConfig.localization?.enabled), preventing unnecessary recomputation of newChanel, session.locale, and session.currency when localization is disabled.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • vtex/faststore#3268: Also modifies validateSession in the same file, changing validation flow and session-ready logic.
  • vtex/faststore#3279: Directly related — introduces the localization enrichment logic that this PR conditionally gates.
  • vtex/faststore#3285: Modifies validateSession to skip refresh-token logic in local environments.

Suggested labels

enhancement

Suggested reviewers

  • lariciamota
  • gabpaladino
  • hellofanny

Poem

🔑 Sessions now heed the localization call,
A conditional guard stands proud and tall,
When enabled flows true, enrichment takes flight,
When false, we skip through, clean and light! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding a conditional check for the localization flag in session validation logic.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/check-localization-flag

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codesandbox-ci

codesandbox-ci Bot commented May 5, 2026

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@eduardoformiga
eduardoformiga requested review from hellofanny, lemagnetic and ommeirelles and removed request for emersonlaurentino and lariciamota May 5, 2026 20:00
@pkg-pr-new

pkg-pr-new Bot commented May 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

@faststore/api

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/cli

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/components

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/core

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/diagnostics

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/lighthouse

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/sdk

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/ui

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

commit: caccbd1

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/src/sdk/session/index.ts`:
- Around line 145-147: The JSON.parse on session.channel when building newChanel
can throw on malformed persisted data; modify the code around newChanel (the
JSON.stringify call that spreads JSON.parse(session.channel ?? '{}')) to safely
parse session.channel (e.g., use a try/catch or a safeParse helper that returns
{} on error) before constructing newChanel so parsing errors do not occur
outside the existing validation try block (refer to newChanel and
session.channel in this file and ensure the safe parse happens prior to creating
newChanel or move it inside the surrounding try that calls validateSession).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 12d8dd13-a517-485d-a4e1-c7a3c301604d

📥 Commits

Reviewing files that changed from the base of the PR and between d18bee0 and caccbd1.

📒 Files selected for processing (1)
  • packages/core/src/sdk/session/index.ts

Comment on lines +145 to +147
const newChanel = JSON.stringify({
...(JSON.parse(session.channel ?? '{}') ?? {}),
salesChannel: settings.salesChannel,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Guard session.channel parsing to avoid pre-validation crashes

At Line 146, JSON.parse(session.channel ?? '{}') can throw for malformed persisted data, and this happens before the try block (Line 179). That can break validateSession entirely when localization is enabled.

Suggested fix
   if (storeConfig.localization?.enabled) {
     const settings = getSettings()
-    const newChanel = JSON.stringify({
-      ...(JSON.parse(session.channel ?? '{}') ?? {}),
+    let parsedChannel: Record<string, unknown> = {}
+    try {
+      parsedChannel = JSON.parse(session.channel ?? '{}') ?? {}
+    } catch {
+      parsedChannel = {}
+    }
+
+    const newChanel = JSON.stringify({
+      ...parsedChannel,
       salesChannel: settings.salesChannel,
     })
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/core/src/sdk/session/index.ts` around lines 145 - 147, The
JSON.parse on session.channel when building newChanel can throw on malformed
persisted data; modify the code around newChanel (the JSON.stringify call that
spreads JSON.parse(session.channel ?? '{}')) to safely parse session.channel
(e.g., use a try/catch or a safeParse helper that returns {} on error) before
constructing newChanel so parsing errors do not occur outside the existing
validation try block (refer to newChanel and session.channel in this file and
ensure the safe parse happens prior to creating newChanel or move it inside the
surrounding try that calls validateSession).

@eduardoformiga
eduardoformiga merged commit aa54dce into canary-v2 May 5, 2026
5 of 6 checks passed
@eduardoformiga
eduardoformiga deleted the chore/check-localization-flag branch May 5, 2026 22:36
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