fix: numeric validator accepts '123abc', truncate exceeds maxLength - #306
Open
fix2015 wants to merge 1 commit into
Open
fix: numeric validator accepts '123abc', truncate exceeds maxLength#306fix2015 wants to merge 1 commit into
fix2015 wants to merge 1 commit into
Conversation
- the numeric validation used parseFloat which parses "123abc" as 123 and passes validation. changed to Number() which correctly returns NaN for strings with trailing non-numeric characters. - truncate directive produced strings longer than maxLength because the suffix length wasn't subtracted from the slice. "hello world" truncated to length 8 with "..." suffix became 11 chars instead of 8.
Contributor
|
@fix2015 is attempting to deploy a commit to the Vercel Labs Team on Vercel. A member of the Team first needs to authorize it. |
| numeric: (value: unknown) => { | ||
| if (typeof value === "number") return !isNaN(value); | ||
| if (typeof value === "string") return !isNaN(parseFloat(value)); | ||
| if (typeof value === "string") return !isNaN(Number(value)) && isFinite(Number(value)); |
Contributor
There was a problem hiding this comment.
Suggested change
| if (typeof value === "string") return !isNaN(Number(value)) && isFinite(Number(value)); | |
| if (typeof value === "string") { | |
| const trimmed = value.trim(); | |
| if (trimmed === "") return false; | |
| return !isNaN(Number(trimmed)) && isFinite(Number(trimmed)); | |
| } |
The numeric validator accepts empty and whitespace-only strings as valid numbers because Number("") and Number(" ") both evaluate to a finite 0.
|
|
||
| if (text.length <= maxLength) return text; | ||
| return text.slice(0, maxLength) + suffix; | ||
| return text.slice(0, maxLength - suffix.length) + suffix; |
Contributor
There was a problem hiding this comment.
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.
Two bugs:
the `numeric` validator was using `parseFloat` which parses `"123abc"` as `123` and passes validation. Users could submit `"99bottles"` and it would be accepted as a valid number. Changed to `Number()` which correctly returns `NaN` for strings with trailing non-numeric characters.
the `$truncate` directive was producing strings longer than `maxLength` because it did `text.slice(0, maxLength) + suffix` without accounting for the suffix length. So `length: 50` with a `"..."` suffix would produce a 53-character string. Changed to `text.slice(0, maxLength - suffix.length)` so the final output actually respects the limit.