Introduce DurationValidator, feedback:

https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/9219#note_24032923
This commit is contained in:
Lin Jen-Shin 2017-02-24 17:28:24 +08:00
parent ac531c0ee5
commit 728b0a5fe0
2 changed files with 18 additions and 8 deletions

View file

@ -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

View file

@ -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