summaryrefslogtreecommitdiff
path: root/app/validators
diff options
context:
space:
mode:
authorShinya Maeda <gitlab.shinyamaeda@gmail.com>2017-04-01 02:02:26 +0900
committerShinya Maeda <gitlab.shinyamaeda@gmail.com>2017-04-06 23:46:58 +0900
commit57d082f3589060c90c2841dd52dda77574f5d984 (patch)
treeddf11a4ccffd21c0976273077a848d8488b393ff /app/validators
parent21cabf381b55ab2747d773ae1eeb70d2bb40e9a5 (diff)
downloadgitlab-ce-57d082f3589060c90c2841dd52dda77574f5d984.tar.gz
Add validator
Diffstat (limited to 'app/validators')
-rw-r--r--app/validators/cron_validator.rb16
-rw-r--r--app/validators/ref_validator.rb10
2 files changed, 26 insertions, 0 deletions
diff --git a/app/validators/cron_validator.rb b/app/validators/cron_validator.rb
new file mode 100644
index 00000000000..ad70e0897ba
--- /dev/null
+++ b/app/validators/cron_validator.rb
@@ -0,0 +1,16 @@
+# CronValidator
+#
+# Custom validator for Cron.
+class CronValidator < ActiveModel::EachValidator
+ def validate_each(record, attribute, value)
+ cron_parser = Ci::CronParser.new(record.cron, record.cron_time_zone)
+ is_valid_cron, is_valid_cron_time_zone = cron_parser.validation
+ next_time = cron_parser.next_time_from(Time.now)
+
+ if !is_valid_cron
+ record.errors.add(:cron, " is invalid syntax")
+ elsif !is_valid_cron_time_zone
+ record.errors.add(:cron_time_zone, " is invalid timezone")
+ end
+ end
+end
diff --git a/app/validators/ref_validator.rb b/app/validators/ref_validator.rb
new file mode 100644
index 00000000000..2024255a770
--- /dev/null
+++ b/app/validators/ref_validator.rb
@@ -0,0 +1,10 @@
+# RefValidator
+#
+# Custom validator for Ref.
+class RefValidator < ActiveModel::EachValidator
+ def validate_each(record, attribute, value)
+ unless record.project.repository.branch_exists?(value)
+ record.errors.add(attribute, " does not exist")
+ end
+ end
+end