Skip to content

Commit

Permalink
Improve EMLGeoCoverage validation messages
Browse files Browse the repository at this point in the history
- For each error type, provide solutions for invalid geo coverages
- Move error messages to an object in the model (rather than a switch statement)
- Update tests to get error messages from the model

Issue #2159
  • Loading branch information
robyngit committed Nov 2, 2023
1 parent e0633ff commit 53934d4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 59 deletions.
75 changes: 39 additions & 36 deletions src/js/models/metadata/eml211/EMLGeoCoverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,44 @@ define(["jquery", "underscore", "backbone", "models/DataONEObject"], function (
};
},

/**
* The map of error keys to human-readable error messages to use for
* validation issues.
* @type {Object}
* @property {string} default - When the error is not in this list
* @property {string} north - When the Northwest latitude is not in the
* valid range
* @property {string} east - When the Southeast longitude is not in the
* valid range
* @property {string} south - When the Southeast latitude is not in the
* valid range
* @property {string} west - When the Northwest longitude is not in the
* valid range
* @property {string} missing - When a long or lat coordinate is missing
* @property {string} description - When a description is missing
* @property {string} needPair - When there are no coordinate pairs
* @property {string} northSouthReversed - When the North latitude is less
* than the South latitude
* @property {string} crossesAntiMeridian - When the bounding box crosses
* the anti-meridian
* @property {string} containsPole - When the bounding box contains the
* North or South pole
* @since x.x.x
*/
errorMessages: {
"default": "Please correct the geographic coverage.",
"north": "Northwest latitude out of range, must be >-90 and <90. Please correct the latitude.",
"east": "Southeast longitude out of range (-180 to 180). Please adjust the longitude.",
"south": "Southeast latitude out of range, must be >-90 and <90. Please correct the latitude.",
"west": "Northwest longitude out of range (-180 to 180). Check and correct the longitude.",
"missing": "Latitude and longitude are required for each coordinate. Please complete all fields.",
"description": "Missing location description. Please add a brief description.",
"needPair": "Location requires at least one coordinate pair. Please add coordinates.",
"northSouthReversed": "North latitude should be greater than South. Please swap the values.",
"crossesAntiMeridian": "Bounding box crosses the anti-meridian. Please use multiple boxes that meet at the anti-meridian instead.",
"containsPole": "Coordinates include a pole. Latitudes should be >-90 and <90."
},

/**
* Parses the objectDOM to populate this model with data.
* @param {Element} objectDOM - The EML object element
Expand Down Expand Up @@ -123,7 +161,6 @@ define(["jquery", "underscore", "backbone", "models/DataONEObject"], function (
* @returns {string} The XML string
*/
serialize: function () {

const objectDOM = this.updateDOM();
let xmlString = objectDOM?.outerHTML;
if (!xmlString) xmlString = objectDOM?.[0]?.outerHTML;
Expand Down Expand Up @@ -243,40 +280,7 @@ define(["jquery", "underscore", "backbone", "models/DataONEObject"], function (
* @return {string} The error message
*/
getErrorMessage: function (area) {
switch (area) {
case "north":
return "The Northwest latitude must be between -90 and 90.";
break;
case "east":
return "The Southeast longitude must be between -180 and 180.";
break;
case "south":
return "The Southeast latitude must be between -90 and 90.";
break;
case "west":
return "The Northwest longitude must be between -180 and 180.";
break;
case "missing":
return "Each coordinate must include a latitude AND longitude.";
break;
case "description":
return "Each location must have a description.";
break;
case "needPair":
return "Each location description must have at least one coordinate pair.";
break;
case "northSouthReversed":
return "The North latitude must be greater than the South latitude.";
break;
case "crossesAntiMeridian":
return "The bounding box cannot cross the anti-meridian.";
break;
case "containsPole":
return "The bounding box cannot contain the North or South pole.";
default:
return "";
break;
}
return this.errorMessages[area] || this.errorMessages.default;
},

/**
Expand Down Expand Up @@ -442,7 +446,6 @@ define(["jquery", "underscore", "backbone", "models/DataONEObject"], function (
}
}


if (Object.keys(errors).length) return errors;
else return false;
},
Expand Down
46 changes: 23 additions & 23 deletions test/js/specs/unit/models/metadata/eml211/EMLGeoCoverage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ define([
<southBoundingCoordinate>50.1600</southBoundingCoordinate>
</boundingCoordinates>
</geographicCoverage>`;
// remove ALL whitespace
// remove ALL whitespace
testEML = testEML.replace(/\s/g, "");
this.testEML = testEML;
});
Expand All @@ -35,7 +35,6 @@ define([

describe("parse()", function () {
it("should parse EML", function () {

var emlGeoCoverage = new EMLGeoCoverage(
{ objectDOM: this.testEML },
{ parse: true }
Expand All @@ -52,7 +51,6 @@ define([
});

describe("serialize()", function () {

it("should serialize to XML", function () {
var emlGeoCoverage = new EMLGeoCoverage(
{ objectDOM: this.testEML },
Expand Down Expand Up @@ -86,7 +84,7 @@ define([
{ parse: true }
);
var status = emlGeoCoverage.getCoordinateStatus();
var errors = emlGeoCoverage.generateStatusErrors(status);
const errors = emlGeoCoverage.generateStatusErrors(status);
expect(errors).to.be.empty;
});

Expand All @@ -96,10 +94,9 @@ define([
{ parse: true }
);
emlGeoCoverage.set("north", "100");
var errors = emlGeoCoverage.validate();
errors.north.should.equal(
"The Northwest latitude must be between -90 and 90."
);
const errors = emlGeoCoverage.validate();
const expectedMsg = emlGeoCoverage.errorMessages.north;
errors.north.should.equal(expectedMsg);
});

it("should give an error if the coordinates are missing", function () {
Expand All @@ -108,8 +105,9 @@ define([
{ parse: true }
);
emlGeoCoverage.set("north", "");
var errors = emlGeoCoverage.validate();
errors.north.should.equal("Each coordinate must include a latitude AND longitude.");
const errors = emlGeoCoverage.validate();
const expectedMsg = emlGeoCoverage.errorMessages.missing;
errors.north.should.equal(expectedMsg);
});

it("should give an error if the north and south coordinates are reversed", function () {
Expand All @@ -119,10 +117,10 @@ define([
);
emlGeoCoverage.set("north", "40");
emlGeoCoverage.set("south", "50");
var errors = emlGeoCoverage.validate();
const msg = "The North latitude must be greater than the South latitude.";
errors.north.should.equal(msg);
errors.south.should.equal(msg);
const errors = emlGeoCoverage.validate();
const expectedMsg = emlGeoCoverage.errorMessages.northSouthReversed;
errors.north.should.equal(expectedMsg);
errors.south.should.equal(expectedMsg);
});

it("should give an error if the bounds cross the anti-meridian", function () {
Expand All @@ -132,9 +130,10 @@ define([
);
emlGeoCoverage.set("west", "170");
emlGeoCoverage.set("east", "-170");
var errors = emlGeoCoverage.validate();
errors.west.should.equal("The bounding box cannot cross the anti-meridian.");
errors.east.should.equal("The bounding box cannot cross the anti-meridian.");
const errors = emlGeoCoverage.validate();
const expectedMsg = emlGeoCoverage.errorMessages.crossesAntiMeridian;
errors.west.should.equal(expectedMsg);
errors.east.should.equal(expectedMsg);
});

it("should give an error if the bounds contain the north pole", function () {
Expand All @@ -143,8 +142,9 @@ define([
{ parse: true }
);
emlGeoCoverage.set("north", "90");
var errors = emlGeoCoverage.validate();
errors.north.should.equal("The bounding box cannot contain the North or South pole.");
const errors = emlGeoCoverage.validate();
const expectedMsg = emlGeoCoverage.errorMessages.containsPole;
errors.north.should.equal(expectedMsg);
});

it("should give an error if the bounds contain the south pole", function () {
Expand All @@ -153,10 +153,10 @@ define([
{ parse: true }
);
emlGeoCoverage.set("south", "-90");
var errors = emlGeoCoverage.validate();
errors.south.should.equal("The bounding box cannot contain the North or South pole.");
const errors = emlGeoCoverage.validate();
const expectedMsg = emlGeoCoverage.errorMessages.containsPole;
errors.south.should.equal(expectedMsg);
});

});
});
});
});

0 comments on commit 53934d4

Please sign in to comment.