Skip to content

Commit

Permalink
Merge pull request #280 from cioos-siooc/fix-doi-validation
Browse files Browse the repository at this point in the history
fix doi validation
  • Loading branch information
sorochak authored Dec 5, 2023
2 parents 9e8983a + 6eb9bf0 commit c867c3b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
27 changes: 16 additions & 11 deletions src/components/Tabs/IdentificationTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import RequiredMark from "../FormComponents/RequiredMark";
import SelectInput from "../FormComponents/SelectInput";
import licenses from "../../utils/licenses";
import recordToDataCite from "../../utils/recordToDataCite";
import { validateField, doiRegexp } from "../../utils/validate";
import { validateField, validateDOI } from "../../utils/validate";

import {
QuestionText,
Expand All @@ -44,9 +44,8 @@ const IdentificationTab = ({
const { createDraftDoi, updateDraftDoi, deleteDraftDoi } = useContext(UserContext);
const { language, region, userID } = useParams();
const regionInfo = regions[region];
const doiIsValid = Boolean(
!record.datasetIdentifier || doiRegexp.test(record.datasetIdentifier)
);
const doiIsValid =validateDOI(record.datasetIdentifier)

const languageUpperCase = language.toUpperCase();
const [doiGenerated, setDoiGenerated] = useState(false);
const [doiErrorFlag, setDoiErrorFlag] = useState(false);
Expand Down Expand Up @@ -89,13 +88,13 @@ const IdentificationTab = ({
})
.then(async (attributes) => {
// Update the record object with datasetIdentifier and doiCreationStatus
updateRecord("datasetIdentifier")(attributes.doi);
updateRecord("datasetIdentifier")(`https://doi.org/${attributes.doi}`);
updateRecord("doiCreationStatus")("draft");

// Create a new object with updated properties
const updatedRecord = {
...record,
datasetIdentifier: attributes.doi,
datasetIdentifier: `https://doi.org/${attributes.doi}`,
doiCreationStatus: "draft",
};

Expand Down Expand Up @@ -134,8 +133,11 @@ const IdentificationTab = ({
delete mappedDataCiteObject.data.type;
delete mappedDataCiteObject.data.attributes.prefix;

// Extract DOI from the full URL
const doi = record.datasetIdentifier.replace('https://doi.org/', '');

const dataObject = {
doi: record.datasetIdentifier,
doi,
data: mappedDataCiteObject,
}

Expand All @@ -161,7 +163,10 @@ const IdentificationTab = ({
setLoadingDoiDelete(true);

try {
deleteDraftDoi(record.datasetIdentifier)
// Extract DOI from the full URL
const doi = record.datasetIdentifier.replace('https://doi.org/', '');

deleteDraftDoi(doi)
.then((response) => response.data)
.then(async (statusCode) => {
if (statusCode === 204) {
Expand Down Expand Up @@ -732,7 +737,7 @@ const IdentificationTab = ({
Loading...
</>
) : (
"Generate Draft DOI"
"Generate DOI"
)}
</div>
</Button>
Expand All @@ -749,7 +754,7 @@ const IdentificationTab = ({
Loading...
</>
) : (
"Update Draft DOI"
"Update DOI"
)}
</div>
</Button>
Expand All @@ -766,7 +771,7 @@ const IdentificationTab = ({
Loading...
</>
) : (
"Delete Draft DOI"
"Delete DOI"
)}
</div>
</Button>
Expand Down
4 changes: 2 additions & 2 deletions src/utils/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function isValidHttpUrl(string) {

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

export const validateDOI = (val) => !val || (doiRegexp.test(val) && isValidHttpUrl(val));
const validateLatitude = (num) => num >= -90 && num <= 90;

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

0 comments on commit c867c3b

Please sign in to comment.