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

Remove type from schema in favor of better type check in contract

Oliver Günther56e53620cb58
diff --git a/app/contracts/user_preferences/base_contract.rb b/app/contracts/user_preferences/base_contract.rb
index fca042a87f4..e5b0c6e4b6f 100644
--- a/app/contracts/user_preferences/base_contract.rb
+++ b/app/contracts/user_preferences/base_contract.rb
@@ -48,6 +48,9 @@ module UserPreferences
validate :full_hour_reminder_time,
if: -> { model.daily_reminders.present? }
+ validate :workdays_are_iso,
+ if: -> { model.workdays.present? }
+
protected
def time_zone_correctness
@@ -68,5 +71,15 @@ module UserPreferences
errors.add :daily_reminders, :full_hour
end
end
+
+ def workdays_are_iso
+ unless model.workdays.all? { |workday| workday.is_a?(Integer) && (1..7).include?(workday) }
+ errors.add :workdays, :iso_workday
+ end
+
+ unless model.workdays.uniq.length == model.workdays.length
+ errors.add :workdays, :no_duplicates
+ end
+ end
end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 0c4680fb838..50011883b2a 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -762,6 +762,9 @@ en:
day_limit: "is too high as a maximum of 24 hours can be logged per date."
user_preference:
attributes:
+ workdays:
+ iso_workday: "can only contain numbers between 1-7."
+ no_duplicates: "must not contain any duplicates."
daily_reminders:
full_hour: "can only be configured to be delivered at a full hour."
wiki_page:
diff --git a/config/schemas/user_preferences.schema.json b/config/schemas/user_preferences.schema.json
index 4c4de6f51d7..1f82f2452ee 100644
--- a/config/schemas/user_preferences.schema.json
+++ b/config/schemas/user_preferences.schema.json
@@ -23,10 +23,7 @@
"type": "boolean"
},
"workdays": {
- "type": "array",
- "items": {
- "type": "number"
- }
+ "type": "array"
},
"daily_reminders": {
"$ref": "#/definitions/DailyReminders"
diff --git a/spec/contracts/user_preferences/update_contract_spec.rb b/spec/contracts/user_preferences/update_contract_spec.rb
index 36352dc39e0..91bc375bad1 100644
--- a/spec/contracts/user_preferences/update_contract_spec.rb
+++ b/spec/contracts/user_preferences/update_contract_spec.rb
@@ -236,4 +236,26 @@ describe UserPreferences::UpdateContract do
it_behaves_like 'contract is invalid', time_zone: :inclusion
end
+
+ describe 'workdays' do
+ context 'with duplicate entries' do
+ let(:settings) do
+ {
+ workdays: [1, 1]
+ }
+ end
+
+ it_behaves_like 'contract is invalid', workdays: :no_duplicates
+ end
+
+ context 'with non-iso entries' do
+ let(:settings) do
+ {
+ workdays: [nil, 'foo', :bar, 21345, 2.0]
+ }
+ end
+
+ it_behaves_like 'contract is invalid', workdays: :iso_workday
+ end
+ end
end