• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / 4e4e59fc-9df4-4838-9fd4-6c7ea33cdb7c

07 Apr 2025 06:36PM UTC coverage: 1.283% (-94.6%) from 95.862%
4e4e59fc-9df4-4838-9fd4-6c7ea33cdb7c

Pull #1994

circleci

hectorcorrea
Switched to use the autocomplete that we aleady use for ROR. Integrated it with the existing logic for creators
Pull Request #1994: Started adding auto complete to contributors

0 of 46 new or added lines in 2 files covered. (0.0%)

4806 existing lines in 74 files now uncovered.

65 of 5065 relevant lines covered (1.28%)

0.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/app/services/work_validator.rb
1
# frozen_string_literal: true
UNCOV
2
class WorkValidator
×
UNCOV
3
  include Rails.application.routes.url_helpers
×
UNCOV
4
  attr_reader :work
×
5

UNCOV
6
  delegate :errors, :metadata, :resource, :ark, :doi, :user_entered_doi, :id, :group,
×
UNCOV
7
           :pre_curation_uploads, :post_curation_uploads, to: :work
×
8

UNCOV
9
  def initialize(work)
×
UNCOV
10
    @work = work
×
UNCOV
11
  end
×
12

UNCOV
13
  def valid?
×
UNCOV
14
    if work.none?
×
UNCOV
15
      validate_ids
×
UNCOV
16
    elsif work.draft?
×
UNCOV
17
      valid_to_draft
×
UNCOV
18
    else
×
UNCOV
19
      valid_to_submit
×
UNCOV
20
    end
×
UNCOV
21
  end
×
22

UNCOV
23
  def valid_to_draft(*)
×
UNCOV
24
    errors.add(:base, "Must provide a title") if resource.main_title.blank?
×
UNCOV
25
    validate_ark
×
UNCOV
26
    validate_creators
×
UNCOV
27
    validate_related_objects if resource.present? && !resource.related_objects.empty?
×
UNCOV
28
    errors.count == 0
×
UNCOV
29
  end
×
30

UNCOV
31
  def valid_to_complete(*args)
×
UNCOV
32
    validate_files
×
UNCOV
33
    valid_to_submit(args)
×
UNCOV
34
  end
×
35

UNCOV
36
  def valid_to_submit(*args)
×
UNCOV
37
    valid_to_draft(args)
×
UNCOV
38
    validate_metadata
×
UNCOV
39
    if errors.count == 0
×
UNCOV
40
      valid_datacite? # test if the datacite update will be valid
×
UNCOV
41
    end
×
UNCOV
42
    errors.count == 0
×
UNCOV
43
  end
×
44

UNCOV
45
  def valid_to_approve(user)
×
UNCOV
46
    if resource.doi.blank?
×
UNCOV
47
      errors.add :base, "DOI must be present for a work to be approved"
×
UNCOV
48
    end
×
UNCOV
49
    valid_to_submit(user)
×
UNCOV
50
    unless user.has_role? :group_admin, group
×
UNCOV
51
      errors.add :base, "Unauthorized to Approve"
×
UNCOV
52
    end
×
UNCOV
53
    validate_files
×
UNCOV
54
    if pre_curation_uploads.empty? && post_curation_uploads.empty?
×
UNCOV
55
      errors.add(:base, "You must include at least one file. <a href='#{work_file_upload_path(work)}'>Please upload one</a>")
×
UNCOV
56
    end
×
UNCOV
57
    errors.count == 0
×
UNCOV
58
  end
×
59

UNCOV
60
  def valid_datacite?
×
UNCOV
61
    datacite_serialization = resource.datacite_serialization
×
UNCOV
62
    datacite_serialization.valid?
×
UNCOV
63
    datacite_serialization.errors.each { |error| errors.add(:base, error) }
×
UNCOV
64
    errors.count == 0
×
UNCOV
65
  rescue ArgumentError => error
×
UNCOV
66
    argument_path = error.backtrace_locations.first.path
×
UNCOV
67
    argument_file = argument_path.split("/").last
×
UNCOV
68
    argument_name = argument_file.split(".").first
×
UNCOV
69
    errors.add(:base, "#{argument_name.titleize}: #{error.message}")
×
UNCOV
70
    false
×
UNCOV
71
  end
×
72

UNCOV
73
  private
×
74

UNCOV
75
    def validate_ark
×
UNCOV
76
      return if ark.blank?
×
UNCOV
77
      return false unless unique_ark
×
UNCOV
78
      first_save = id.blank?
×
UNCOV
79
      changed_value = metadata["ark"] != ark
×
UNCOV
80
      if first_save || changed_value
×
UNCOV
81
        errors.add(:base, "Invalid ARK provided for the Work: #{ark}") unless Ark.valid?(ark)
×
UNCOV
82
      end
×
UNCOV
83
    end
×
84

UNCOV
85
    def validate_related_objects
×
UNCOV
86
      return if resource.related_objects.empty?
×
UNCOV
87
      invalid = resource.related_objects.reject(&:valid?)
×
UNCOV
88
      if invalid.count.positive?
×
UNCOV
89
        related_object_errors = "Related Objects are invalid: "
×
UNCOV
90
        prev_errors = errors.to_a
×
UNCOV
91
        prev_related_object_errors = prev_errors.map { |e| e.include?(related_object_errors) }.reduce(:|)
×
92

UNCOV
93
        errors.add(:base, "#{related_object_errors}#{invalid.map(&:errors).join(', ')}") unless prev_related_object_errors
×
UNCOV
94
      end
×
UNCOV
95
    end
×
96

UNCOV
97
    def validate_creators
×
UNCOV
98
      if resource.creators.count == 0
×
UNCOV
99
        errors.add(:base, "Must provide at least one Creator")
×
UNCOV
100
      else
×
UNCOV
101
        resource.creators.each do |creator|
×
UNCOV
102
          if creator.orcid.present? && Orcid.invalid?(creator.orcid)
×
UNCOV
103
            errors.add(:base, "ORCID for creator #{creator.value} is not in format 0000-0000-0000-0000")
×
UNCOV
104
          end
×
UNCOV
105
        end
×
UNCOV
106
      end
×
UNCOV
107
    end
×
108

UNCOV
109
    def validate_required_metadata
×
UNCOV
110
      return if metadata.blank?
×
UNCOV
111
      errors.add(:base, "Must provide a title") if resource.main_title.blank?
×
UNCOV
112
      validate_creators
×
UNCOV
113
    end
×
114

UNCOV
115
    def validate_doi
×
UNCOV
116
      return true unless user_entered_doi
×
UNCOV
117
      return false unless unique_doi
×
UNCOV
118
      if /^10.\d{4,9}\/[-._;()\/:a-z0-9\-]+$/.match?(doi.downcase)
×
UNCOV
119
        response = Faraday.get("#{Rails.configuration.datacite.doi_url}#{doi}")
×
UNCOV
120
        errors.add(:base, "Invalid DOI: can not verify it's authenticity") unless response.success? || response.status == 302
×
UNCOV
121
      else
×
UNCOV
122
        errors.add(:base, "Invalid DOI: does not match format")
×
UNCOV
123
      end
×
UNCOV
124
      errors.count == 0
×
UNCOV
125
    end
×
126

UNCOV
127
    def unique_ark
×
UNCOV
128
      return true if ark.blank?
×
UNCOV
129
      other_record = Work.find_by_ark(ark)
×
UNCOV
130
      return true if other_record == work
×
UNCOV
131
      errors.add(:base, "Invalid ARK: It has already been applied to another work #{other_record.id}")
×
UNCOV
132
      false
×
UNCOV
133
    rescue ActiveRecord::RecordNotFound
×
UNCOV
134
      true
×
UNCOV
135
    end
×
136

UNCOV
137
    def validate_ids
×
UNCOV
138
      validate_doi
×
UNCOV
139
      unique_ark
×
UNCOV
140
    end
×
141

UNCOV
142
    def unique_doi
×
UNCOV
143
      other_record = Work.find_by_doi(doi)
×
UNCOV
144
      return true if other_record == work
×
UNCOV
145
      errors.add(:base, "Invalid DOI: It has already been applied to another work #{other_record.id}")
×
UNCOV
146
      false
×
UNCOV
147
    rescue ActiveRecord::RecordNotFound
×
UNCOV
148
      true
×
UNCOV
149
    end
×
150

UNCOV
151
    def validate_metadata
×
UNCOV
152
      return if metadata.blank?
×
UNCOV
153
      validate_required_metadata
×
UNCOV
154
      errors.add(:base, "Must provide a description") if resource.description.blank?
×
UNCOV
155
      errors.add(:base, "Must indicate the Publisher") if resource.publisher.blank?
×
UNCOV
156
      errors.add(:base, "Must indicate the Publication Year") if resource.publication_year.blank?
×
UNCOV
157
      errors.add(:base, "Must indicate at least one Rights statement") if resource.rights_many.count == 0
×
UNCOV
158
      errors.add(:base, "Must provide a Version number") if resource.version_number.blank?
×
UNCOV
159
      validate_related_objects
×
UNCOV
160
    end
×
161

UNCOV
162
    def validate_files
×
UNCOV
163
      return if @work.resource.migrated
×
UNCOV
164
      readme = Readme.new(work, nil)
×
165

166
      # when files are not uploaded
UNCOV
167
      errors.add(:base, "You must include a README. <a href='#{work_readme_select_path(work)}'>Please upload one</a>") if readme.blank?
×
UNCOV
168
      if !work.files_location_upload?
×
UNCOV
169
      elsif work.uploads.length < 2
×
170

171
        # files_location_upload?
172
        # 1 readme and 1 file
173
        # 2 readme files and 0 files
UNCOV
174
        errors.add(:base,
×
UNCOV
175
        "You must include one or more files if you are uploading files from your local environment. <a href='#{work_file_upload_path(work)}'>Please resubmit after uploading the file(s)</a>")
×
UNCOV
176
      end
×
UNCOV
177
    end
×
UNCOV
178
end
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc