summaryrefslogtreecommitdiff
path: root/app/validators
diff options
context:
space:
mode:
authorLin Jen-Shin <godfat@godfat.org>2017-02-24 17:28:24 +0800
committerLin Jen-Shin <godfat@godfat.org>2017-02-24 17:28:24 +0800
commit728b0a5fe073f1f2f088f9d2c6ce9a4a050e1fcf (patch)
tree0db3d34799ac3ce36646dfa245965a89a26b72d5 /app/validators
parentac531c0ee5121faed524157fc1a2e0d4bd13297d (diff)
downloadgitlab-ce-728b0a5fe073f1f2f088f9d2c6ce9a4a050e1fcf.tar.gz
Introduce DurationValidator, feedback:27762-add-default-artifacts-expiration
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/9219#note_24032923
Diffstat (limited to 'app/validators')
-rw-r--r--app/validators/duration_validator.rb17
1 files changed, 17 insertions, 0 deletions
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