Skip to content

Fix stateful Schema.isPattern validation - #6712

Merged
gcanti merged 1 commit into
mainfrom
fix/schema-is-pattern-stateful-regexp
Jul 28, 2026
Merged

Fix stateful Schema.isPattern validation#6712
gcanti merged 1 commit into
mainfrom
fix/schema-is-pattern-stateful-regexp

Conversation

@gcanti

@gcanti gcanti commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • Bug Fixes
    • Made schema pattern validation deterministic when using global or sticky regular expressions.
    • Prevented regular expression state from leaking between validation runs.
  • Tests
    • Added coverage confirming repeated validations succeed and regular expression state is reset consistently.
  • Documentation
    • Clarified pattern-matching behavior and how regular expression flags are handled.

@github-project-automation github-project-automation Bot moved this to Discussion Ongoing in PR Backlog Jul 28, 2026
@changeset-bot

changeset-bot Bot commented Jul 28, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1281480

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 29 packages
Name Type
effect Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-deno Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/vitest Patch
@effect/ai-anthropic Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/docgen Patch
@effect/openapi-generator Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@effect-slopcop effect-slopcop Bot added 4.0 bug Something isn't working labels Jul 28, 2026
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Schema.isPattern now uses a cloned, reset regular expression for deterministic matching, with tests covering global and sticky flags and a patch changeset documenting the fix.

Changes

Schema pattern state

Layer / File(s) Summary
Deterministic pattern implementation
packages/effect/src/SchemaAST.ts
SchemaAST.isPattern clones the supplied regular expression, resets lastIndex before each test, and documents source-only arbitrary derivation.
Pattern validation and release note
packages/effect/test/schema/Schema.test.ts, .changeset/fix-schema-pattern-state.md
Tests verify repeated decoding with global and sticky expressions, and the changeset records the patch-level fix.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
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.

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/effect/test/schema/Schema.test.ts (1)

1250-1257: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Also test a pre-existing non-zero lastIndex.

Initialize each expression with lastIndex = 1 before decoding and assert it remains 1. This verifies both that validation starts from zero and that caller-owned RegExp state is preserved.

Proposed test adjustment
         for (const regExp of [/^a/g, /^a/y]) {
+          regExp.lastIndex = 1
           const schema = Schema.String.check(Schema.isPattern(regExp))
           const decoding = new TestSchema.Asserts(schema).decoding()

           await decoding.succeed("a")
           await decoding.succeed("a")
-          strictEqual(regExp.lastIndex, 0)
+          strictEqual(regExp.lastIndex, 1)
         }
🤖 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/effect/test/schema/Schema.test.ts` around lines 1250 - 1257, Update
the “isPattern with stateful RegExp flags” test to set each RegExp’s lastIndex
to 1 before decoding, then assert it remains 1 after validation. Preserve the
existing repeated decoding assertions for both global and sticky expressions,
verifying validation resets its working position without mutating caller-owned
RegExp state.
🤖 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.

Nitpick comments:
In `@packages/effect/test/schema/Schema.test.ts`:
- Around line 1250-1257: Update the “isPattern with stateful RegExp flags” test
to set each RegExp’s lastIndex to 1 before decoding, then assert it remains 1
after validation. Preserve the existing repeated decoding assertions for both
global and sticky expressions, verifying validation resets its working position
without mutating caller-owned RegExp state.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: efeaa7d8-7117-43d5-8eb0-223c8f12ce3a

📥 Commits

Reviewing files that changed from the base of the PR and between 1a7ce81 and 1281480.

📒 Files selected for processing (3)
  • .changeset/fix-schema-pattern-state.md
  • packages/effect/src/SchemaAST.ts
  • packages/effect/test/schema/Schema.test.ts

@github-actions

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

File Name Current Size Previous Size Difference
basic.ts 6.63 KB 6.63 KB 0.00 KB (0.00%)
batching.ts 9.42 KB 9.42 KB 0.00 KB (0.00%)
brand.ts 6.29 KB 6.29 KB 0.00 KB (0.00%)
cache.ts 10.12 KB 10.12 KB 0.00 KB (0.00%)
config.ts 19.90 KB 19.89 KB +0.02 KB (+0.08%)
differ.ts 19.07 KB 19.06 KB +0.02 KB (+0.10%)
http-client.ts 20.94 KB 20.94 KB 0.00 KB (0.00%)
logger.ts 10.28 KB 10.28 KB 0.00 KB (0.00%)
metric.ts 8.55 KB 8.55 KB 0.00 KB (0.00%)
optic.ts 7.45 KB 7.45 KB 0.00 KB (0.00%)
pubsub.ts 14.26 KB 14.26 KB 0.00 KB (0.00%)
queue.ts 11.09 KB 11.09 KB 0.00 KB (0.00%)
schedule.ts 10.27 KB 10.27 KB 0.00 KB (0.00%)
schema-class.ts 18.86 KB 18.84 KB +0.02 KB (+0.12%)
schema-fromJsonSchemaDocument.ts 28.77 KB 28.75 KB +0.01 KB (+0.04%)
schema-representation-roundtrip.ts 25.07 KB 25.05 KB +0.01 KB (+0.06%)
schema-string-transformation.ts 12.96 KB 12.96 KB 0.00 KB (0.00%)
schema-string.ts 10.65 KB 10.65 KB 0.00 KB (0.00%)
schema-template-literal.ts 14.85 KB 14.85 KB 0.00 KB (0.00%)
schema-toArbitraryLazy.ts 21.64 KB 21.62 KB +0.02 KB (+0.09%)
schema-toCodeDocument.ts 24.09 KB 24.07 KB +0.01 KB (+0.06%)
schema-toCodecJson.ts 18.99 KB 18.97 KB +0.02 KB (+0.11%)
schema-toEquivalence.ts 18.71 KB 18.69 KB +0.02 KB (+0.10%)
schema-toFormatter.ts 18.58 KB 18.56 KB +0.02 KB (+0.10%)
schema-toJsonSchemaDocument.ts 22.21 KB 22.19 KB +0.02 KB (+0.09%)
schema-toRepresentation.ts 19.25 KB 19.23 KB +0.02 KB (+0.10%)
schema.ts 18.12 KB 18.10 KB +0.02 KB (+0.10%)
stm.ts 12.05 KB 12.05 KB 0.00 KB (0.00%)
stream.ts 9.37 KB 9.37 KB 0.00 KB (0.00%)

@gcanti
gcanti merged commit 951d06b into main Jul 28, 2026
15 checks passed
@gcanti
gcanti deleted the fix/schema-is-pattern-stateful-regexp branch July 28, 2026 19:40
@github-project-automation github-project-automation Bot moved this from Discussion Ongoing to Done in PR Backlog Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.0 bug Something isn't working

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant