Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Move the date format validation to the contract level

Dombi Attila2ac2c8889c91
diff --git a/app/contracts/project_life_cycle_steps/update_contract.rb b/app/contracts/project_life_cycle_steps/update_contract.rb
index 207b7e77176..f68d18552c0 100644
--- a/app/contracts/project_life_cycle_steps/update_contract.rb
+++ b/app/contracts/project_life_cycle_steps/update_contract.rb
@@ -33,6 +33,7 @@ module ProjectLifeCycleSteps
validate :validate_start_after_preceeding_phases
validate :validate_start_date_is_a_working_day
validate :validate_finish_date_is_a_working_day
+ validate :validate_date_format
delegate :project, to: :model
@@ -58,6 +59,15 @@ module ProjectLifeCycleSteps
end
end
+ def validate_date_format
+ %i[start_date finish_date].each do |attr|
+ raw_value = model.send("#{attr}_before_type_cast")
+ if raw_value.present? && raw_value.is_a?(String) && !raw_value.match?(/^\d{4}-\d{2}-\d{2}$/)
+ model.errors.add(attr, :invalid)
+ end
+ end
+ end
+
private
def start_after_preceding_phases?
diff --git a/app/models/project/phase.rb b/app/models/project/phase.rb
index 01df7adf283..7bb0fca3ce9 100644
--- a/app/models/project/phase.rb
+++ b/app/models/project/phase.rb
@@ -39,7 +39,6 @@ class Project::Phase < ApplicationRecord
through: :definition
validate :validate_date_range
- validate :validate_date_format
delegate :name,
:position,
@@ -79,15 +78,6 @@ class Project::Phase < ApplicationRecord
end
end
- def validate_date_format
- %i[start_date finish_date].each do |attr|
- raw_value = send("#{attr}_before_type_cast")
- if raw_value.present? && raw_value.is_a?(String) && !raw_value.match?(/^\d{4}-\d{2}-\d{2}$/)
- errors.add(attr, :invalid)
- end
- end
- end
-
def calculate_duration
return nil unless date_range_set?
diff --git a/spec/contracts/project_life_cycle_steps/update_contract_spec.rb b/spec/contracts/project_life_cycle_steps/update_contract_spec.rb
index 0651e47ac97..2e2c2fa7a19 100644
--- a/spec/contracts/project_life_cycle_steps/update_contract_spec.rb
+++ b/spec/contracts/project_life_cycle_steps/update_contract_spec.rb
@@ -220,6 +220,52 @@ RSpec.describe ProjectLifeCycleSteps::UpdateContract do
end
end
end
+
+ describe "date format validation" do
+ let(:phase) { build_stubbed(:project_phase) }
+ let(:start_date) { nil }
+ let(:finish_date) { nil }
+
+ before do
+ phase.start_date = start_date
+ phase.finish_date = finish_date
+ end
+
+ context "with correct YYYY-MM-DD format" do
+ let(:start_date) { "2025-06-17" }
+ let(:finish_date) { "2025-06-18" }
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "with extra characters in date" do
+ let(:start_date) { "2025-06-170" }
+ let(:finish_date) { "2025-06-18" }
+
+ it_behaves_like "contract is invalid", start_date: :invalid
+ end
+
+ context "with non-date string" do
+ let(:start_date) { "2025-06-17" }
+ let(:finish_date) { "not-a-date" }
+
+ it_behaves_like "contract is invalid", finish_date: :invalid
+ end
+
+ context "with wrong format (missing leading zero)" do
+ let(:start_date) { "2025-6-17" }
+ let(:finish_date) { "2025-06-18" }
+
+ it_behaves_like "contract is invalid", start_date: :invalid
+ end
+
+ context "when date is blank" do
+ let(:start_date) { "" }
+ let(:finish_date) { nil }
+
+ it_behaves_like "contract is valid"
+ end
+ end
end
end
diff --git a/spec/models/project/phase_spec.rb b/spec/models/project/phase_spec.rb
index 2559d4a87b0..c380dfe1d4e 100644
--- a/spec/models/project/phase_spec.rb
+++ b/spec/models/project/phase_spec.rb
@@ -86,38 +86,6 @@ RSpec.describe Project::Phase do
subject.start_date = subject.finish_date
expect(subject).to be_valid
end
-
- describe "date format validation" do
- it "is valid with correct YYYY-MM-DD format" do
- subject.start_date = "2025-06-17"
- subject.finish_date = "2025-06-18"
- expect(subject).to be_valid
- end
-
- it "is invalid with extra characters in date" do
- subject.start_date = "2025-06-170"
- expect(subject).not_to be_valid
- expect(subject.errors.symbols_for(:start_date)).to include(:invalid)
- end
-
- it "is invalid with non-date string" do
- subject.finish_date = "not-a-date"
- expect(subject).not_to be_valid
- expect(subject.errors.symbols_for(:finish_date)).to include(:invalid)
- end
-
- it "is invalid with wrong format (missing leading zero)" do
- subject.start_date = "2025-6-17"
- expect(subject).not_to be_valid
- expect(subject.errors.symbols_for(:start_date)).to include(:invalid)
- end
-
- it "is valid when date is blank" do
- subject.start_date = ""
- subject.finish_date = nil
- expect(subject).to be_valid
- end
- end
end
describe "duration calculation" do