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

Add email validation to EML Party model #2556

Merged
merged 1 commit into from
Oct 21, 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
13 changes: 12 additions & 1 deletion src/js/models/metadata/eml211/EMLParty.js
robyngit marked this conversation as resolved.
Show resolved Hide resolved
robyngit marked this conversation as resolved.
Show resolved Hide resolved
robyngit marked this conversation as resolved.
Show resolved Hide resolved
robyngit marked this conversation as resolved.
Show resolved Hide resolved
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"
robyngit marked this conversation as resolved.
Show resolved Hide resolved
? this.get("roles")
: [this.get("type")];
for (role of roles) {
for (let role of roles) {
robyngit marked this conversation as resolved.
Show resolved Hide resolved
let requiredFields = MetacatUI.appModel.get(
robyngit marked this conversation as resolved.
Show resolved Hide resolved
"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;
robyngit marked this conversation as resolved.
Show resolved Hide resolved
},

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;
});
});
});
});
Loading