diff --git a/app/models/doi.rb b/app/models/doi.rb index 53c358d00..6e8988335 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -83,7 +83,10 @@ 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? + validate :check_dates, if: :dates? + validate :check_rights_list, if: :rights_list? + validate :check_descriptions, if: :descriptions? + validate :check_subjects, if: :subjects? after_commit :update_url, on: [:create, :update] after_commit :update_media, on: [:create, :update] @@ -771,7 +774,26 @@ def event=(value) def check_dates Array.wrap(dates).each do |d| - errors.add(:dates, "Date #{d["date"]} is not a valid date in ISO8601 format.") unless Date.edtf(d["date"]).present? + errors.add(:dates, "Date #{d} should be an object instead of a string.") unless d.is_a?(Hash) + #errors.add(:dates, "Date #{d["date"]} is not a valid date in ISO8601 format.") unless Date.edtf(d["date"]).present? + end + end + + def check_rights_list + Array.wrap(rights_list).each do |r| + errors.add(:rights_list, "Rights '#{r}' should be an object instead of a string.") unless r.is_a?(Hash) + end + end + + def check_descriptions + Array.wrap(descriptions).each do |d| + errors.add(:descriptions, "Description '#{d}' should be an object instead of a string.") unless d.is_a?(Hash) + end + end + + def check_subjects + Array.wrap(subjects).each do |s| + errors.add(:subjects, "Subject '#{s}' should be an object instead of a string.") unless s.is_a?(Hash) end end diff --git a/spec/models/doi_spec.rb b/spec/models/doi_spec.rb index d96f872f9..6c1a0ed6c 100644 --- a/spec/models/doi_spec.rb +++ b/spec/models/doi_spec.rb @@ -216,6 +216,54 @@ # end end + describe "descriptions" do + let(:doi) { build(:doi) } + + it "hash" do + doi.descriptions = [{ "description" => "This is a description." }] + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + end + + it "string" do + doi.descriptions = ["This is a description."] + expect(doi.save).to be false + expect(doi.errors.details).to eq(:descriptions=>[{:error=>"Description 'This is a description.' should be an object instead of a string."}]) + end + end + + describe "rights_list" do + let(:doi) { build(:doi) } + + it "hash" do + doi.rights_list = [{ "rights" => "Creative Commons Attribution 4.0 International license (CC BY 4.0)" }] + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + end + + it "string" do + doi.rights_list = ["Creative Commons Attribution 4.0 International license (CC BY 4.0)"] + expect(doi.save).to be false + expect(doi.errors.details).to eq(:rights_list => [{:error=>"Rights 'Creative Commons Attribution 4.0 International license (CC BY 4.0)' should be an object instead of a string."}]) + end + end + + describe "subjects" do + let(:doi) { build(:doi) } + + it "hash" do + doi.subjects = [{ "subject" => "Tree" }] + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + end + + it "string" do + doi.subjects = ["Tree"] + expect(doi.save).to be false + expect(doi.errors.details).to eq(:subjects=>[{:error=>"Subject 'Tree' should be an object instead of a string."}]) + end + end + describe "dates" do let(:doi) { build(:doi) } @@ -255,6 +303,12 @@ expect(doi.errors.details).to be_empty end + it "string" do + doi.dates = ["2019-08-01"] + expect(doi.save).to be false + expect(doi.errors.details).to eq(:dates=>[{:error=>"Date 2019-08-01 should be an object instead of a string."}]) + end + # it "invalid" do # doi.dates = [{ "date" => "08/01/2019" }] # expect(doi.save).to be false