Skip to content

Commit

Permalink
validate json fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Aug 3, 2019
1 parent 49d9200 commit c062f39
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
26 changes: 24 additions & 2 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand Down
54 changes: 54 additions & 0 deletions spec/models/doi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c062f39

Please sign in to comment.