debian-mirror-gitlab/app/validators/cron_validator.rb

17 lines
629 B
Ruby
Raw Normal View History

2018-11-08 19:23:39 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class CronValidator < ActiveModel::EachValidator
2020-05-24 23:13:21 +05:30
ATTRIBUTE_WHITELIST = %i[cron freeze_start freeze_end].freeze
NonWhitelistedAttributeError = Class.new(StandardError)
2017-08-17 22:00:37 +05:30
def validate_each(record, attribute, value)
2020-05-24 23:13:21 +05:30
if ATTRIBUTE_WHITELIST.include?(attribute)
cron_parser = Gitlab::Ci::CronParser.new(record.public_send(attribute), record.cron_timezone) # rubocop:disable GitlabSecurity/PublicSend
record.errors.add(attribute, " is invalid syntax") unless cron_parser.cron_valid?
else
raise NonWhitelistedAttributeError.new "Non-whitelisted attribute"
end
2017-08-17 22:00:37 +05:30
end
end