Repository navigation
fix: enforce the length bound on the trivially-satisfiable fast path - #82
Merged
Merged
Conversation
`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.
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
Contributor
|
The changes in this PR will be included in the next version bump.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
generateStringhas a fast path that skipsenforce/satisfieswhen a string context has no constraints beyond the default bounds. That assumed every generator stays underDEFAULT_MAX_LENGTH(128), but auto-discovered key-name generators such asfaker.lorem.text()for atextkey regularly return 300+ characters. A schema likev.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, whereapp.bsky.feed.post#mainhas exactly thattextfield.Fix
The fast path keeps its cheap exit but routes the candidate through
enforcewhen it overshootsbounds.max. That is one.lengthcomparison on the hot path, so the perf motivation for the fast path is preserved.Tests
mockString.spec.tsthat mocks atextkey withmaxGraphemes(300)50 times and parses each result. Verified it fails onmainand passes with the fix.vp checkand the fullvp testrun (234 tests) pass.