diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2017-02-24 12:28:24 +0300 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2017-02-24 12:28:24 +0300 |
commit | 728b0a5fe073f1f2f088f9d2c6ce9a4a050e1fcf (patch) | |
tree | 0db3d34799ac3ce36646dfa245965a89a26b72d5 /app | |
parent | ac531c0ee5121faed524157fc1a2e0d4bd13297d (diff) |
Introduce DurationValidator, feedback:
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/9219#note_24032923
Diffstat (limited to 'app')
-rw-r--r-- | app/models/application_setting.rb | 9 | ||||
-rw-r--r-- | app/validators/duration_validator.rb | 17 |
2 files changed, 18 insertions, 8 deletions
diff --git a/app/models/application_setting.rb b/app/models/application_setting.rb index 99759ec4cae..dc36c754438 100644 --- a/app/models/application_setting.rb +++ b/app/models/application_setting.rb @@ -80,8 +80,7 @@ class ApplicationSetting < ActiveRecord::Base presence: true, numericality: { only_integer: true, greater_than: 0 } - validates :default_artifacts_expire_in, presence: true - validate :check_default_artifacts_expire_in + validates :default_artifacts_expire_in, presence: true, duration: true validates :container_registry_token_expire_delay, presence: true, @@ -305,10 +304,4 @@ class ApplicationSetting < ActiveRecord::Base errors.add(:repository_storages, "can't include: #{invalid.join(", ")}") unless invalid.empty? end - - def check_default_artifacts_expire_in - ChronicDuration.parse(default_artifacts_expire_in) - rescue ChronicDuration::DurationParseError - errors.add(:default_artifacts_expire_in, "is not a correct duration") - end end diff --git a/app/validators/duration_validator.rb b/app/validators/duration_validator.rb new file mode 100644 index 00000000000..10ff44031c6 --- /dev/null +++ b/app/validators/duration_validator.rb @@ -0,0 +1,17 @@ +# DurationValidator +# +# Validate the format conforms with ChronicDuration +# +# Example: +# +# class ApplicationSetting < ActiveRecord::Base +# validates :default_artifacts_expire_in, presence: true, duration: true +# end +# +class DurationValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + ChronicDuration.parse(value) + rescue ChronicDuration::DurationParseError + record.errors.add(attribute, "is not a correct duration") + end +end |