Skip to content

fix: enforce the length bound on the trivially-satisfiable fast path - #82

Merged
Saeris merged 1 commit into
mainfrom
fix/explicit-max-length
Nov 4, 2026
Merged

Saeris merged 1 commit into
mainfrom
fix/explicit-max-length

Conversation

@Saeris

@Saeris Saeris commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Summary

generateString has a fast path that skips enforce/satisfies when a string context has no constraints beyond the default bounds. That assumed every generator stays under DEFAULT_MAX_LENGTH (128), but auto-discovered key-name generators such as faker.lorem.text() for a text key regularly return 300+ characters. A schema like v.object({ text: v.pipe(v.string(), v.maxGraphemes(300)) }) therefore produced a value the schema rejected.

Found while integrating Valimock 1.7 into @saeris/valicon, where app.bsky.feed.post#main has exactly that text field.

Fix

The fast path keeps its cheap exit but routes the candidate through enforce when it overshoots bounds.max. That is one .length comparison on the hot path, so the perf motivation for the fast path is preserved.

Tests

  • New regression test in mockString.spec.ts that mocks a text key with maxGraphemes(300) 50 times and parses each result. Verified it fails on main and passes with the fix.
  • vp check and the full vp test run (234 tests) pass.

`generateString` skips `enforce`/`satisfies` when a context has no constraints beyond the default bounds. That assumed every generator stays under `DEFAULT_MAX_LENGTH`, but auto-discovered key-name generators such as `faker.lorem.text()` for a `text` key regularly return 300+ characters, so a schema like `v.pipe(v.string(), v.maxGraphemes(300))` on a `text` field got a value the schema rejected. The fast path now truncates through `enforce` when the candidate overshoots `bounds.max`, which is a single length check on the hot path.
@codesandbox

codesandbox Bot commented Nov 4, 2026

Copy link
Copy Markdown

Review or Edit in CodeSandbox

Open the branch in Web Editor • VS Code • Insiders

Open Preview

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

bumpy-frog

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

patch Patch releases

  • valimock 1.7.0 → 1.7.1

Bump files in this PR

Click here if you want to add another bump file to this PR


This comment is maintained by bumpy.

@Saeris
Saeris merged commit 875a107 into main Nov 4, 2026
4 checks passed
@Saeris
Saeris deleted the fix/explicit-max-length branch November 4, 2026 21:46
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.

1 participant