Skip to content
Sarj Code Standards

no-numeric-search-param-coercion

Parse numeric URL search parameters with a schema instead of parseInt or Number.

Why

parseInt, parseFloat and Number accept a missing or malformed query value and return NaN or a truncated number (parseInt("10abc") is 10), which then reaches queries and pagination unchecked.

Fix

Parse the value with a schema that states the contract, for example z.coerce.number().int().positive().default(10).parse(params.get("page") ?? undefined).

Examples

Before — flagged Do not coerce a search parameter with parseInt
src/page.ts
export function page(params: URLSearchParams): number {
return parseInt(params.get("page") ?? "1", 10);
}
After — preferred Parse the page number with a schema
src/page.ts
declare const z: { coerce: { number(): { parse(value: unknown): number } } };
export function page(params: URLSearchParams): number {
return z.coerce.number().parse(params.get("page") ?? undefined);
}