Skip to content

Commit

Permalink
validating doi name only on new dois
Browse files Browse the repository at this point in the history
removes unpermitted characters from DOI names when making Metadata validation

fix datacite/bracco#189

when using `/dois/validate` DOI name is not being validated.

Not check the DOI format when you are validating a DOI that you want to update.
  • Loading branch information
kjgarza committed Apr 12, 2019
1 parent 46e7254 commit 2d16f71
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Doi < ActiveRecord::Base
# validates_presence_of :url, if: :is_registered_or_findable?

# from https://www.crossref.org/blog/dois-and-matching-regular-expressions/ but using uppercase
validates_format_of :doi, :with => /\A10\.\d{4,5}\/[-\._;()\/:a-zA-Z0-9\*~\$\=]+\z/, :on => :create
validates_format_of :doi, :with => /\A10\.\d{4,5}\/[-\._;()\/:a-zA-Z0-9\*~\$\=]+\z/, :on => :create, unless: :only_validate
validates_format_of :url, :with => /\A(ftp|http|https):\/\/[\S]+/ , if: :url?, message: "URL is not valid"
validates_uniqueness_of :doi, message: "This DOI has already been taken", unless: :only_validate
validates :last_landing_page_status, numericality: { only_integer: true }, if: :last_landing_page_status?
Expand Down
57 changes: 57 additions & 0 deletions spec/requests/dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,36 @@
end
end

context 'when doi has unpermitted characters' do
let(:xml) { Base64.strict_encode64(file_fixture('datacite.xml').read) }
let(:valid_attributes) do
{
"data" => {
"type" => "dois",
"attributes" => {
"doi" => "10.14454/107+03",
"url" => "http://www.bl.uk/pdf/patspec.pdf",
"xml" => xml,
"source" => "test",
"event" => "publish"
}
}
}
end

before { post '/dois', params: valid_attributes.to_json, headers: headers }

it 'returns validation error' do
expect(json.dig('errors')).to eq([{"source"=>"doi", "title"=>"Is invalid"}])
end


it 'returns status code 422' do
expect(response).to have_http_status(422)
end

end

context 'creators no xml' do
let(:creators) { [{ "name"=>"Ollomi, Benjamin" }, { "name"=>"Duran, Patrick" }] }
let(:valid_attributes) do
Expand Down Expand Up @@ -1600,6 +1630,33 @@
end
end

context 'validates but not DOIname' do
let(:xml) { ::Base64.strict_encode64(File.read(file_fixture('datacite.xml'))) }
let(:params) do
{
"data" => {
"type" => "dois",
"attributes" => {
"doi" => "10.14454/107+03",
"xml" => xml,
}
}
}
end

before { post '/dois/validate', params: params.to_json, headers: headers }

it 'validates a Doi' do
expect(json.dig('data', 'attributes', 'doi')).to eq("10.14454/107+03")
expect(json.dig('data', 'attributes', 'titles')).to eq([{"title"=>"Eating your own Dog Food"}])
expect(json.dig('data', 'attributes', 'dates')).to eq([{"date"=>"2016-12-20", "dateType"=>"Created"}, {"date"=>"2016-12-20", "dateType"=>"Issued"}, {"date"=>"2016-12-20", "dateType"=>"Updated"}])
end

it 'returns status code 200' do
expect(response).to have_http_status(200)
end
end

context 'validates schema 3' do
let(:xml) { ::Base64.strict_encode64(File.read(file_fixture('datacite_schema_3.xml'))) }
let(:params) do
Expand Down

0 comments on commit 2d16f71

Please sign in to comment.