Skip to content

Commit

Permalink
Robustify ORCiD validation
Browse files Browse the repository at this point in the history
Now handles
* http and https URIs
* trailing slashes
* identifiers with more than 19 characters

Also changes default form to be the HTTPS URI rather than the HTTP URI

Fixes samvera-labs/nurax-pre2023#105
  • Loading branch information
mjgiarlo committed Oct 6, 2017
1 parent b7fdd85 commit 476fe5e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
6 changes: 3 additions & 3 deletions app/models/concerns/hyrax/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ def normalize_orcid
# 1. validation has already flagged the ORCID as invalid
# 2. the orcid field is blank
# 3. the orcid is already in its normalized form
return if errors[:orcid].first.present? || orcid.blank? || orcid.starts_with?('http://orcid.org/')
bare_orcid = Hyrax::OrcidValidator.match(orcid).string
self.orcid = "http://orcid.org/#{bare_orcid}"
return if errors[:orcid].first.present? || orcid.blank? || orcid.starts_with?('https://orcid.org/')
bare_orcid = Hyrax::OrcidValidator.match(orcid)
self.orcid = "https://orcid.org/#{bare_orcid}"
end

# Format the json for select2 which requires just an id and a field called text.
Expand Down
7 changes: 6 additions & 1 deletion app/models/hyrax/orcid_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ def validate(record)
end

def self.match(string)
/\d{4}-\d{4}-\d{4}-\d{3}[\dX]/.match(string)
Regexp.new(orcid_regex).match(string) { |m| m[:orcid] }
end

def self.orcid_regex
'^(?<prefix>https?://orcid.org/)?(?<orcid>\d{4}-\d{4}-\d{4}-\d{3}[\dX])/?$'
end
private_class_method :orcid_regex
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
expect(u.facebook_handle).to eq 'face'
expect(u.googleplus_handle).to eq 'goo'
expect(u.linkedin_handle).to eq 'link'
expect(u.orcid).to eq 'http://orcid.org/0000-0000-1111-2222'
expect(u.orcid).to eq 'https://orcid.org/0000-0000-1111-2222'
end

it 'displays a flash when invalid ORCID is entered' do
Expand Down
25 changes: 20 additions & 5 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,36 @@
expect(user).to be_valid
expect(user.save).to be true
end
it 'saves when a valid ORCID URI is supplied' do
user.orcid = 'http://orcid.org/0000-0000-1111-2222'
it 'saves when a valid ORCID HTTP URI w/ trailing slash is supplied' do
user.orcid = 'http://orcid.org/0000-0000-1111-2222/'
expect(user).to be_valid
expect(user.save).to be true
end
it 'normalizes bare ORCIDs to URIs' do
it 'saves when a valid ORCID HTTPS URI is supplied' do
user.orcid = 'https://orcid.org/0000-0000-1111-2222'
expect(user).to be_valid
expect(user.save).to be true
end
it 'normalizes bare ORCIDs to HTTPS URIs' do
user.orcid = '0000-0000-1111-2222'
user.save
expect(user.orcid).to eq 'http://orcid.org/0000-0000-1111-2222'
expect(user.orcid).to eq 'https://orcid.org/0000-0000-1111-2222'
end
it 'normalizes HTTP ORCIDs to HTTPS URIs' do
user.orcid = 'http://orcid.org/0000-0000-1111-2222'
user.save
expect(user.orcid).to eq 'https://orcid.org/0000-0000-1111-2222'
end
it 'marks bad ORCIDs as invalid' do
it 'marks short ORCIDs as invalid' do
user.orcid = '000-000-111-222'
expect(user).not_to be_valid
expect(user.save).to be false
end
it 'marks long ORCIDs as invalid' do
user.orcid = '0000-0000-1111-222222'
expect(user).not_to be_valid
expect(user.save).to be false
end
end

describe "#to_param" do
Expand Down

0 comments on commit 476fe5e

Please sign in to comment.