Skip to content

Commit

Permalink
CMR-10146: Making sure validBbox compares numbers and not strings (#378)
Browse files Browse the repository at this point in the history
* CMR-10146: Making sure validBbox compares numbers and not strings

* CMR-10146: Adding comments

* CMR-10146: Adjusting the bbox types and adding tests

* CMR-10146: Making test prettier
  • Loading branch information
william-valencia authored Nov 14, 2024
1 parent 5d86a25 commit a5aff1b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
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

0 comments on commit a5aff1b

Please sign in to comment.