Skip to content

Commit

Permalink
added date validation, using edft gem. #326
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Aug 1, 2019
1 parent 26b5362 commit c20ea80
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Doi < ActiveRecord::Base
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?
validates :xml, presence: true, xml_schema: true, if: Proc.new { |doi| doi.validatable? }
validate :check_dates, if: :dates?

after_commit :update_url, on: [:create, :update]
after_commit :update_media, on: [:create, :update]
Expand Down Expand Up @@ -743,6 +744,12 @@ def event=(value)
self.send(value) if %w(register publish hide show).include?(value)
end

def check_dates
Array.wrap(dates).each do |d|
errors.add(:dates, "Date #{d["date"]} is not in a supported format.") unless Date.edtf(d["date"]).present?
end
end

# to be used after DOIs were transferred to another DOI RA
def self.delete_dois_by_prefix(prefix, options={})
logger = Logger.new(STDOUT)
Expand Down
40 changes: 40 additions & 0 deletions spec/models/doi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,46 @@
# end
end

describe "dates" do
let(:doi) { build(:doi) }

it "full date" do
doi.dates = [{ "date" => "2019-08-01" }]
expect(doi.save).to be true
expect(doi.errors.details).to be_empty
end

it "year-month" do
doi.dates = [{ "date" => "2019-08" }]
expect(doi.save).to be true
expect(doi.errors.details).to be_empty
end

it "year" do
doi.dates = [{ "date" => "2019" }]
expect(doi.save).to be true
expect(doi.errors.details).to be_empty
end

it "date range" do
doi.dates = [{ "date" => "2019-07-31/2019-08-01" }]
expect(doi.save).to be true
expect(doi.errors.details).to be_empty
end

it "date range years" do
doi.dates = [{ "date" => "2018/2019" }]
expect(doi.save).to be true
expect(doi.errors.details).to be_empty
end

it "invalid" do
doi.dates = [{ "date" => "08/01/2019" }]
expect(doi.save).to be false
expect(doi.errors.details).to eq(:dates=>[{:error=>"Date 08/01/2019 is not in a supported format."}])
end
end

describe "metadata" do
subject { create(:doi) }

Expand Down

0 comments on commit c20ea80

Please sign in to comment.