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

Feature/doi url validation fix #258

Merged
merged 3 commits into from
Nov 20, 2023
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
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);
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
Loading