Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMR-10146: Making sure validBbox compares numbers and not strings #378

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/middleware/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import chai from "chai";
const { expect } = chai;

import { validBbox } from "../index";

describe("validBBOX", () => {
describe("when bbox is a string", () => {
it("returns a valid bbox", async () => {
const bbox = "-122.09,39.89,-122.03,39.92";
expect(validBbox(bbox)).to.equal(true);
});
});

describe("when bbox is a string array", () => {
it("returns a valid bbox", async () => {
const bbox = ["-122.09", "39.89", "-122.03", "39.92"];
expect(validBbox(bbox)).to.equal(true);
});
});

describe("when bbox is an invalid string array with negative numbers", () => {
it("returns an invalid bbox", async () => {
const bbox = ["-122.03", "39.89", "-122.09", "39.92"];
expect(validBbox(bbox)).to.equal(false);
});
});

describe("when bbox is a number array", () => {
it("returns a valid bbox", async () => {
const bbox = [-122.09, 39.89, -122.03, 39.92];
expect(validBbox(bbox)).to.equal(true);
});
});
});
19 changes: 12 additions & 7 deletions src/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ const validLat = (lat: number) => inclusiveBetween(lat, -90.0, 90.0);

const validLon = (lon: number) => inclusiveBetween(lon, -180.0, 180.0);

const validBbox = (bbox: string | number[]) => {
// validBbox should be able to accept a comma separated string, a string array and
// a number array. We noticed the string array should be accepted when running the
// R Stac script.
export const validBbox = (bbox: string | string[] | number[]) => {
const parsedBbox = typeof bbox === "string" ? parseOrdinateString(bbox) : bbox;

if (parsedBbox.length !== 4 && parsedBbox.length !== 6) return false;
Expand All @@ -201,13 +204,15 @@ const validBbox = (bbox: string | number[]) => {
} else {
[swLon, swLat, , neLon, neLat] = parsedBbox;
}

return (
validLon(swLon) &&
validLat(swLat) &&
validLon(neLon) &&
validLat(neLat) &&
swLat <= neLat &&
swLon <= neLon
validLon(Number(swLon)) &&
validLat(Number(swLat)) &&
validLon(Number(neLon)) &&
validLat(Number(neLat)) &&
// Ensure that number comparisons are used instead of string comparisons
Number(swLat) <= Number(neLat) &&
Number(swLon) <= Number(neLon)
);
};

Expand Down