mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-09-10 19:26:00 +00:00
fix(web): validate ontology pagination (#138)
This commit is contained in:
parent
edf9a1b063
commit
1cfec322ac
6 changed files with 63 additions and 4 deletions
17
apps/logicsrc-web/src/lib/pagination.test.ts
Normal file
17
apps/logicsrc-web/src/lib/pagination.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
13
apps/logicsrc-web/src/lib/pagination.ts
Normal file
13
apps/logicsrc-web/src/lib/pagination.ts
Normal 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue