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

Add custom Doi and Orcid structs and use them to warn of parsing errors as user types #249

Merged
merged 22 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
50a6c44
Add diesel-derive-newtype crate, use to define Doi/Orcid wrapper type…
rhigman May 20, 2021
0bdcbf4
POC: convert String to Orcid on UpdateContributor and only update if …
rhigman May 21, 2021
fca60a3
Revert "POC: convert String to Orcid on UpdateContributor and only up…
rhigman May 21, 2021
68e06bc
[test, bugged] Use Orcid throughout and parse string on ChangeOrcid
rhigman May 21, 2021
8658624
Add string variable to track user-entered ORCID [still some bugs]
rhigman May 25, 2021
648869a
Replace parse error notifications with tooltip (invalid ORCIDs reset …
rhigman May 25, 2021
7f1d08a
Implement static button by duplicating and extending FormTextTooltip …
rhigman May 27, 2021
058cd68
Implement new structs, parsing, error tooltip and static button in al…
rhigman May 27, 2021
bfbac5c
Use diesel-derive-newtype to create Timestamp wrapper type allowing d…
rhigman Jun 2, 2021
dcfd968
Move Doi and Orcid out into model/mod.rs (no changes) for shareabilit…
rhigman Jun 2, 2021
b06ced2
Fix bug where saving on an invalid value overwrote stored version if …
rhigman Jun 2, 2021
f1b2210
Add migrations to improve database DOI/ORCID checks so that they matc…
rhigman Jun 2, 2021
cddb72b
Merge duplicated tooltip/static button util into FormTextInputExtended
rhigman Jun 3, 2021
d7d5fe3
Merge branch 'develop' into feature/233_doi_orcid_validation
rhigman Jun 16, 2021
989f438
Re-version new migrations as latest merge from develop has put us up …
rhigman Jun 16, 2021
4be0557
Simplify PureTextInputExtended using Yew optional attributes (newly a…
rhigman Jun 17, 2021
9f7d541
Make database doi/orcid constraints case-sensitive to prevent manual …
rhigman Jun 17, 2021
90646ce
Merge branch 'develop' into feature/233_doi_orcid_validation
rhigman Jun 17, 2021
69531de
Review markups: allow additional protocol/resource variants, improve …
rhigman Jun 18, 2021
514d60d
Move Doi/Orcid empty check to parsing, and ignore empty error in inte…
rhigman Jun 21, 2021
ba90b91
Merge branch 'develop' into feature/233_doi_orcid_validation
rhigman Jun 21, 2021
b6641b0
Fix build error due to app/api dependencies following merge
rhigman Jun 21, 2021
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
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions thoth-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ chrono = { version = "0.4", features = ["serde"] }
csv = { version = "1.1.6", optional = true }
diesel = { version = "1.4.0", features = ["postgres", "uuidv07", "chrono", "r2d2", "64-column-tables", "serde_json"], optional = true }
diesel-derive-enum = { version = "1.1.0", features = ["postgres"], optional = true }
diesel-derive-newtype = "0.1"
diesel_migrations = { version = "1.4.0", features = ["postgres"], optional = true }
dotenv = "0.9.0"
failure = "0.1.6"
Expand Down
16 changes: 16 additions & 0 deletions thoth-api/migrations/0.4.1/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Reinstate earlier versions of ORCID and DOI validation

ALTER TABLE contributor
DROP CONSTRAINT contributor_orcid_check,
ADD CONSTRAINT contributor_orcid_check
CHECK (orcid ~* '0000-000(1-[5-9]|2-[0-9]|3-[0-4])\d{3}-\d{3}[\dX]');

ALTER TABLE work
DROP CONSTRAINT work_doi_check,
ADD CONSTRAINT work_doi_check
CHECK (doi ~* 'https:\/\/doi.org\/10.\d{4,9}\/[-._\;\(\)\/:a-zA-Z0-9]+$');

ALTER TABLE funder
DROP CONSTRAINT funder_funder_doi_check,
ADD CONSTRAINT funder_funder_doi_check
CHECK (funder_doi ~* 'https:\/\/doi.org\/10.\d{4,9}\/[-._\;\(\)\/:a-zA-Z0-9]+$');
21 changes: 21 additions & 0 deletions thoth-api/migrations/0.4.1/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Improve validation of ORCID identifiers (include protocol/resource name, make case-sensitive)
-- Should be kept in line with Orcid::FromStr, although regex syntax differs slightly

ALTER TABLE contributor
DROP CONSTRAINT contributor_orcid_check,
ADD CONSTRAINT contributor_orcid_check
CHECK (orcid ~ '^https:\/\/orcid\.org\/0000-000(1-[5-9]|2-[0-9]|3-[0-4])\d{3}-\d{3}[\dX]$');

-- Improve validation of DOI identifiers (add line start marker, escape periods, make case-sensitive)
-- Should be kept in line with Orcid::FromStr, although regex syntax differs slightly
-- (e.g. `;()/` need to be escaped here but not in Orcid::FromStr)

ALTER TABLE work
DROP CONSTRAINT work_doi_check,
ADD CONSTRAINT work_doi_check
CHECK (doi ~ '^https:\/\/doi\.org\/10\.\d{4,9}\/[-._\;\(\)\/:a-zA-Z0-9]+$');

ALTER TABLE funder
DROP CONSTRAINT funder_funder_doi_check,
ADD CONSTRAINT funder_funder_doi_check
CHECK (funder_doi ~ '^https:\/\/doi\.org\/10\.\d{4,9}\/[-._\;\(\)\/:a-zA-Z0-9]+$');
15 changes: 7 additions & 8 deletions thoth-api/src/account/model.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use chrono::DateTime;
use chrono::Utc;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::errors::ThothError;
use crate::errors::ThothResult;
use crate::model::Timestamp;
#[cfg(feature = "backend")]
use crate::schema::account;
#[cfg(feature = "backend")]
Expand All @@ -22,8 +21,8 @@ pub struct Account {
pub is_superuser: bool,
pub is_bot: bool,
pub is_active: bool,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub created_at: Timestamp,
pub updated_at: Timestamp,
pub token: Option<String>,
}

Expand Down Expand Up @@ -54,8 +53,8 @@ pub struct PublisherAccount {
pub account_id: Uuid,
pub publisher_id: Uuid,
pub is_admin: bool,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub created_at: Timestamp,
pub updated_at: Timestamp,
}

#[cfg_attr(feature = "backend", derive(Insertable))]
Expand Down Expand Up @@ -99,8 +98,8 @@ pub struct AccountDetails {
pub surname: String,
pub email: String,
pub token: Option<String>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub created_at: Timestamp,
pub updated_at: Timestamp,
pub resource_access: AccountAccess,
}

Expand Down
30 changes: 5 additions & 25 deletions thoth-api/src/contribution/model.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use chrono::DateTime;
use chrono::Utc;
use serde::{Deserialize, Serialize};
use strum::Display;
use strum::EnumString;
use uuid::Uuid;

use crate::model::Timestamp;
#[cfg(feature = "backend")]
use crate::schema::contribution;
#[cfg(feature = "backend")]
Expand Down Expand Up @@ -54,7 +53,7 @@ pub enum ContributionField {
}

#[cfg_attr(feature = "backend", derive(Queryable))]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Contribution {
pub contribution_id: Uuid,
Expand All @@ -64,8 +63,8 @@ pub struct Contribution {
pub main_contribution: bool,
pub biography: Option<String>,
pub institution: Option<String>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub created_at: Timestamp,
pub updated_at: Timestamp,
pub first_name: Option<String>,
pub last_name: String,
pub full_name: String,
Expand Down Expand Up @@ -113,7 +112,7 @@ pub struct ContributionHistory {
pub contribution_id: Uuid,
pub account_id: Uuid,
pub data: serde_json::Value,
pub timestamp: DateTime<Utc>,
pub timestamp: Timestamp,
}

#[cfg_attr(
Expand All @@ -133,25 +132,6 @@ impl Default for ContributionType {
}
}

impl Default for Contribution {
fn default() -> Contribution {
Contribution {
contribution_id: Default::default(),
work_id: Default::default(),
contributor_id: Default::default(),
contribution_type: Default::default(),
main_contribution: Default::default(),
biography: None,
institution: None,
created_at: chrono::TimeZone::timestamp(&Utc, 0, 0),
updated_at: chrono::TimeZone::timestamp(&Utc, 0, 0),
first_name: None,
last_name: Default::default(),
full_name: Default::default(),
}
}
}

#[test]
fn test_contributiontype_default() {
let contributiontype: ContributionType = Default::default();
Expand Down
35 changes: 10 additions & 25 deletions thoth-api/src/contributor/model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use chrono::DateTime;
use chrono::Utc;
use serde::Deserialize;
use serde::Serialize;
use std::fmt;
Expand All @@ -8,6 +6,8 @@ use strum::EnumString;
use uuid::Uuid;

use crate::graphql::utils::Direction;
use crate::model::Orcid;
use crate::model::Timestamp;
#[cfg(feature = "backend")]
use crate::schema::contributor;
#[cfg(feature = "backend")]
Expand Down Expand Up @@ -35,17 +35,17 @@ pub enum ContributorField {
}

#[cfg_attr(feature = "backend", derive(Queryable))]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Contributor {
pub contributor_id: Uuid,
pub first_name: Option<String>,
pub last_name: String,
pub full_name: String,
pub orcid: Option<String>,
pub orcid: Option<Orcid>,
pub website: Option<String>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub created_at: Timestamp,
pub updated_at: Timestamp,
}

#[cfg_attr(
Expand All @@ -57,7 +57,7 @@ pub struct NewContributor {
pub first_name: Option<String>,
pub last_name: String,
pub full_name: String,
pub orcid: Option<String>,
pub orcid: Option<Orcid>,
pub website: Option<String>,
}

Expand All @@ -72,7 +72,7 @@ pub struct PatchContributor {
pub first_name: Option<String>,
pub last_name: String,
pub full_name: String,
pub orcid: Option<String>,
pub orcid: Option<Orcid>,
pub website: Option<String>,
}

Expand All @@ -82,7 +82,7 @@ pub struct ContributorHistory {
pub contributor_id: Uuid,
pub account_id: Uuid,
pub data: serde_json::Value,
pub timestamp: DateTime<Utc>,
pub timestamp: Timestamp,
}

#[cfg_attr(
Expand All @@ -101,7 +101,7 @@ pub struct NewContributorHistory {
derive(juniper::GraphQLInputObject),
graphql(description = "Field and order to use when sorting contributors list")
)]
#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct ContributorOrderBy {
pub field: ContributorField,
pub direction: Direction,
Expand All @@ -113,21 +113,6 @@ impl Default for ContributorField {
}
}

impl Default for Contributor {
fn default() -> Contributor {
Contributor {
contributor_id: Default::default(),
first_name: None,
last_name: "".to_string(),
full_name: "".to_string(),
orcid: None,
website: None,
created_at: chrono::TimeZone::timestamp(&Utc, 0, 0),
updated_at: chrono::TimeZone::timestamp(&Utc, 0, 0),
}
}
}

impl fmt::Display for Contributor {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(orcid) = &self.orcid {
Expand Down
14 changes: 14 additions & 0 deletions thoth-api/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ pub enum ThothError {
CsvError(String),
#[fail(display = "Could not generate {}: {}", _0, _1)]
IncompleteMetadataRecord(String, String),
#[fail(
display = "{} is not a validly formatted ORCID and will not be saved",
_0
)]
OrcidParseError(String),
#[fail(
display = "{} is not a validly formatted DOI and will not be saved",
_0
)]
DoiParseError(String),
#[fail(display = "Cannot parse ORCID: no value provided")]
OrcidEmptyError,
#[fail(display = "Cannot parse DOI: no value provided")]
DoiEmptyError,
}

impl juniper::IntoFieldError for ThothError {
Expand Down
Loading