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/validators/duration_validator.rb | |
parent | ac531c0ee5121faed524157fc1a2e0d4bd13297d (diff) |
Introduce DurationValidator, feedback:
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/9219#note_24032923
Diffstat (limited to 'app/validators/duration_validator.rb')
-rw-r--r-- | app/validators/duration_validator.rb | 17 |
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 |