Skip to content

Commit

Permalink
Merge pull request #258 from cioos-siooc/feature/doi-url-validation-fix
Browse files Browse the repository at this point in the history
Feature/doi url validation fix
  • Loading branch information
n-a-t-e authored Nov 20, 2023
2 parents 2e57c6d + e1bdc13 commit 2104d73
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/components/Tabs/IdentificationTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const IdentificationTab = ({

async function handleUpdateDraftDOI() {
setLoadingDoiUpdate(true);

try {
const mappedDataCiteObject = recordToDataCite(record, language, region);
delete mappedDataCiteObject.data.type;
Expand All @@ -141,7 +141,7 @@ const IdentificationTab = ({

const response = await updateDraftDoi( dataObject );
const statusCode = response.data.status;

if (statusCode === 200) {
setDoiUpdateFlag(true);
} else {
Expand Down Expand Up @@ -701,7 +701,7 @@ const IdentificationTab = ({
<En>What is the DOI for this dataset? Eg,</En>
<Fr>Quel est le DOI de ce jeu de données ? Par exemple,</Fr>
</I18n>{" "}
10.0000/0000
https://doi.org/10.0000/0000
{showGenerateDoi && (
<SupplementalText>
<I18n>
Expand Down Expand Up @@ -738,8 +738,8 @@ const IdentificationTab = ({
</Button>
)}
{showUpdateDoi && (
<Button
onClick={handleUpdateDraftDOI}
<Button
onClick={handleUpdateDraftDOI}
style={{ display: 'inline' }}
>
<div style={{ display: "flex", alignItems: "center" }}>
Expand All @@ -755,8 +755,8 @@ const IdentificationTab = ({
</Button>
)}
{showDeleteDoi && (
<Button
onClick={handleDeleteDOI}
<Button
onClick={handleDeleteDOI}
style={{ display: "inline" }}
>
<div style={{ display: "flex", alignItems: "center" }}>
Expand Down
15 changes: 13 additions & 2 deletions src/utils/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@ export const validateEmail = (email) => !email || validator.isEmail(email);
export const validateURL = (url) => !url || validator.isURL(url);

// See https://stackoverflow.com/a/48524047/7416701
export const doiRegexp = /^10.\d{4,9}\/[-._;()/:A-Z0-9]+$/i;
export const doiRegexp = /^(https:\/\/doi.org\/)?10\.\d{4,9}\/[-._;()/:A-Z0-9]+$/i;
function isValidHttpUrl(string) {
let url;

try {
url = new URL(string);
} catch (_) {
return false;
}

return url.protocol === "http:" || url.protocol === "https:";
}

const validateLatitude = (num) => num >= -90 && num <= 90;

Expand Down Expand Up @@ -70,7 +81,7 @@ const validators = {
},
},
datasetIdentifier: {
validation: (val) => !val || doiRegexp.test(val),
validation: (val) => !val || (doiRegexp.test(val) && isValidHttpUrl(val)),
optional: true,
tab: "dataID",
error: {
Expand Down

0 comments on commit 2104d73

Please sign in to comment.