Skip to content

Commit

Permalink
Moving all project validation to the ProjectValidator (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
carolyncole authored May 6, 2024
1 parent 99e31d3 commit 38bccac
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/models/project_mediaflux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def self.create!(project:, session_id:, xml_namespace: nil)
if project.valid?
raise response_error[:message] # something strange went wrong
else
raise TigerData::MissingMetadata.missing_metadata(schema_version: ::TigerdataSchema::SCHEMA_VERSION, errors: project.metadata_model.errors)
raise TigerData::MissingMetadata.missing_metadata(schema_version: ::TigerdataSchema::SCHEMA_VERSION, errors: project.errors)
end
else
raise(StandardError,"An error has occured during project creation, not related to namespace creation or collection creation")
Expand Down
27 changes: 0 additions & 27 deletions app/models/project_metadata.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
# frozen_string_literal: true
class ProjectMetadata
include ActiveModel::API

class Validator < ActiveModel::Validator
def validate(record)
if record.required_attributes.values.include?(nil)
record.required_keys.each do |attr|
value = record.required_attributes[attr]
record.errors.add(attr, "Missing metadata value for #{attr}") if value.nil? && record.project.metadata_json.include?(attr)
end
end
end
end
validates_with Validator

attr_reader :project, :current_user, :params
def initialize(project:, current_user: nil)
@project = project
Expand Down Expand Up @@ -71,15 +57,6 @@ def create(params:)
project.metadata["project_id"]
end

def required_keys
tableized = required_field_labels.map { |v| v.parameterize.underscore }
tableized
end

def required_attributes
project.metadata_json.select { |k, _v| required_keys.include?(k) }
end

def attributes
{
data_sponsor: params[:data_sponsor],
Expand Down Expand Up @@ -146,10 +123,6 @@ def form_metadata
data.merge(timestamps)
end

def required_field_labels
TigerdataSchema.required_project_schema_fields.pluck(:label)
end

def draft_doi
puldatacite = PULDatacite.new
project.metadata_json["project_id"] = puldatacite.draft_doi
Expand Down
26 changes: 24 additions & 2 deletions app/models/project_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ def validate(project)
project.metadata[:data_user_read_write]&.each { |read_write| validate_role(project:, netid: read_write, role: "Data User Read Write")}

# validate all required fields
if !project.metadata_model.valid?
project.errors.add :base, "Invalid Project Metadata it does not match the schema #{TigerdataSchema::SCHEMA_VERSION}\n #{project.metadata_model.errors.to_a.join(", ")}"
required_metadata_field_errors = []
required_metadata = required_attributes(project:)
if required_metadata.values.include?(nil)
required_keys.each do |attr|
value = required_metadata[attr]
required_metadata_field_errors << "Missing metadata value for #{attr}" if value.nil? && project.metadata_json.include?(attr)
end
end
if required_metadata_field_errors.count > 0
project.errors.add :base, "Invalid Project Metadata it does not match the schema #{TigerdataSchema::SCHEMA_VERSION}\n #{required_metadata_field_errors.join(", ")}"
end
end

Expand All @@ -23,4 +31,18 @@ def validate_role(project:, netid:, role:)
project.errors.add :base, "Invalid netid: #{netid} for role #{role}"
end
end

def required_field_labels
TigerdataSchema.required_project_schema_fields.pluck(:label)
end

def required_keys
tableized = required_field_labels.map { |v| v.parameterize.underscore }
tableized
end

def required_attributes(project:)
project.metadata_json.select { |k, _v| required_keys.include?(k) }
end

end
5 changes: 3 additions & 2 deletions spec/models/project_mediaflux_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@
}.to raise_error do |error|
expect(error).to be_a(TigerData::MetadataError)
expect(error.message).to include("Project creation failed with metadata schema version 0.6.1 due to the missing fields:")
expect(incomplete_project.metadata_model.errors.attribute_names.size).to eq 1
expect(incomplete_project.metadata_model.errors.attribute_names[0].to_s).to eq "project_id"
expect(incomplete_project.errors.attribute_names.size).to eq 1
expect(incomplete_project.errors.attribute_names[0].to_s).to eq "base"
expect(incomplete_project.errors.messages[:base]).to eq ["Invalid Project Metadata it does not match the schema 0.6.1\n Missing metadata value for project_id"]
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/models/project_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
project = FactoryBot.build(:project, data_sponsor: nil, data_manager: sponsor.uid, project_id: "abc123", schema_version: nil)
expect(project).not_to be_valid
expect(project.errors.map(&:full_message)).to eq(["Mising netid for role Data Sponsor",
"Invalid Project Metadata it does not match the schema 0.6.1\n Data sponsor Missing metadata value for data_sponsor"])
"Invalid Project Metadata it does not match the schema 0.6.1\n Missing metadata value for data_sponsor"])
end
end

Expand All @@ -27,7 +27,7 @@
project = FactoryBot.build(:project, data_sponsor: nil, data_manager: sponsor.uid, project_id: "abc123")
expect(project).not_to be_valid
expect(project.errors.map(&:full_message)).to eq(["Mising netid for role Data Sponsor",
"Invalid Project Metadata it does not match the schema 0.6.1\n Data sponsor Missing metadata value for data_sponsor"])
"Invalid Project Metadata it does not match the schema 0.6.1\n Missing metadata value for data_sponsor"])
end
end

Expand All @@ -37,7 +37,7 @@
project = FactoryBot.build(:project, data_sponsor: sponsor.uid, data_manager: nil)
expect(project).not_to be_valid
expect(project.errors.map(&:full_message)).to eq(["Mising netid for role Data Manager",
"Invalid Project Metadata it does not match the schema 0.6.1\n Data manager Missing metadata value for data_manager"])
"Invalid Project Metadata it does not match the schema 0.6.1\n Missing metadata value for data_manager"])
end
end

Expand Down

0 comments on commit 38bccac

Please sign in to comment.