From 38bccacc4b05e0858f33118f85bdaaf3d0bb03f6 Mon Sep 17 00:00:00 2001 From: carolyncole <1599081+carolyncole@users.noreply.github.com> Date: Mon, 6 May 2024 11:42:43 -0400 Subject: [PATCH] Moving all project validation to the ProjectValidator (#680) --- app/models/project_mediaflux.rb | 2 +- app/models/project_metadata.rb | 27 --------------------------- app/models/project_validator.rb | 26 ++++++++++++++++++++++++-- spec/models/project_mediaflux_spec.rb | 5 +++-- spec/models/project_validator_spec.rb | 6 +++--- 5 files changed, 31 insertions(+), 35 deletions(-) diff --git a/app/models/project_mediaflux.rb b/app/models/project_mediaflux.rb index edb88040..f7b85a95 100644 --- a/app/models/project_mediaflux.rb +++ b/app/models/project_mediaflux.rb @@ -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") diff --git a/app/models/project_metadata.rb b/app/models/project_metadata.rb index fb52a3dc..aca8a157 100644 --- a/app/models/project_metadata.rb +++ b/app/models/project_metadata.rb @@ -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 @@ -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], @@ -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 diff --git a/app/models/project_validator.rb b/app/models/project_validator.rb index 0b336592..8a5846bc 100644 --- a/app/models/project_validator.rb +++ b/app/models/project_validator.rb @@ -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 @@ -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 \ No newline at end of file diff --git a/spec/models/project_mediaflux_spec.rb b/spec/models/project_mediaflux_spec.rb index c07ac50c..78a617a1 100644 --- a/spec/models/project_mediaflux_spec.rb +++ b/spec/models/project_mediaflux_spec.rb @@ -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 diff --git a/spec/models/project_validator_spec.rb b/spec/models/project_validator_spec.rb index 488ff415..d22241b3 100644 --- a/spec/models/project_validator_spec.rb +++ b/spec/models/project_validator_spec.rb @@ -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 @@ -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 @@ -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