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

Validate daily reminder preferences with a JSON schema

Oliver Günther1e4d67b3eb36
diff --git a/Gemfile b/Gemfile
index 6aa3531587c..aa45ab6a5a0 100644
--- a/Gemfile
+++ b/Gemfile
@@ -98,6 +98,7 @@ gem 'svg-graph', '~> 2.2.0'
gem 'date_validator', '~> 0.12.0'
gem 'email_validator', '~> 2.2.3'
gem 'ruby-duration', '~> 3.2.0'
+gem 'activerecord_json_validator', '~> 2.0.0'
# provide compatible filesystem information for available storage
gem 'sys-filesystem', '~> 1.4.0', require: false
diff --git a/Gemfile.lock b/Gemfile.lock
index 204bf4a62ac..4fa9f088c83 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -246,6 +246,9 @@ GEM
multi_json (~> 1.11, >= 1.11.2)
rack (>= 2.0.8, < 3)
railties (>= 5.2.4.1)
+ activerecord_json_validator (2.0.1)
+ activerecord (>= 4.2.0, < 7)
+ json_schemer (~> 0.2.18)
activestorage (6.1.4.1)
actionpack (= 6.1.4.1)
activejob (= 6.1.4.1)
@@ -428,6 +431,8 @@ GEM
dry-core (~> 0.5, >= 0.5)
dry-inflector (~> 0.1, >= 0.1.2)
dry-logic (~> 1.0, >= 1.0.2)
+ ecma-re-validator (0.3.0)
+ regexp_parser (~> 2.0)
em-http-request (1.1.7)
addressable (>= 2.3.4)
cookiejar (!= 0.3.1)
@@ -521,6 +526,7 @@ GEM
rack (>= 1.3.0)
rack-accept
gravatar_image_tag (1.2.0)
+ hana (1.3.7)
hashdiff (1.0.1)
hashery (2.1.2)
hashie (3.6.0)
@@ -549,6 +555,11 @@ GEM
activesupport (>= 4.2)
aes_key_wrap
bindata
+ json_schemer (0.2.18)
+ ecma-re-validator (~> 0.3)
+ hana (~> 1.3)
+ regexp_parser (~> 2.0)
+ uri_template (~> 0.7)
json_spec (1.1.5)
multi_json (~> 1.0)
rspec (>= 2.0, < 4.0)
@@ -921,6 +932,7 @@ GEM
unicorn-worker-killer (0.4.5)
get_process_mem (~> 0)
unicorn (>= 4, < 7)
+ uri_template (0.7.0)
validate_email (0.1.6)
activemodel (>= 3.0)
mail (>= 2.2.5)
@@ -961,6 +973,7 @@ DEPENDENCIES
activerecord-import (~> 1.2.0)
activerecord-nulldb-adapter (~> 0.7.0)
activerecord-session_store (~> 2.0.0)
+ activerecord_json_validator (~> 2.0.0)
acts_as_list (~> 1.0.1)
acts_as_tree (~> 2.9.0)
addressable (~> 2.8.0)
diff --git a/app/contracts/user_preferences/update_contract.rb b/app/contracts/user_preferences/update_contract.rb
index 2b6e0ce9947..fc785e0090a 100644
--- a/app/contracts/user_preferences/update_contract.rb
+++ b/app/contracts/user_preferences/update_contract.rb
@@ -30,5 +30,17 @@
module UserPreferences
class UpdateContract < BaseContract
+ property :settings
+
+ USER_PREFERENCE_SCHEMA = Rails.root.join('config/schemas/api/user_preferences.schema.json')
+
+ validates :settings,
+ presence: true,
+ json: {
+ message: ->(errors) do
+ errors.map { |error| JSONSchemer::Errors.pretty(error) }
+ end,
+ schema: USER_PREFERENCE_SCHEMA
+ }
end
end
diff --git a/config/schemas/api/user_preferences.schema.json b/config/schemas/api/user_preferences.schema.json
new file mode 100644
index 00000000000..81594fc5790
--- /dev/null
+++ b/config/schemas/api/user_preferences.schema.json
@@ -0,0 +1,64 @@
+{
+ "$schema": "http://json-schema.org/draft-07/schema#",
+ "$ref": "#/definitions/UserPreferences",
+ "definitions": {
+ "UserPreferences": {
+ "type": "object",
+ "additionalProperties": false,
+ "properties": {
+ "hide_mail": {
+ "type": "boolean"
+ },
+ "time_zone": {
+ "type": ["string", "null"]
+ },
+ "warn_on_leaving_unsaved": {
+ "type": "boolean"
+ },
+ "comments_sorting": {
+ "type": "string"
+ },
+ "auto_hide_popups": {
+ "type": "boolean"
+ },
+ "self_notified": {
+ "type": "boolean"
+ },
+ "daily_reminders": {
+ "$ref": "#/definitions/DailyReminders"
+ }
+ },
+ "required": [
+ "auto_hide_popups",
+ "comments_sorting",
+ "daily_reminders",
+ "hide_mail",
+ "self_notified",
+ "time_zone",
+ "warn_on_leaving_unsaved"
+ ],
+ "title": "Welcome"
+ },
+ "DailyReminders": {
+ "type": "object",
+ "additionalProperties": false,
+ "properties": {
+ "enabled": {
+ "type": "boolean"
+ },
+ "times": {
+ "type": "array",
+ "items": {
+ "type": "string",
+ "format": "time"
+ }
+ }
+ },
+ "required": [
+ "enabled",
+ "times"
+ ],
+ "title": "DailyReminders"
+ }
+ }
+}
diff --git a/lib/api/v3/user_preferences/user_preference_representer.rb b/lib/api/v3/user_preferences/user_preference_representer.rb
index b11e8cd601a..6c2b9f68569 100644
--- a/lib/api/v3/user_preferences/user_preference_representer.rb
+++ b/lib/api/v3/user_preferences/user_preference_representer.rb
@@ -65,6 +65,8 @@ module API
as: :commentSortDescending
property :auto_hide_popups
+ property :daily_reminders
+
property :notification_settings,
as: :notifications,
exec_context: :decorator,