Skip to content

Commit

Permalink
Tweak ORCID validation and move into util
Browse files Browse the repository at this point in the history
  • Loading branch information
katamartin committed Jan 8, 2025
1 parent 735a28c commit 6d1a733
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
3 changes: 2 additions & 1 deletion app/(userSection)/account/account-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Button, Column, Field, Form, Row } from '../../../components'
import { useForm } from '../../../hooks/use-form'
import { updateAccount } from '../../../actions'
import { useLoading } from '../../../components/layouts/paneled-page'
import { isValidOrcid } from '../../../utils/data'

type FormData = {
email: string
Expand Down Expand Up @@ -51,7 +52,7 @@ const validateForm = ({
result.last_name = 'You must provide a last name.'
}

if (orcid && !orcid.match(/^\d{4}-\d{4}-\d{4}-\d{4}$/)) {
if (orcid && !isValidOrcid(orcid)) {
result.orcid =
'Please provide a valid ORCID identifier of the format 0000-0000-0000-0000.'
}
Expand Down
3 changes: 2 additions & 1 deletion app/(userSection)/register/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { useForm } from '../../../hooks/use-form'
import { useLoading } from '../../../components/layouts/paneled-page'
import { registerAccount, verifyHCaptcha } from '../../../actions'
import { isValidOrcid } from '../../../utils/data'

type FormData = {
email: string
Expand Down Expand Up @@ -75,7 +76,7 @@ const validateForm = ({
result.last_name = 'You must provide a last name.'
}

if (orcid && !orcid.match(/^\d{4}-\d{4}-\d{4}-\d{4}$/)) {
if (orcid && !isValidOrcid(orcid)) {
result.orcid =
'Please provide a valid ORCID identifier of the format 0000-0000-0000-0000.'
}
Expand Down
10 changes: 3 additions & 7 deletions app/submit/(authed)/authors/author-search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,17 @@ import { usePreprint } from '../../preprint-context'
import { searchAuthor, updatePreprint } from '../../../../actions'
import useLoadingText from '../../../../hooks/use-loading-text'
import useTracking from '../../../../hooks/use-tracking'
import { isValidOrcid } from '../../../../utils/data'

const isEmail = (value: string) => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
return emailRegex.test(value)
}

const isOrcid = (value: string) => {
const orcidRegex = /^\d{4}-\d{4}-\d{4}-\d{4}$/
return orcidRegex.test(value)
}

const validateAuthorSearch = (value: string) => {
if (isEmail(value)) {
return 'email'
} else if (isOrcid(value)) {
} else if (isValidOrcid(value)) {
return 'orcid'
} else {
return 'invalid'
Expand Down Expand Up @@ -50,7 +46,7 @@ const AuthorSearch = () => {
}

try {
const searchResults = await searchAuthor(value)
const searchResults = await searchAuthor(value.toUpperCase())
const author = searchResults.results[0]
if (author && searchResults.results.length === 1) {
const updatedPreprint = await updatePreprint(preprint, {
Expand Down
5 changes: 5 additions & 0 deletions utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,8 @@ export const isPreprintEmpty = (preprint: Preprint) => {
return false
})
}

export const isValidOrcid = (orcid: string) => {
const orcidRegex = /^\d{4}-\d{4}-\d{4}-\d{3}(\d|X)$/i
return orcidRegex.test(orcid)
}

0 comments on commit 6d1a733

Please sign in to comment.