Validate Integer Query Parameters Without parseInt Truncation
A leaderboard page receives ?pageSize=25items. Its loader calls parseInt(value, 10) and fetches 25 rows. The request succeeds, hiding a malformed link that should have failed validation.
For a bounded count such as page size, validate the complete decoded string before converting it to a number. This recipe uses JavaScript modules in modern browsers or Node.js 20 or later, with an explicit policy for missing and repeated query parameters.
Reproduce Prefix Parsing
Run this example:
Passing radix 10 chooses the numeric base. It does not require every character to belong to that base. The ECMAScript parseInt algorithm stops at the first non-digit after handling whitespace and a possible sign. A valid prefix therefore produces a number even when the rest of the value is unsuitable.
Checking Number.isInteger(parseInt(text, 10)) afterward cannot detect the lost suffix. By that point, both "25" and "25items" have become the same number.
Define a Complete Decimal Contract
For this endpoint, accept only ASCII decimal digits representing an integer from 1 through the supplied maximum. Reject leading zeros, signs, whitespace, decimal points, exponents, and other characters. These are application rules; another endpoint may deliberately choose a different grammar.
Save this as positive-integer.mjs:
| |
The character check examines the entire string, including trailing line breaks. It has no global or sticky flag, so repeated calls do not share a regular-expression cursor. The length check rejects obviously oversized values before scanning or converting their contents. This does not limit the size of the URL already received; the surrounding server still needs its normal request-size limits.
Conversion happens only after the grammar check. The Number.isSafeInteger reference defines the exact integer range supported here. This function returns numeric counts for arithmetic. Keep opaque identifiers that may exceed that range as strings.
Distinguish Missing, Empty, and Repeated Parameters
Save this as page-size.mjs:
| |
Pass a URL object, for example new URL(window.location.href) in a browser. A missing key selects the default of 25. A present empty value fails. Repetition fails even when the values agree. The
URL Standard
specifies that getAll() returns every value associated with a name, which lets the application enforce this single-value policy.
Query decoding has already happened when the helper receives a value. A raw + in a query becomes a space, while %2B becomes a literal plus sign; this grammar rejects both. Percent-encoded digits such as %32%35 decode to 25 and are accepted. The contract concerns decoded values, rather than requiring a unique byte spelling of the original URL.
Read and validate page size before issuing the data request. Let the caller display an invalid-link error when validation throws. A server endpoint should enforce the same count and duplicate rules independently, because browser validation cannot constrain requests made by other clients.
Avoid Conversion-Based Defaults
These tempting alternatives change the chosen input policy:
Number() consumes the whole numeric representation, but accepts more syntax than this decimal-only contract. An integer check alone would accept several of these conversions. Likewise, || 25 merges malformed values and zero into the missing-value default. Using ?? after parseInt() also does not repair validation: NaN is not null or undefined.
Apply the default only when the key is absent. If a product wants forgiving text-field input, normalize that input in a separately defined flow. Silent trimming or clamping at this URL boundary would make rejected links appear valid and obscure the original error.
Test the Boundary Before Fetching
Save this as test-page-size.mjs beside the two modules and run node test-page-size.mjs:
| |
The tests separate absence from invalid presence, verify the inclusive upper bound, and exercise query decoding and repetition. Keeping the parser independent of network calls makes these boundary rules easy to test before a request can use the count.