From 718ff5bd15e1234536ae183bdbf8accde808f84c Mon Sep 17 00:00:00 2001 From: robyngit Date: Mon, 21 Oct 2024 09:35:00 -0400 Subject: [PATCH] Add email validation to EML Party model - Include unit tests for email validation in spec file - Also add extra pattern matching in the EML Party template Issue #2554 --- src/js/models/metadata/eml211/EMLParty.js | 13 ++++++++++++- src/js/templates/metadata/EMLParty.html | 3 ++- .../models/metadata/eml211/EMLParty.spec.js | 17 +++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/js/models/metadata/eml211/EMLParty.js b/src/js/models/metadata/eml211/EMLParty.js index 1722565a4..70ad47882 100644 --- a/src/js/models/metadata/eml211/EMLParty.js +++ b/src/js/models/metadata/eml211/EMLParty.js @@ -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]; @@ -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; }, diff --git a/src/js/templates/metadata/EMLParty.html b/src/js/templates/metadata/EMLParty.html index 318247e09..a545f3229 100644 --- a/src/js/templates/metadata/EMLParty.html +++ b/src/js/templates/metadata/EMLParty.html @@ -20,7 +20,8 @@
- +
diff --git a/test/js/specs/unit/models/metadata/eml211/EMLParty.spec.js b/test/js/specs/unit/models/metadata/eml211/EMLParty.spec.js index 9e9346124..861a541f2 100644 --- a/test/js/specs/unit/models/metadata/eml211/EMLParty.spec.js +++ b/test/js/specs/unit/models/metadata/eml211/EMLParty.spec.js @@ -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"); }); @@ -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; + }); }); }); });