Skip to content

Commit 5e9b2ea

Browse files
committed
fix: accept only numbers for listing pagination options
1 parent 38a4a9d commit 5e9b2ea

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

‎src/project/types/website/listing/website-listing-read.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,13 @@ export function resolvePaginationOptions(
170170
listingId: string,
171171
raw: unknown,
172172
): PaginationOptions | undefined {
173-
if (raw === undefined || raw === null || typeof raw !== "object") {
173+
if (raw === undefined) {
174+
return undefined;
175+
}
176+
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
177+
warning(
178+
`Listing '${listingId}': pagination must be an object with inner-window, outer-window, outer-window-left or outer-window-right, got '${raw}'. The option is ignored.`,
179+
);
174180
return undefined;
175181
}
176182
const resolved: PaginationOptions = {};
@@ -179,9 +185,8 @@ export function resolvePaginationOptions(
179185
if (value === undefined) {
180186
continue;
181187
}
182-
const n = Number(value);
183-
if (Number.isInteger(n) && n >= min) {
184-
resolved[key as keyof PaginationOptions] = n;
188+
if (typeof value === "number" && Number.isInteger(value) && value >= min) {
189+
resolved[key as keyof PaginationOptions] = value;
185190
} else {
186191
warning(
187192
`Listing '${listingId}': pagination option '${key}' must be a whole number of ${min} or more, got '${value}'. The option is ignored.`,

‎tests/unit/website-listing-pagination.test.ts‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,33 @@ unitTest("listing pagination - invalid values resolve to nothing", async () => {
7878
});
7979

8080
// deno-lint-ignore require-await
81-
unitTest("listing pagination - numeric strings are coerced", async () => {
82-
assertEquals(resolvePaginationOptions("x", { "inner-window": "3" }), {
83-
"inner-window": 3,
84-
});
81+
unitTest("listing pagination - only numbers are accepted", async () => {
82+
for (const value of ["3", null, true, [], {}]) {
83+
assertEquals(
84+
resolvePaginationOptions("x", { "inner-window": value }),
85+
undefined,
86+
`inner-window: ${JSON.stringify(value)}`,
87+
);
88+
}
8589
});
90+
91+
// deno-lint-ignore require-await
92+
unitTest(
93+
"listing pagination - valid options survive an invalid one",
94+
async () => {
95+
assertEquals(
96+
resolvePaginationOptions("x", { "inner-window": 0, "outer-window": 1 }),
97+
{ "outer-window": 1 },
98+
);
99+
},
100+
);
101+
102+
// deno-lint-ignore require-await
103+
unitTest(
104+
"listing pagination - a block that is not an object resolves to nothing",
105+
async () => {
106+
for (const value of ["wide", 3, true, ["inner-window"]]) {
107+
assertEquals(resolvePaginationOptions("x", value), undefined);
108+
}
109+
},
110+
);

0 commit comments

Comments
 (0)