Skip to content

Commit

Permalink
Merge pull request #893 from datacite/fabrica-improvements-12
Browse files Browse the repository at this point in the history
Adjusts validation of Doi language attribute to allow non-ISO 639-1 values
  • Loading branch information
codycooperross authored Jan 17, 2023
2 parents 221d82a + ae7631d commit d47f6b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
12 changes: 7 additions & 5 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1940,8 +1940,7 @@ def check_container
end

def check_language
entry = ISO_639.find_by_code(language) || ISO_639.find_by_english_name(language.upcase_first)
errors.add(:language, "Language #{language} not found.") if entry.blank?
errors.add(:language, "Language #{language} is in an invalid format.") if !language.match?(/^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$/)
end

# To be used for isolated clean up of errored individual DOIs
Expand Down Expand Up @@ -2218,9 +2217,12 @@ def update_agency
def update_language
lang = language.to_s.split("-").first
entry = ISO_639.find_by_code(lang) || ISO_639.find_by_english_name(lang.upcase_first)
self.language = if entry.present? && entry.alpha2.present?
entry.alpha2
end
self.language =
if entry.present? && entry.alpha2.present?
entry.alpha2
elsif language.match?(/^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$/)
language
end
end

def update_field_of_science
Expand Down
23 changes: 22 additions & 1 deletion spec/models/doi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,31 @@
expect(doi.language).to eq("fr")
end

it "error" do
it "human longer than 8 characters" do
doi.language = "Indonesian"
expect(doi.save).to be true
expect(doi.errors.details).to be_empty
expect(doi.language).to eq("id")
end

it "non-iso 639-1" do
doi.language = "hhh"
expect(doi.save).to be true
expect(doi.errors.details).to be_empty
expect(doi.language).to eq("hhh")
end

it "non-iso 639-1 with country code" do
doi.language = "prq-PE"
expect(doi.save).to be true
expect(doi.errors.details).to be_empty
expect(doi.language).to eq("prq-PE")
end

it "fails xs:language regex" do
doi.language = "Borgesian"
expect(doi.save).to be true
expect(doi.errors.details).to be_empty
expect(doi.language).to be_nil
end

Expand Down

0 comments on commit d47f6b7

Please sign in to comment.