Skip to content

Commit

Permalink
Merge pull request #2556 from NCEAS/bugfix-2554-email-validation
Browse files Browse the repository at this point in the history
Add email validation to EML Party model
  • Loading branch information
robyngit authored Oct 21, 2024
2 parents d7c744e + 718ff5b commit c109815
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/js/models/metadata/eml211/EMLParty.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ define(["jquery", "underscore", "backbone", "models/DataONEObject"], function (
this.get("type") == "associatedParty"
? this.get("roles")
: [this.get("type")];
for (role of roles) {
for (let role of roles) {
let requiredFields = MetacatUI.appModel.get(
"emlEditorRequiredFields_EMLParty",
)[role];
Expand All @@ -899,6 +899,17 @@ define(["jquery", "underscore", "backbone", "models/DataONEObject"], function (
});
}

// If there is an email address, validate it
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
const email = this.get("email");
if (email?.length) {
email.forEach((emailAddress) => {
if (!emailAddress.match(emailRegex)) {
errors.email = "Provide a valid email address.";
}
});
}

return Object.keys(errors)?.length ? errors : false;
},

Expand Down
3 changes: 2 additions & 1 deletion src/js/templates/metadata/EMLParty.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<div class="span4">
<div class="row-fluid">
<label class="hidden" for="<%=uniqueId%>-email">E-mail</label>
<input type="email" id="<%=uniqueId%>-email" name="E-mail" class="span12 tooltip-this" data-attribute="email" placeholder="Email" />
<input type="email" id="<%=uniqueId%>-email" name="E-mail" class="span12
tooltip-this" data-attribute="email" placeholder="Email" pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" />
</div>
<div class="row-fluid">
<label class="hidden" for="<%=uniqueId%>-website">Website</label>
Expand Down
17 changes: 15 additions & 2 deletions test/js/specs/unit/models/metadata/eml211/EMLParty.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
"use strict";

define([
"../../../../../../../../src/js/models/metadata/eml211/EMLParty",
], function (EMLParty) {
"/test/js/specs/shared/clean-state.js",
"models/metadata/eml211/EMLParty",
], function (cleanState, EMLParty) {
// Configure the Chai assertion library
var should = chai.should();
var expect = chai.expect;

describe("EMLParty Test Suite", function () {
const state = cleanState(() => {
const party = new EMLParty();
return { party };
}, beforeEach);

describe("Creating", function () {
it("should be created from the logged in user");
});
Expand Down Expand Up @@ -36,6 +44,11 @@ define([
it("can require an email");
it("can require a country");
it("can require a user id (ORCID)");

it("should require a valid email", function () {
state.party.set("email", ["not an email"]);
state.party.isValid().should.be.false;
});
});
});
});

0 comments on commit c109815

Please sign in to comment.