fix(web): validate ontology pagination (#138)
Some checks failed
CI / build (push) Has been cancelled
test / test (push) Has been cancelled

This commit is contained in:
RissRIce 2026-08-10 19:59:16 -06:00 committed by GitHub
parent edf9a1b063
commit 1cfec322ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 63 additions and 4 deletions

View file

@ -0,0 +1,17 @@
import { describe, test } from "node:test";
import assert from "node:assert/strict";
import { parseBoundedIntegerParam } from "./pagination.ts";
describe("pagination parameters", () => {
test("uses the fallback for negative, fractional, and malformed values", () => {
for (const value of [null, "-1", "1.5", "invalid", "9007199254740992"]) {
assert.equal(parseBoundedIntegerParam(value, 50, 1, 200), 50);
}
});
test("accepts valid values and caps the upper bound", () => {
assert.equal(parseBoundedIntegerParam(" 1 ", 50, 1, 200), 1);
assert.equal(parseBoundedIntegerParam("500", 50, 1, 200), 200);
assert.equal(parseBoundedIntegerParam("0", 0, 0, Number.MAX_SAFE_INTEGER), 0);
});
});

View file

@ -0,0 +1,13 @@
export function parseBoundedIntegerParam(
value: string | null,
fallback: number,
minimum: number,
maximum: number
): number {
const text = value?.trim() ?? "";
if (!/^\d+$/.test(text)) return fallback;
const parsed = Number(text);
if (!Number.isSafeInteger(parsed) || parsed < minimum) return fallback;
return Math.min(parsed, maximum);
}